Process to deal with java.lang.outOfMemory
I was facing java.lang.outOfMemory
error while running one of the java class file on HP-Unix machine.
This was the command which was not running for me:-
java com.documentum.ldap.LDAPSync -docbase_name cbec_repo -user_name dmadmin -password dmadmin -source_directory LDAPtest -full_sync true
This was the modified version of the command which went successful for me:-
java -Xms1024m -Xmx1024m com.documentum.ldap.LDAPSync -docbase_name cbec_repo -user_name dmadmin -password dmadmin -source_directory LDAPtest -full_sync true
Every time we face problem, get solution and forget everything after that. I have a better approach in order to make sure you learn while you work:-
1. What was the exact problem?
2. Why we got this problem?
3. How this can be resolved?
4. How can we avoid this problem in future?
5. Last but not least “How can we remember this solution?”
So in this paper we will try to follow the same thing here:--
What was the exact problem?
java.lang.Object
java.lang.Throwable
java.lang.Error
java.lang.VirtualMachineError
java.lang.OutOfMemoryError
What was the exact problem?
Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.
So VM is out of memory and possible reasons can be anything:
1. Memory usage is really too high(but I don’t think this could be a problem as java supports a lot of memory space)
2. Memory Leaks are there in your program
3. Memory is too less and so many others that I have not mentioned here.
Why we got this problem?
So we need to know the status of our memory allocation—how much is left, how much we have and so on. Please run this as a java class and get the result:-
Runtime runtime =Runtime.getRuntime();
long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
System.out.println("free memory: "+ freeMemory /1024);
System.out.println("allocated memory: "+ allocatedMemory /1024);
System.out.println("max memory: "+ maxMemory /1024);
System.out.println("total free memory: "+ (freeMemory + (maxMemory - allocatedMemory)) / 1024);
Based on the result you can identify the real problem.
How this can be resolved?
Solution is Java HotSpot VM options.There are two types of options:-
Options that begin with-X
are non-standard (not guaranteed to be supported on all VM implementations), and are subject to change without notice in subsequent releases of the JDK.
Options that are specified with-XX
are not stable and are not recommended for casual use. These options are subject to change without notice.
For more please read this:-
http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp
We are using –X options(non-standard) to resolve our issue.
Use java –X to have a look at them. For all the parameters that are used with java application launcher please look at this url.
http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html
http://java.sun.com/performance/reference/whitepapers/tuning.html
Regarding –XX options please use the above urls.
As you can see there are two options –Xms and –Xmx that are used to increase and set heap size.So restart you jvm with these parameters like
java -Xms<some_min_size> -Xmx<some_max_size> you.own.code.com
How can we avoid this problem in future?
Avoid memory leaks in your code. For that you have to follow the best practices.
You can read more from these urls:-
http://www.ibm.com/developerworks/library/j-leaks/index.html
http://www.javaworld.com/javaworld/javatips/jw-javatip79.html?page=2
Use tools that are provided by java or any other open source tools to understand where your memory is being consumed more:-
Monitoring and Management Tools
You can use the following tools to monitor JVM performance and resource consumption. The tools described in this section are unsupported and experimental, and should be used with that in mind. They may not be available in future JDK versions.
Platform support:
- jconsole: all platforms.
- jps, jstat, and jstatd: all platforms except Windows 98 and Windows ME.
For more information, see Monitoring and Management for the Java Platform.
Tool Name | Brief Description |
Experimental: Java Monitoring and Management Console - JMX-compliant graphical tool for monitoring a Java virtual machine. It can monitor both local and remote JVMs. | |
Experimental: JVM Process Status Tool - Lists instrumented HotSpot Java virtual machines on a target system. | |
Experimental: JVM Statistics Monitoring Tool - Attaches to an instrumented HotSpot Java virtual machine and collects and logs performance statistics as specified by the command line options. | |
Experimental: JVM jstat Daemon - Launches an RMI server application that monitors for the creation and termination of instrumented HotSpot Java virtual machines and provides a interface to allow remote monitoring tools to attach to Java virtual machines running on the local system. |
The following tools can be used for specific troubleshooting tasks. The tools described in this section are unsupported and experimental in nature and should be used with that in mind. They may not be available in future JDK versions.
These tools are not currently available on Windows platforms.
Tool Name | Brief Description |
Experimental - Configuration Info for Java - Prints configuration information for for a given process or core file or a remote debug server. | |
Experimental - Memory Map for Java - Prints shared object memory maps or heap memory details of a given process or core file or a remote debug server. | |
Experimental - Serviceability Agent Debug Daemon for Java - Attaches to a process or core file and acts as a debug server. | |
Experimental - Stack Trace for Java - Prints a stack trace of threads for a given process or core file or remote debug server. |
Refer to the Java TM 2 Platform, Standard Edition 5.0 Trouble-Shooting and Diagnostic Guide which provides descriptions of tools, options, and other items for use in analyzing problems. This document contains suggestions about what to try before submitting a bug report and what data to collect for a report.
Last but not least “How can we remember this solution?”
Discuss this with other people and interact with other experts. Try this by writing some code and contribute to the society by writing blogs or papers.
Never write the same thing that is already present on internet instead give the urls because I don’t believe in re-inventing the wheels. java.lang.OutofMemory is of various reasons and varieties depends on message which are not considered here (Please check which one you are getting and then check if you are also getting the same as this one or other).
0 comments:
Post a Comment