Garbage Collection- My Notes
Garbage Collection- My Notes
Q 3) finalize() method ?
The finalize() method is invoked each time before the
object is garbage collected. This method can be used to
perform cleanup processing. This method is defined in
Object class as:
protected void finalize(){}
Q 4) gc() method ?
We cannot force Garbage collection.
gc() method gives request to garbage collector to
perform cleanup processing
The gc() is found in System and Runtime classes.
public static void gc(){}
`
1) Serial Garbage Collector
Serial garbage collector works by holding all the
application threads. It is designed for the single-threaded
environments. It uses just a single thread for garbage
collection. The way it works by freezing all the
application threads while doing garbage collection may
not be suitable for a server environment. It is best suited
for simple command-line programs.
Turn on the -XX:+UseSerialGC JVM argument to use the
serial garbage collector.
2) Parallel Garbage Collector
Parallel garbage collector is also called as throughput
collector. It is the default garbage collector of the JVM.
Unlike serial garbage collector, this uses multiple threads
for garbage collection. Similar to serial garbage collector
this also freezes all the application threads while
performing garbage collection.
4) G1 Garbage Collector
G1 (Garbage First) Garbage Collector is designed for
applications running on multi-processor machines with
large memory space. It’s available since JDK7 Update 4
and in later releases.
G1 collector will replace the CMS collector since its more
performance efficient.
Unlike other collectors, G1 collector partitions the heap
into a set of equal-sized heap regions, each a contiguous
range of virtual memory. When performing garbage
collections, G1 shows a concurrent global marking phase
(i.e. phase 1 known as Marking) to determine the
liveness of objects throughout the heap.
After the mark phase is completed, G1 knows which
regions are mostly empty. It collects in these areas first,
which usually yields a significant amount of free space
(i.e. phase 2 known as Sweeping). It is why this method
of garbage collection is called Garbage-First.
To enable G1 Garbage Collector, we can use the
following argument:
java -XX:+UseG1GC -jar Application.java
Q 12) One important change in Memory Management
in Java 8
Oracle’s latest edition for Java – Java 8 was released in
March 2014. As usual, tons of new features have been
added. There is one major change in the Memory
management area
So long PermGen, Hello Metaspace !!”
What is PermGen ?
Short form for Permanent Generation, PermGen is the
memory area in Heap that is used by the JVM to store
class and method objects. If your application loads lots
of classes, PermGen utilization will be high. PermGen
also holds ‘interned’ Strings
The size of the PermGen space is configured by the
Java command line option -XX:MaxPermSize
Typically 256 MB should be more than enough of
PermGen space for most of the applications
However, It is not unusal to see the error
“java.lang.OutOfMemoryError: PermGen space“ if you
are loading unusual number of classes.
Gone are the days of OutOfMemory Errors due to
PermGen space.
With Java 8, there is NO PermGen. That’s right. So no
more OutOfMemory Errors due to PermGen
The key difference between PermGen and Metaspace is
this: while PermGen is part of Java Heap (Maximum size
configured by -Xmx option), Metaspace is NOT part of
Heap. Rather Metaspace is part of Native Memory
(process memory) which is only limited by the Host
Operating System.