Re: Looping-related Memory Leak



On Jun 30, 1:55 pm, Tom Davis <binju...@xxxxxxxxx> wrote:
On Jun 26, 5:38 am, Carl Banks <pavlovevide...@xxxxxxxxx> wrote:



On Jun 26, 5:19 am, Tom Davis <binju...@xxxxxxxxx> wrote:

I am having a problem where a long-running function will cause a
memory leak / balloon for reasons I cannot figure out. Essentially, I
loop through a directory of pickled files, load them, and run some
other functions on them. In every case, each function uses only local
variables and I even made sure to use `del` on each variable at the
end of the loop. However, as the loop progresses the amount of memory
used steadily increases.

Do you happen to be using a single Unpickler instance? If so, change
it to use a different instance each time. (If you just use the module-
level load function you are already using a different instance each
time.)

Unpicklers hold a reference to everything they've seen, which prevents
objects it unpickles from being garbage collected until it is
collected itself.

Carl Banks

Carl,

Yes, I was using the module-level unpickler. I changed it with little
effect. I guess perhaps this is my misunderstanding of how GC works.
For instance, if I have `a = Obj()` and run `a.some_method()` which
generates a highly-nested local variable that cannot be easily garbage
collected, it was my assumption that either (1) completing the method
call or (2) deleting the object instance itself would automatically
destroy any variables used by said method. This does not appear to be
the case, however. Even when a variable/object's scope is destroyed,
it would seem t hat variables/objects created within that scope cannot
always be reclaimed, depending on their complexity.

To me, this seems illogical. I can understand that the GC is
reluctant to reclaim objects that have many connections to other
objects and so forth, but once those objects' scopes are gone, why
doesn't it force a reclaim?


Are your objects involved in circular references, and do you have any
objects with a __del__ method? Normally objects are reclaimed when
the reference count goes to zero, but if there are cycles then the
reference count never reaches zero, and they remain alive until the
generational garbage collector makes a pass to break the cycle.
However, the generational collector doesn't break cycles that involve
objects with a __del__method.

Are you calling any C extensions that might be failing to decref an
object? There could be a memory leak.

Are you keeping a reference around somewhere. For example, appending
results to a list, and the result keeps a reference to all of your
unpickled data for some reason.


You know, we can throw out all these scenarios, but these suggestions
are just common pitfalls. If it doesn't look like one of these
things, you're going to have to do your own legwork to help isolate
what's causing the behavior. Then if needed you can come back to us
with more detailed information.

Start with your original function, and slowly remove functionality
from it until the bad behavior goes away. That will give you a clue
what's causing it.


Carl Banks
.



Relevant Pages

  • Re: Looping-related Memory Leak
    ... memory leak / balloon for reasons I cannot figure out. ... end of the loop. ... reference count never reaches zero, and they remain alive until the ... the generational collector doesn't break cycles that involve ...
    (comp.lang.python)
  • Possible to assure no "cyclic"/"uncollectible" memory leaks?
    ... is defined and a cyclic reference is made, ... collector cannot clean it up. ... have never used it in my Python programming. ... situation that would create a memory leak using a language that is ...
    (comp.lang.python)
  • Re: StackOverflow Exception in JNI
    ... > I have a loop written in C/JNI which calls back a Java object (which ... temporary objects in your native method and then return a valid ... reference without thinking too much about these issues. ...
    (comp.lang.java.programmer)
  • Re: to dispose or not ?
    ... always have the system memory roots available. ... collector for the Gen0 and Gen1 heaps and a mark/sweep/compact collector for ... heap or compacts free space out of the heap. ... the reference from the variable to the object in the heap. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: to dispose or not ?
    ... always have the system memory roots available. ... It also means that MS took some shortcuts in the garbage collector to improve the performance of finalizers. ... the reference from the variable to the object in the heap. ...
    (microsoft.public.dotnet.languages.vb)