Heap Generations for
Garbage Collection in Java
Java objects are
created in Heap.
Heap is divided into three parts or generations
for the sake of garbage collection in Java,
- Young generation
- Tenured or Old Generation
- Perm Area of the heap.
New objects are
created by young generation and subsequently moved to the old generation.
New Generation is
further divided into three parts known as
- Eden space
- Survivor 1
- Survivor 2 space.
When an object
first created in heap its gets created in new generation inside Eden space and
after subsequent minor garbage collection if an object survives its gets moved
to survivor 1 and then survivor 2 before major garbage collection moved that
object to old or tenured generation.
.tmp)
Whenever Major
garbage collection occurs application threads stop during that period which will
reduce application’s performance and throughput.
It's all automatic
as you cannot force garbage collection in Java
Important Points about JVM
Options:
- Boolean JVM options can
be turned on with
-XX:+ and can
be turned off with
-XX:-
- Numeric JVM Options can be set
with -XX:= Numbers can include 'm' or 'M'
for megabytes, 'k' or 'K' for kilobytes, and 'g' or 'G' for gigabytes (for
example, 32k is the same as 32768).
- String JVM options can
be set by using -XX:= and usually used to
specify a file, a path, or a list of commands.
.tmp)
.tmp)
GC startup args
- -Xms8192m -Xmx8192m
- -XX:PermSize=256m
-XX:MaxPermSize=768m
- -XX:+UseG1GC
- -XX:MaxGCPauseMillis=200
- -verbose:class|gc|jni
- -XX:+PrintGCDetails
-XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps
- -Xloggc:/app/oracle/osbdomain/servers/101_osb1/logs/101_osb1_gc.log
- -XX:+UseGCLogFileRotation
- -XX:NumberOfGCLogFiles=10
- -XX:GCLogFileSize=1000K
Create verbose
outputs by using the command-line option -Xverbose;
Ex:
“-Xverbose:memdbg,gcpause,gcreport” will show memory management data like
garbage collection frequency and duration, “-Xverbose:memdbg” will also show the
reason why each garbage collection was started. This will help you study the
garbage collection behavior.
- -Xverbose:memory,gc,gcreport,gcpause,memdbg
- -Xverbosetimestamp
- -Xverboselog:/app/oracle/osbdomain/servers/a0001_osb1/logs/a0001_osb1_gc.log
----------------
- -XX:+HeapDumpOnOutOfMemoryError
- -XX:HeapDumpPath=/app/oracle/osbdomain/servers/001_osb1/logs/hprof-dumps
-----------------
- -Xns2048m
- -Xgc:genconpar
- -XXkeepAreaRatio:25
- -XpauseTarget=200ms
- -XXgcThreads:8
- -Dweblogic.SelfTuningThreadPoolSizeMin=100
- -Dweblogic.SelfTuningThreadPoolSizeMax=200
- -Dcom.bea.wli.sb.pipeline.RouterRuntimeCache.size=500
JFR startup
args
- -XX:+UnlockCommercialFeatures
- -XX:+FlightRecorder
- -XX:StartFlightRecording=duration=0s,filename=/app/oracle/osbdomain/servers/101_osb1/logs/101_osb1.jfr,maxage=300s,maxsize=10M
Types of Garbage Collector
in Java
Java Runtime (J2SE
5) provides various types of GC’s in Java which you can choose based on your
application's performance requirement. Java 5 adds three additional garbage
collectors except serial garbage collector. Each is generational garbage
collector which has been implemented to increase the throughput of the
application or to reduce garbage collection pause times.
Throughput Garbage Collector:
- This garbage collector in Java
uses a parallel version of the young generation collector.
- It is used if
the -XX:+UseParallelGC option is passed to the runtime
via JVM command line
options .
- The tenured generation collector
is same as the serial collector
Concurrent low pause/ Concurrent Mark Sweep
Garbage collector:
- This Collector is used if the
-Xingc or -XX:+UseConcMarkSweepGC is passed on the command line.
- The concurrent collector is used
to collect the tenured generation and does most of the collection concurrently
with the execution of the application.
- The application
is paused for short
periods during the collection.
- A parallel version of the young
generation copying collector is used with the concurrent collector.
- This is most widely used garbage
collector in java and it uses an algorithm to first mark object which needs to
collect when garbage collection triggers.
The Incremental (Sometimes called train) low
pause collector:
- This collector is used only if
-XX:+UseTrainGC is passed on the command line.
- This garbage collector has not
changed since the java 1.4.2 and is currently not under active development.
- It will not be supported in
future releases so avoid using this and please see 1.4.2 GC Tuning document
for information on this collector.
- It’s not recommended to use this
garbage collector in java.
An important point to note is
that:
-XX:+UseParallelGC should
not be used with -XX:+UseConcMarkSweepGC
-XX:NewRatio=3 means
that the ratio of the young and tenured generation is 1:3 , you got to be
careful on sizing this generation.
1) The main
difference between heap and stack
- Stack memory is used to store
local variables and function call
- Stack which can be specified
using -Xss JVM parameter
- If there is no memory left in
the stack
for storing
function call or local variable, JVM will throw java.lang.StackOverFlowError
- Heap memory is used to store
objects in Java. No matter, where the object is created in code e.g. as a
member variable, local variable or class variable, they are always created
inside heap space in Java.
- Heap size of Java program using
JVM option -Xms and -Xmx
-Xms
is starting size of the heap and
-Xmx
is a maximum size of java heap
.tmp)
- If there is no more heap
space for creating
an object, JVM will throw java.lang.OutOfMemoryError: Java Heap Space.
Types:
1)
The java.lang.OutOfMemoryError: Java heap
space
2)
The java.lang.OutOfMemoryError: PermGen
space
Permanent generation of the heap is used to
store String pool and various Metadata required by JVM related to Class, method
and other java primitives.
- Another reason is if any thread
started by the application doesn't exit when you undeploy your
application.
- So the conclusion is to avoid
using "-Xnoclassgc" in the J2EE environment especially with AppServer.
- An important point to remember
is that it doesn't depend on –Xmx value so no matter how big your
total heap size you can run OutOfMemory in perm space.
- The good thing is you can
specify the size of permanent generation using JVM options "-XX: PermSize"
and "-XX: MaxPermSize" based on
your project need.
1) Visualgc -
Visualgc stands for Visual Garbage Collection Monitoring Tool and you can attach
it to your instrumented hotspot JVM
Garbage collection is a
mechanism provided by JVM to reclaim heap space from
objects which are eligible for Garbage collection
- Garbage Collection in Java is
carried by a daemon thread called Garbage Collector.
- Methods like System.gc()
and Runtime.gc()
which is used to send request of GC to JVM but it’s not guaranteed that GC
will happen.
Garbage collection
works by employing several GC algorithm
There are
different kinds of garbage collector available in Java to collect different area
of heap memory
- Serial
- Parallel
- Concurrent garbage collector in
Java.
JVM provides
memory management, developers only care about creating an object and they don't
care about cleaning up
- That is done by the garbage
collector, but it can only collect objects which have no live strong reference
or it's not reachable from any thread.
- If an object, which is supposed
to be collected but still lives in memory due to unintentional strong
reference then it's known as a memory leak in Java. ThreadLocal variables
in Java web application can easily cause the memory leak.