Leak memory

In what situations can a Java-program leak memory?

Few hunches:

- unnecessarily holding static references to objects

- unnecessarily holding references to objects in a long-living thread

- not closing connections, statements or resultsets

- writing native code that leaks memory

- using libraries or JVM-implementation that leak memory

Any other ideas?

[401 byte] By [LonnyBa] at [2007-9-30 2:01:19]
# 1

Here are some references I can add during my lunch hour (I am sure

others will contribure more):

See Chapter two "Creating and Destroying Objects" of Bloch's book

Effective Java, especially sections 5 Eliminate obsolete object

references and 6 Avoid finalizers

http://java.sun.com/docs/books/effective/

http://java.sun.com/docs/books/effective/toc.html

Use of finalizers, or anything else that requests work to be done

at some unspecified time in the future (such as java.io.File.deleteOnExit()),

will resemble a memory leak because the system must retain enough

state (possibly including strong references to objects) to be able to

do the work later.

debugging_teama at 2007-7-16 13:11:26 > top of Java-index,Archived Forums,Debugging Tools and Techniques...
# 2

Thanx, in some of my code I do use java.io.File.deleteOnExit(). Especially when used in a webapp where the JVM does not *exits* unless the webserver (e.g. Tomcat or Resin) is restarted completely, memory can go crazy. So I conclude: don't use java.io.File.deleteOnExit() in webapps on production-machines.

LonnyBa at 2007-7-16 13:11:26 > top of Java-index,Archived Forums,Debugging Tools and Techniques...
# 3

> In what situations can a Java-program leak memory?

>

> Few hunches:

> - unnecessarily holding static references to objects

> - unnecessarily holding references to objects in a

> long-living thread

> - not closing connections, statements or resultsets

> - writing native code that leaks memory

> - using libraries or JVM-implementation that leak

> memory

>

> Any other ideas?

not closing connections, statements or resultsets

is the most common cause of leaks I guess.

yrhduahcmarruhka at 2007-7-16 13:11:26 > top of Java-index,Archived Forums,Debugging Tools and Techniques...
# 4
ObjectInputStream/ObjectOutputStream keeps a record of every object it has ever seen. If these objects are long lived it can appear as a memory leak.
Peter-Lawreya at 2007-7-16 13:11:27 > top of Java-index,Archived Forums,Debugging Tools and Techniques...
# 5
> ObjectInputStream/ObjectOutputStream keeps a record of> every object it has ever seen. If these objects are> long lived it can appear as a memory leak.yup, thus the reset() method
dmbdmba at 2007-7-16 13:11:27 > top of Java-index,Archived Forums,Debugging Tools and Techniques...
# 6
How would you do this in java1.3? Mark/Reset is not implemented in ObjectInputStream in 1.3 and it throws the default IOException from the InputStream class. Any help will be highly appreciated.
RajibBhakata at 2007-7-16 13:11:27 > top of Java-index,Archived Forums,Debugging Tools and Techniques...
# 7
Creating JNI Global References and never deleting them.
kellyohaira at 2007-7-16 13:11:27 > top of Java-index,Archived Forums,Debugging Tools and Techniques...
# 8
placing an object into a collection and forgetting to remove it. The memory used by that object will never be reclaimed. &#9824
yrhduahcmarruhka at 2007-7-16 13:11:27 > top of Java-index,Archived Forums,Debugging Tools and Techniques...
# 9

Do you imply its so obvious from the following wonderfully terse and elegant javadoc? I wasted 3 days for unwanted, unneeded, undocumented caching. What about a "CachingObjectOutputStream" instead?

reset

public void reset()

throws IOExceptionReset will disregard the state of any objects already written to the stream. The state is reset to be the same as a new ObjectOutputStream. The current point in the stream is marked as reset so the corresponding ObjectInputStream will be reset at the same point. Objects previously written to the stream will not be refered to as already being in the stream. They will be written to the stream again.

jccq2a at 2007-7-16 13:11:27 > top of Java-index,Archived Forums,Debugging Tools and Techniques...
# 10
The reset method doesn't work for ObjectInputStream. If anyone know how to clear the ObjectInputStream's cache of read Object please let me know.
tedmbeara at 2007-7-16 13:11:27 > top of Java-index,Archived Forums,Debugging Tools and Techniques...