Java Memory Management

Nandita Hans
5 min readDec 13, 2020

Java uses an automatic memory management system called a garbage collector.

Whereas in other programming languages such as C the programmer has direct access to the memory who allocates memory in his code, thereby creating a lot of scope for leaks

Why programmer should know about memory management?

  • To make high performance based programs.
  • If we don’t know how the memory management works, we end up making objects that aren’t eligible for the automatic garbage collection.

Java memory management divides into two major parts:

  • JVM Memory Structure
  • Working of the Garbage Collector

JVM MEMORY STRUCTURE

JVM creates many run time data areas in the heap which is used during the execution of the program

  • Memory areas are destroyed when the JVM exits
  • Data areas are destroyed when the Thread exits

METHOD AREA

  • It is used to allocate memory to
  • class structures
  • method data
  • constructor field data
  • interfaces or special method used in class
  • It is the logical part of the heap
  • It is not compulsory to be garbage collected even if the garbage collection is compulsory in the heap

HEAP

  • Stores the actual object
  • Can be fixed or dynamic in size.
  • When we use a new keyword, the JVM creates an instance for the object in a heap.
  • Reference of the object created from the new keyword is stored in the stack
  • There is only one heap for each and every running process
  • When the size of heap becomes full, the garbage is collected

For eg:

StringBuilder str=new StrngBuilder();

The object is stored in the heap and the refrence of the object “str” is stored in the stack

JVM memory is divided into separate parts. At broad level, JVM Heap memory divided into two parts — Young Generation and Old Generation.

Young Generation

Young Generation is divided into three parts — Eden Memory and two Survivor Memory 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.

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 it’s full. Old Generation Garbage Collection is called Major GC and usually takes a longer time.

Garbage collection in heap area is mandatory.

REFERENCE TYPE

There are four types of references:

  1. Strong Reference: Any object having a strong reference is not eligible for garbage collection

For eg: StringBuilder sb= new StringBuilder();

2. Weak Reference: If we are not sure if the object will be required again or not we can apply garbage collection on the object by using a weak reference. It destroys the object. An object can be referenced as weak Reference in the following ways.

For eg: WeakReference<StringBuilder> reference = new WeakReference<>(new StringBuilder());

3. Soft Reference: These objects are collected when the application is running low on memory. All the soft reference object are collected before JVM throws are OutOfMemoryError

For eg: SoftReference<StringBuilder> reference = new SoftReference<>(new StringBuilder());

4. Phantom Reference: The object with phantom reference can be collected whenever the garbage collector wants to run. . We can create phantom reference by using the following statement:

For eg:PhantomReference<StringBuilder> reference = new PhantomReference<>(new StringBuilder())

STACK AREA

  • It contains references to the heap object and also holds value to that object
  • Can be fixed or dynamic in size
  • Stack generates when thread creates stack memory is allocated per thread
  • It is used to store partial results and data
  • Stack Frame
  • Used to store thread Data
  • Components of Frame
  1. Local Variable Array (LVA)
  2. Operand Stack (OS)
  3. Frame Data (FD)
  • Only one frame is active at a point

1.Frame-Current Frame

2.Method: Current Method

3.Class:Current Class

WORKING OF GARBAGE COLLECTOR

  • All the garbage collection makes sure that the heap has as much free space as possible.
  • The function of the garbage collector is to find and delete the objects that cannot be reached.
  • JVM controls the garbage collector.
  • JVM decides when to perform the garbage collection.
Note: System.gc() and Runtime.gc() are the methods which requests for Garbage collection to JVM explicitly but it doesn’t ensures garbage collection as the final decision of garbage collection is of JVM only.

When garbage collection of an object occurs?

  • object is eligible for garbage collection when no live thread can access it
  • The garbage collector considers that object as eligible for deletion. If a program has a reference variable that refers to an object, that reference variable available to live thread, this object is called reachable.

Can a Java application run out of memory?

Yes,if we have many live objects, garbage collection does not ensures that there is enough memory. Only available memory will be managed effectively.

Types of Garbage Collection

There are five types of garbage collection are as follows:

  • Serial GC: uses the mark and sweeps approach for young and old generation
  • Parallel GC: It spawns N (the number of CPU cores in the system) threads for young generation garbage collection.
  • Parallel Old GC: It uses multiple threads for both generations.
  • Concurrent Mark Sweep (CMS) Collector: It does the garbage collection for the old generation. You can limit the number of threads in CMS collector using XX:ParalleCMSThreads=JVM option. It is also known as Concurrent Low Pause Collector.
  • G1 Garbage Collector: It introduced in Java 7. Its objective is to replace the CMS collector. It is a parallel, concurrent, and CMS collector. There is no young and old generation space. It divides the heap into several equal sized heaps. It first collects the regions with lesser live data.

--

--