tags:

views:

229

answers:

4

I don't have an example of code but I'm curious if you do bad coding practice, is that possible?

+5  A: 

Of course you can. The typical example of a memory leak is if you build a cache that you never flush manually and that has no automatic eviction policy.

Antoine P.
Technically that's not a memory leak as the application is still **able** to release the memory, although it chooses not to.
Kragen
Well I just think we define "memory leak" differently, then. For me a leak is a leak, regardless of whether it is possible to fix it or not.
Antoine P.
If it doesn't have code to remove elements from the cache then it isn't able to remove them, is it?
Laurence Gonsalves
I think a memory leak is anything that doesn't release memory when it's done fulfilling its purpose. I wouldn't call an improperly managed cache a memory leak however, only because it's not really done. It's just wasteful.
orokusaki
By your definition a long-running process cannot have a memory leak until it is "done", which sounds a bit improper to me.
Antoine P.
+1  A: 

In the sense of orphaning allocated objects after they go out of scope because you forgot to deallocate them, no; Python will automatically deallocate out of scope objects (Garbage Collection). But in the sense that @Antione is talking about, yes.

Rob Curtis
+18  A: 

It is possible, yes.

It depends on what kind of memory leak you are talking about. Within pure python code, it's not possible to "forget to free" memory such as in C, but it is possible to leave a reference hanging somewhere. Some examples of such:

  • an unhandled traceback object that is keeping an entire stack frame alive, even though the function is no longer running

  • storing values in a class or global scope instead of instance scope, and not realizing it.

  • Cyclic references in classes which also have a __del__ method. Ironically, the existence of a __del__ makes it impossible for the cyclic garbage collector to clean an instance up.

  • poorly implemented C extensions, or not properly using C libraries as they are supposed to be.

  • Scopes which contain closures which contain a whole lot more than you could've anticipated

  • Default parameters which are mutable types:

.

def foo(a=[]):
    a.append(time.time())
    return a

And lots more.....

Crast
Nice mention of the mutable default parameters. They are dangerous because the way they work is not really intuitive, but also because they can grow quite a lot if you don't take care... +1
Flávio Amieiro
+1 for mentioning that foo method implementation. I never knew that!
Maddy
+5  A: 

The classic definition of a memory leak is memory that was used once, and now is not, but has not been reclaimed. That nearly impossible with pure Python code. But as Antoine points out, you can easily have the effect of consuming all your memory inadvertently by allowing data structures to grow without bound, even if you don't need to keep all of the data around.

With C extensions, of course, you are back in unmanaged territory, and anything is possible.

Ned Batchelder