Plug memory leaks in enterprise Java applications
Strategies for detecting and fixing enterprise memory leaks
By Ambily Pankajakshan, JavaWorld.com, 03/13/06
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
The Java automatic garbage collection process typically operates as a low-priority thread that constantly searches memory
for unreachable objects, objects not referenced by any other object reachable by a live thread. Different JVMs use different algorithms to
determine how to collect garbage most efficiently.
In the JVM, memory is allocated in two regions:
- Stack: Where local variables (declared in methods and constructors) are allocated. Local variables are allocated when a method is
invoked and de-allocated when the method is exited.
- Heap: Where all objects created with the
new keyword are allocated. Since local variables are few in number, only primitive types and references, usually the stack will
not overflow, except in cases of unusually deep or infinite recursion. The JVM throws a Java out-of-memory error if it is
not able to get more memory in the heap to allocate more Java objects. The JVM cannot allocate more objects if the heap is
full of live objects and unable to expand further.
Causes of memory leaks in Java
The four typical causes of memory leaks in a Java program are:
- Unknown or unwanted object references: These objects are no longer needed, but the garbage collector can not reclaim the memory because another object still refers
to it.
- Long-living (static) objects: These objects stay in the memory for the application's full lifetime. Objects tagged to the session may also have the same
lifetime as the session, which is created per user and remains until the user logs out of the application.
- Failure to clean up or free native system resources: Native system resources are resources allocated by a function external to Java, typically native code written in C or C++.
Java Native Interface (JNI) APIs are used to embed native libraries/code into Java code.
- Bugs in the JDK or third-party libraries: Bugs in various versions of the JDK or in the Abstract Window Toolkit and Swing packages can cause memory leaks.
Detection of memory leaks
Some approaches to detecting memory leaks follow in the list below:
- Use the operating system process monitor, which tells how much memory a process is using. On Microsoft Windows, the task manager
can show memory usage when a Java program is running. This mechanism gives a coarse-grained idea of a process's total memory
utilization.
- Use the
totalMemory() and freeMemory() methods in the Java runtime class, which shows how much total heap memory is being controlled by the JVM, along with how
much is not in use at a particular time. This mechanism can provide the heap's memory utilization. However, details of the
heap utilization indicating the objects in the heap are not shown.
- Use memory-profiling tools, e.g., JProbe Memory Debugger. These tools can provide a runtime picture of the heap and its utilization. However, they can be used only during development,
not deployment, as they slow application performance considerably.
Causes of memory leaks in enterprise Java applications
In the subsequent sections, I analyze some causes of memory leaks in enterprise Java applications using a sample application
and a memory profiling tool. I also suggest strategies for detecting and plugging such leaks in your own projects.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- JVM specification
http://java.sun.com/docs/books/vmspec
- Download JProbe Memory Debugger
http://www.quest.com/jprobe/debugger.asp
- J2EE Design Patterns, by William C. R. Crawford and Jonathan Kaplan (O'Reilly, September 2003; ISBN0596004273) is a good book on J2EE design patterns
and anti-patterns
http://www.amazon.com/exec/obidos/ASIN/0596004273/javaworld
- Learn more about weak references"Reference Objects and Garbage Collection," Monica Pawlan (Sun Developer Network, August 1998)
http://java.sun.com/developer/technicalArticles/ALT/RefObj
- For more on the Singleton pattern, read "Simply Singleton," David Geary (JavaWorld, April 2003)
http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html?
- For the basics on garbage collection, read JavaWorld's "Trash Talk" series from Jeff Friesen:
-
- For more on the JVM, browse the Java Virtual Machine section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-jvm-index.shtml
- For more articles on design patterns, browse the Design Patterns section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-patterns-index.shtml
- For more articles on Java EE, browse the Java Enterprise Edition section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-j2ee-index.shtml?
A class with a static member is called static class???????...By Anonymous on March 13, 2009, 12:22 amA class with a static member is called static class???????. Is this right ? I guess a class with a static keyword is called static class. Not all the classes which...
Reply | Read entire comment
It is not the heap . You should clear the contents inside the CoBy Anonymous on March 13, 2009, 12:18 amIt is not the heap . You should clear the contents inside the Collection. You can call clear method for this.
Reply | Read entire comment
how to ...By Anonymous on December 30, 2008, 11:35 amPardon me, but how can I clear the heap programmatically? System.gc does not clear the heap, correct? how can I use the Business tier ? Storing data in a database...
Reply | Read entire comment
View all comments