Friday, 4 September 2009

Java Memory Insufficent

 

The JVM heap space is where all Java objects are stored, as well as memory used by the garbage collector. Sun recommends increasing this value for server applications,

Large server applications often experience two problems with these 
defaults. One is slow startup, because the initial heap is small and
must be resized over many major collections. A more pressing problem
is that the default maximum heap size is unreasonably small for most
server applications.



If an application gobbles up memory until it throws an OutOfMemory exception, or seems to be spending an excessive amount of time doing garbage collection, a heap dump can help track down the source of the problem.



Three type of Insufficent Memory:



1. Heap memory error: when an application creates a new object but the heap does not have sufficient space and cannot be expanded further, and OutOfMemoryError will be thrown with the following error message:



java.lang.OutofMemoryError: Java heap space



2. Non-heap memory error: The permanent generation is a non-heap memory area in the HotSpot VM implementation that stores per-class structure as well as interned strings. When the permanent generation is full, the application will fail to load a class or to allocate an interned string, and an OutOfMemoryError will be thrown with the following error message:



java.lang.OutOfMemoryError: PermGen space



3. Native memory error: The Java Native Interface(JNI) code or the native library of an applicaiton and the JVM implementation allocate memory from the native heap. An OutOfMemoryError will be thrown when an allocation in the native heap fails. For instance, the following error message indicates insufficient swap space, which could be caused by a configuration issue in the operating system or by another process in the system that is consuming much of memory.



java.lang.OutOfMemoryError: request <size> bytes for <reason>. Out of swap space?



Reasons for Insufficent Memory



Momory Leaks: An applicatoin keeps a reference to an object that it no longer needs. Garbage collector cannot free this part of memory.



Finalizers: excessive use of finalizers.



DeadLocks:



Looping Threads:



High Lock Contention:



 











          How to remotely monitoring JVM



          1) Opening a monitoring port on the remote Tomcat server, we added following java VM options at the startup script, 



          Export JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.port=19993 -Dcom.sun.management.jmxremote.authenticate=

          false -Dcom.sun.management.jmxremote.ssl=false"



          This requires firewall opening between the local pc and server on port specified 19993.



          2) There are two good tools for monitoring JVM, i.e. JConsole and VirtualVM. run these two appliation and login the remote server and the specified port, then you may monitor the VM state.



          JConsole is shipped with JDK after version 5. You may easily find it from JAVA_HOME/bin/jconsole.exe



          VisualVM is a third party software, which can be download from its official home.



          How to Reset Heap Memory Size



          Sometime the insuficent memory is just caused by the insufficent heap memory, so it need to reset the memory size.The Java Virtual Machine takes two command line arguments which set the initial and maximum heap sizes:



          -Xms and -Xmx. For example if you want to give your Java program needs a 128Mb initial and 256Mb maximum heap size you could launch it as follows:



          JAVA_OPTS="-server -Xms128M -Xmx256M"



           



          Set HeapDumpOnOutOfMemoryError Option



          set JAVA_OPTS=%JAVA_OPTS% -server -Xms512m -Xmx800m -XX:PermSize=64M -XX:MaxPermSize=128m -Djava.awt.headless=true -XX:+HeapDumpOnOutOfMemoryError -XX:+HeapDumpOnCtrlBreak



          A heap dump file will be generated in /bin folder, when out of memory happens.



          The -XX:+HeapDumpOnOutOfMemoryError command-line option was introduced in Java SE release


          5.0 update 7. This option tells the HotSpot VM to generate a heap dump when the first thread throws a


          java.lang.OutOfMemoryError because the Java heap or the permanent generation is full. There is


          no overhead in running with this option, and so it can be useful for production systems where


          OutOfMemoryError takes a long time to surface.


          The heap dump is in HPROF binary format, and so it can be anaylzed by any tool that can import this


          format, for example the Heap Analysis Tool (HAT).


          By default the heap dump is created in a file called java_pid<pid>.hprof in the working directory


          of the VM, where <pid> is the process ID. You can specify an alternative file name or directory with


          the -XX:HeapDumpPath= option. For example, -XX:HeapDumpPath=/disk2/dumps will cause the


          heap dump to be generated in the /disk2/dumps directory.



          How to monitor JVM



          Using jconsole



          I sent several banches of data to the debitorregisterweb, and it seems that there are some heap memory that cannot be released afterwards, the used the heap space increased step by step, where is the gc operation,



          How to fix memory leak



          http://java.dzone.com/news/how-fix-memory-leaks-java



          How profile Java application in VirtualVM



          VisualVM - tool for profiling Java applications

          No comments:

          Can Jackson Deserialize Java Time ZonedDateTime

          Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...