Over the years, I've occasionally heard the term "memory leak" but, in all
honesty, I've never quite understood what that is.
What exactly is a memory leak? What causes them? What are the consequences
of memory leaks? How can they be prevented? How can they be detected if you
think you might have one?
Also, I recently saw a passage in a newsgroup post that described files left
open by programs as memory leaks. Is that a misuse of the terminology? I'm
curious about this because I'm currently experiencing some odd behaviour
from a file that may have been inadvertenly left open when my program ended:
some of the lines that should have been written to the file weren't there
*and*, when I tried to delete the file, Windows claimed it was "in use" even
though my program had ended and no other program used the file. I finally
had to reboot to free the file so that I could delete it. Is this an example
of a memory leak and of the consequences or is this something completely
different?

Signature
Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare
Tom Dyess - 11 Mar 2005 01:36 GMT
----- Original Message -----
From: "Rhino" <rhino1@NOSPAM.sympatico.ca>
Newsgroups: comp.lang.java.help
Sent: Thursday, March 10, 2005 5:32 PM
Subject: Memory leaks
> Over the years, I've occasionally heard the term "memory leak" but, in all
> honesty, I've never quite understood what that is.
[quoted text clipped - 18 lines]
> of a memory leak and of the consequences or is this something completely
> different?
The webpedia definition is this:
A bug in a program that prevents it from freeing up memory that it no longer
needs. As a result, the program grabs more and more memory until it finally
crashes because there is no more memory left.
It's caused by allocating memory but forgetting to deallocate it when
finished. A common example is instantiating an object without freeing it. It
is much more common in compiled languages like C++/Delphi, but can happen in
Java if you create objects in a scope that itself is never dereferenced
(like a global variable). There are various memory profilers available for
this sort of thing. The best way I've found to prevent this is to always
write the "free" (as in freedom, not free beer) code immediately after you
write the instantiation code.
The file issue you are having is not a memory leak and is most probably
related to not closing the file. I'm not sure if Java should/would
automatically release the lock after the jvm has closed.
Tom Dyess
OraclePower.com
Dan Nuttle - 11 Mar 2005 02:57 GMT
There are two basic defintions that I know of for a memory leak.
1. A very specific definition says that a memory leak occurs whenever
objects in memory cannot be dereferenced because there are no longer any
references available to them.
2. Anything else.
The first one occurs in some languages when you do something like this.
Class A has a reference to Class B, and Class B has a reference to Class A.
If your program loses all references to both instances of these classes,
then there is no way that you can tell A to break its reference to B.
They're still pointing at each other, so they stay in memory.
This no longer happens in Java, since 1.3 I think. They made the garbage
collector more aggressive, so that if there are no longer any scoped
references to an object, it is garbage collected. In other words, A can
refer only to B, and B can refer only to A, and your program can't refer to
either of them. So out they go.
So these days, in Java the term "memory leak" has come to mean things like
caches that you have created that hold objects that your code "forgets" to
release. Or the cache grows big enough, fast enough, to drag down
performance or even crash the JVM. If you ask me, that's not really a
memory leak: that's a bug.
> Over the years, I've occasionally heard the term "memory leak" but, in all
> honesty, I've never quite understood what that is.
[quoted text clipped - 13 lines]
> of a memory leak and of the consequences or is this something completely
> different?
Rhino - 11 Mar 2005 14:03 GMT
> There are two basic defintions that I know of for a memory leak.
>
[quoted text clipped - 21 lines]
> performance or even crash the JVM. If you ask me, that's not really a
> memory leak: that's a bug.
Thanks to both of you for your answers!
Rhino