Search code examples
pythonwindowsguppyheapy

Guppy/Heapy on Windows with Python 2.7.6


I just intalled guppy 0.1.10. Then typing in Ipython 2.1.0

from guppy import hpy
hp = hpy()

makes the console crash, i.e. windows tells me, that python.exe just crashed. Is there any workaround? Or a heapy alternative?


Solution

  • I had the same issues with guppy/heapy. Guppy is currently a little bit outdated. There is some kind of patch but I can't find it anymore. However it didn't worked for me in python 2.7. You can have also issues because of the OS arch (64 bit!?).

    There are other questions about memory profiling techniques:

    I personally think that the most valuable alternatives are:

    It is useful also to calculate the size of your objects and track it by yourself. I elaborated some code (originally by https://stackoverflow.com/users/216356/noctis-skytower) that I found in one of the StackOverflow questions (Approximately how much memory would a list of 80000 items consume in python?) to be compatible with Python 2.7 (should work in 3 also):

    totalSizeOf = lambda obj: sum(map(sys.getsizeof, explore(obj, set())))
    def explore(obj, memo):
        loc = id(obj)
        if loc not in memo:
            memo.add(loc)
            yield obj
            # Handle instances with slots.
            try:
                slots = obj.__slots__
            except AttributeError:
                pass
            else:
                for name in slots:
                    try:
                        attr = getattr(obj, name)
                    except AttributeError:
                        pass
                    else:
                        #yield from explore(attr, memo)
                        for bar in explore(attr, memo):
                            yield bar
            # Handle instances with dict.
            try:
                attrs = obj.__dict__
            except AttributeError:
                pass
            else:
                #yield from explore(attrs, memo)
                for bar in explore(attrs, memo):
                    yield bar
            # Handle dicts or iterables.
            for name in 'keys', 'values', '__iter__':
                try:
                    attr = getattr(obj, name)
                except AttributeError:
                    pass
                else:
                    for item in attr():
                        #yield from explore(item, memo)
                        for bar in explore(item, memo):
                            yield bar