Java Memory Overview

Java Memory overview tutorial intended for java beginners to learn above memory management like memory types etc.  In Nutshell, java memory management deals with:

  • Allocation of memory to new Object
  • Removing unused objects through Automatic Garbage Collection

Heap Memory:

  • All objects with new operator are created with in Heap memory.
  • Heap memory creates on  application startup for every application, therefore JVM heap is different from application heap.
  • Garbage collector removes the unused java objects and clears the space
  • Heap memory follows into three areas:
    • Permanent Generation :
      •  PermGen space is to store classes, meta information  that are required by JVM
    • Young Generation : young Gen Memory holds all new objects.
      • Aged objects will be move to Old generation and dead objects will be removed very quickly.
      • Minor Garbage Collection will be invoked if youngen fills up
    • Old Generation  :   Old Generation stores long run objects.
      • Major Garbage Collection will be invoked to removed the unused objects

Heap Memory Settings:

is there any bench mark to set heap memory for application? We will discuss this. Assume you VM of 8 GB RAM and memory will be calculation follows as:

 

VM Size  (8GB)  (16 GB)
Tomcat Max Heap Size (Xms) & (Xmx) 4096 8144
Perm Gen 1024 1024
Code Cache 56 56
Other Mem (Assume 5% of JVM) 258.8 461.2
Total JVM (without threads) 5435 9685.2
Thread Memory
Space for thread with 1 MB stack 1119 3422
Space for thead with  512 KB stack 2238 6844
Space for theads with 256 KB  stack 4475 13688

 

 

Suggested Memory Settings are:

VM –    8 GB RAM VM –   16 GB RAM Remarks
-Xms4096m -Xms8144m 50% of RAM – Initial Heap Memory
-Xmx4096m -Xmx8144m 50% of RAM – Max Heap
-XX:NewSize=1024m -XX:NewSize=1024m  Initial Young Gen Memory
-XX:MaxNewSize=1024m -XX:MaxNewSize=1024m  Max Young Gen memory
-XX:PermSize=256m -XX:PermSize=256m  Initial PermGen memory
-XX:MaxPermSize=512m -XX:MaxPermSize=512m  Max PermGen Memory
-XX:NewRatio=2 -XX:NewRatio=2  NewRatio is the ratio of old generation to young generation. By default it is 2.
-XX:SurvivorRatio=4 -XX:SurvivorRatio=4  controls the size of the two survivor spaces
-XX:TargetSurvivorRatio=90 -XX:TargetSurvivorRatio=90
-XX:MaxTenuringThreshold=0 -XX:MaxTenuringThreshold=0
-XX:+UseParNewGC -XX:+UseParNewGC  Makes Garbage collection in Parallel using multiple CPU’s. Hence, this Increases application through put and  also decreases garbage collection overhead
-XX:ParallelGCThreads=4 -XX:ParallelGCThreads=4
-XX:+UseConcMarkSweepGC -XX:+UseConcMarkSweepGC  Concurrent Mark Sweep Collector –  It is good for Long lived objects such as Liferay
-XX:+CMSParallelRemarkEnabled -XX:+CMSParallelRemarkEnabled  CMS(Concurrent Mark Sweep Collector –  enables multple threads to do garbage collection
-XX:+CMSCompactWhenClearAllSoftRefs -XX:+CMSCompactWhenClearAllSoftRefs
-XX:CMSInitiatingOccupancyFraction=85 -XX:CMSInitiatingOccupancyFraction=85
-XX:+CMSScavengeBeforeRemark -XX:+CMSScavengeBeforeRemark
-XX:+CMSConcurrentMTEnabled -XX:+CMSConcurrentMTEnabled
-XX:ParallelCMSThreads=2 -XX:ParallelCMSThreads=2

 

Standard setenv.sh settings for 8GM RAM:

CATALINA_OPTS="$CATALINA_OPTS -Dfile.encoding=UTF8 -Djava.net.preferIPv4Stack=true 
-Dorg.apache.catalina.loader.WebappClassLoader.ENABLE_CLEAR_REFERENCES=false 
-Duser.timezone=GMT -XX:NewSize=1024m -XX:MaxNewSize=1024m -Xms4096m -Xmx4096m -XX:PermSize=256m -XX:MaxPermSize=512m 
-XX:NewRatio=2 -XX:SurvivorRatio=4 -XX:TargetSurvivorRatio=90 -XX:MaxTenuringThreshold=0 -XX:+UseParNewGC 
-XX:ParallelGCThreads=4 -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+CMSCompactWhenClearAllSoftRefs
-XX:CMSInitiatingOccupancyFraction=85 -XX:+CMSScavengeBeforeRemark -XX:+CMSConcurrentMTEnabled -XX:ParallelCMSThreads=2 
-XX:+UseLargePages -XX:+UseCompressedOops -XX:+DisableExplicitGC -XX:+UseBiasedLocking -XX:+BindGCTaskThreadsToCPUs
-XX:+UseFastAccessorMethods -XX:+CMSClassUnloadingEnabled -Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false 
-Djava.rmi.server.hostname=172.27.80.15 -Dcom.sun.management.jmxremote.port=18080 -Djava.library.path=/usr/local/apr/lib 
-Djava.security.manager -Djava.security.policy=$CATALINA_HOME/conf/catalina.policy"

 

How to analyze memory settings:

  1. jconsole
    • In command/terminal enter jconsole and then select org.apache.catalina.bootstrap for tomcatJConsole Local Process
    • Heap OverviewJVM Heap Overview
  2. Java Visualvm
    • in the command prompt : enter “jvisualvm”
    • Go to Tools -> Plugins – > Available plugins -> MarkVisual GC and Visual VM JConsole  and then install
    • Click on Running application: tomcat
    • Go to Visual GC tab to see the memoryVisual GC
    • Select Sampler and click on Memory

 

Leave a Reply

Your email address will not be published. Required fields are marked *