Peter is a DZone MVB and is not an employee of DZone and has posted 154 posts at DZone. You can read more from them at their website. View Full User Profile

Java and Memory Leaks

07.25.2011
| 7093 views |
  • submit to reddit

The term "memory leak" is used in Java in a manner which is different to how it is used in other languages. What does a "memory leak" mean in general terminology and how is it used in Java?

Wikipedia definition

A memory leak, in computer science (or leakage, in this context), occurs when a computer program consumes memory but is unable to release it back to the operating system.
The JVM reserves the heap as virtual memory on startup and doesn't gives that memory back until it exits. This virtual memory turns into main memory as it is used. This is why the virtual size and the resident size for a JVM can be very different and the resident memory can grow without the virtual memory changing.

In object-oriented programming, a memory leak happens when an object is stored in memory but cannot be accessed by the running code.

The GC can always find every object on the heap, even those which are not reachable to the application. As such there is no object which is not reachable to running code.

however, many people refer to any unwanted increase in memory usage as a memory leak, though this is not strictly accurate from a technical perspective.

In Java, the amount of memory required cannot be determined without a full GC. It is normal to see the "used" memory of a Java application sawtooth. Returning to the same amount of memory used after each GC indicates the memory required has not increased.


A memory leak can diminish the performance of the computer by reducing the amount of available memory. Eventually, in the worst case, too much of the available memory may become allocated and all or part of the system or device stops working correctly, the application fails, or the system slows down unacceptably due to thrashing.

This is how the term is used in Java. There is a reduction in available memory, conclusion: it is a memory leak.

But is it really a memory leak?

Examples of "memory leaks" in Java

Recently, two questions were asked on StackOverflow which illustrate a variety of views over what a "memory leak" means in Java.

Is Go subject to the same subtle memory-leaks that Java is?
Creating a memory leak in Java

In the first article there was a view expressed that a "memory leak" is when an application holds references to objects which are no longer needed. Another view was that the JVM itself doesn't have memory leaks.

In the second article there were many examples of how the JVM could be used to hide away memory usage or operations which would consume memory. Examples included File.deleteOnExit(), creating large static fields, discarded sockets, file handles, threads, JDBC connection which you don't need.

The File.deleteOnExit on exit must use some memory to be able to perform its task. This is not a leak in my option as it is not retaining memory which is not needed.

Having static fields and threads which you don't need will waste memory and this is bug which the JVM cannot correct for.

However, creating sockets, file handles, and JDBC connections the JVM does handle. When the finalize() method is called, these resources are cleaned up.

Conclusion

In Java it is meaningful to refer to objects which are retained and are not longer needed as a "memory leak".

There is nothing in the Java language which is prone to this problem and the JRE libraries do protect themselves from bugs in application code.

Your application can still have a memory leak, and you need to make sensible choices about when you still need a resource, in particular a thread and if you hold onto it too long it can mean you have a "memory leak".

 

From http://vanillajava.blogspot.com/2011/07/java-and-memory-leaks.html

Published at DZone with permission of Peter Lawrey, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Jason Holbrook replied on Mon, 2011/07/25 - 3:43pm

I am interested in the Java application servers "memory leak" of PermGen with regard to redeployments of an enterprise application. Why can't this be solved?

Mirko Novakovic replied on Wed, 2011/07/27 - 1:00am

Hi Jason, it can be resolved if you fix the memory leak in your application :-)
It is a common misunderstanding that this is a problem of the application server when redeploying applications. In fact it is a special kind of a memory leak - especially with class loading.
To understand this in more detail you can read my article on Java memory architecture: Java Memory Architecture
There ist also a good documentation on how to analyze and fix these kinds of memory leaks with YourKit - which has a nice view to show you the objects in the heap sorted by class loader: YourKit Profiler Howto
Mirko

Lund Wolfe replied on Sat, 2011/07/30 - 9:54pm

I think the real concern with a "memory leak" in any language, whether caused by the application, a helper framework like an app server, or the JVM itself, is that the memory loss accumulates until performance comes to a crawl and/or you get an OutOfMemory exception. NullPointerException from using a pointer or reference variable where you lost your referent are pretty obvious program errors that are easily located and fixed.

Memory leaks are almost always caused by applications that ignore memory limits by accumulating new regular strong references while not letting unused ones go out of scope or not setting them to null or by not seeing the interlated references between objects that are never let go and that the application no longer uses (and probably shouldn't use). This can easily happen when there are many relationships between classes and object ownership responsibility is not clearly designed for and maintained.

You should definitely handle (close(), dispose(), etc) resources in your code, even if it has to be done in your object's finalize() method, rather than depend on the JVM or other code to do the right thing for you.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.