Java (JVM) Memory Model and Garbage Collection Monitoring Tuning PDF
Java (JVM) Memory Model and Garbage Collection Monitoring Tuning PDF
Java (JVM) Memory Model and Garbage Collection Monitoring Tuning PDF
As you can see in the above image, JVM memory is divided into separate parts. At broad level, JVM Heap
memory is physically divided into two parts Young Generation and Old Generation.
Young Generation
Young generation is the place where all the new objects are created. When young generation is filled, garbage
collection is performed. This garbage collection is called Minor GC. Young Generation is divided into three
parts Eden Memory and two Survivor Memory spaces.
Important Points about Young Generation Spaces:
Most of the newly created objects are located in the Eden memory space.
When Eden space is filled with objects, Minor GC is performed and all the survivor objects are moved to
one of the survivor spaces.
Minor GC also checks the survivor objects and move them to the other survivor space. So at a time, one of
the survivor space is always empty.
Objects that are survived after many cycles of GC, are moved to the Old generation memory space. Usually
its done by setting a threshold for the age of the young generation objects before they become eligible to
promote to Old generation.
Old Generation
Old Generation memory contains the objects that are long lived and survived after many rounds of Minor GC.
Usually garbage collection is performed in Old Generation memory when its full. Old Generation Garbage
Collection is called Major GC and usually takes longer time.
Permanent Generation
Permanent Generation or Perm Gen contains the application metadata required by the JVM to describe the
classes and methods used in the application. Note that Perm Gen is not part of Java Heap memory.
Perm Gen is populated by JVM at runtime based on the classes used by the application. Perm Gen also contains
Java SE library classes and methods. Perm Gen objects are garbage collected in a full garbage collection.
Method Area
Method Area is part of space in the Perm Gen and used to store class structure (runtime constants and static
variables) and code for methods and constructors.
Memory Pool
Memory Pools are created by JVM memory managers to create a pool of immutable objects, if implementation
supports it. String Pool is a good example of this kind of memory pool. Memory Pool can belong to Heap or
Perm Gen, depending on the JVM memory manager implementation.
VM Switch Description
-Xms
-Xmx
-Xmn
For setting the size of the Young Generation, rest of the space goes for Old Generation.
-XX:PermGen
Most of the times, above options are sufficient, but if you want to check out other options too then please
check JVM Options Official Page.
jsat
We can use jstat command line tool to monitor the JVM memory and garbage collection activities. It ships with standard JDK, so you dont need to do anything else
to get it.
For executing jstat you need to know the process id of the application, you can get it easily using ps
1
2
3
command.
So the process id for my java application is 9582. Now we can run jstat command as shown below.
1
2
3
4
5
6
7
8
9
EU
7933.3
8026.5
8030.0
8122.2
8171.2
106.7
145.8
OC
42108.0
42108.0
42108.0
42108.0
42108.0
42108.0
42108.0
OU
23401.3
23401.3
23401.3
23401.3
23401.3
23401.3
23401.3
PC
20480.0
20480.0
20480.0
20480.0
20480.0
20480.0
20480.0
PU
YGC
19990.9
19990.9
19990.9
19990.9
19990.9
19990.9
19990.9
157
157
157
157
157
158
158
YGCT
FGC
0.274 40
0.274 40
0.274 40
0.274 40
0.274 40
0.275 40
0.275 40
FGCT
GCT
1.381
1.654
1.381
1.654
1.381
1.654
1.381
1.654
1.381
1.654
1.381
1.656
1.381
1.656
The last argument for jstat is the time interval between each output, so it will print memory and garbage collection data every 1 second.
S0C and S1C: This column shows the current size of the Survivor0 and Survivor1 areas in KB.
S0U and S1U: This column shows the current usage of the Survivor0 and Survivor1 areas in KB. Notice that one of the survivor areas are empty all the time.
EC and EU: These columns show the current size and usage of Eden space in KB. Note that EU size is increasing and as soon as it crosses the EC, Minor GC is
called and EU size is decreased.
OC and OU: These columns show the current size and current usage of Old generation in KB.
PC and PU: These columns show the current size and current usage of Perm Gen in KB.
YGC and YGCT: YGC column displays the number of GC event occurred in young generation. YGCT column displays the accumulated time for GC operations
for Young generation. Notice that both of them are increased in the same row where EU value is dropped because of minor GC.
FGC and FGCT: FGC column displays the number of Full GC event occurred. FGCT column displays the accumulated time for Full GC operations. Notice that
Full GC time is too high when compared to young generation GC timings.
GCT: This column displays the total accumulated time for GC operations. Notice that its sum of YGCT and FGCT column values.
The advantage of jstat is that it can be executed in remote servers too where we dont have GUI. Notice that sum of S0C, S1C and EC is 10m as specified through Xmn10m
JVM option.
After installing Visual GC, just open the application from the left side column and head over to Visual
GCsection. You will get an image of JVM memory and garbage collection details as shown in below image.
http://www.journaldev.com/2856/java-jvm-memory-model-and-garbage-collection-monitoring-tuning
PermGen space
Gen memory space using -XX:PermGen and -XX:MaxPermGen JVM options. You might also try using XX:+CMSClassUnloadingEnabled
If you are see a lot of Full GC operations, then you should try increasing Old generation memory space.
Overall garbage collection tuning takes a lot of effort and time and there is no hard and fast rule for that. You
would need to try different options and compare them to find out the best one suitable for your application.
Thats all for Java Memory Model and Garbage Collection, I hope it helps you in understanding JVM memory
and garbage collection process.