Marking and deleting objects for garbage collection in java - Mark and sweep algorithm




Contents of page >
  • A) JVM follows mark and sweep algorithm for garbage collection.
  • B) What is Automatic Garbage Collection in JVM heap memory?
  • C) Which objects are not in use in java heap memory?
  • D) Now, let’s understand how garbage collection is done using Marking and deletion of objects.
    • Step 1 > Marking objects
    • Step 2 > Deletion of objects
      • Step 2a > Normal Deletion
      • Step 2b > Deletion with Compacting
    • How Deletion with Compacting is faster/better performance than Normal Deletion?


A) JVM follows mark and sweep algorithm for garbage collection.
Mark and sweep algorithm internal working in 3 steps >  
STEP 1 > Unreferenced objects (garbage) are not reclaimed immediately.
  • Instead, garbage(unreferenced objects) is gathered until memory is not full.
STEP 2 >When memory becomes full >
  • execution of the program is suspended temporarily,
  • mark and sweep algorithm collects all the garbage (i.e. all unreferenced objects are reclaimed )
STEP 3 >Once garbage is collected >
  • execution of the program is resumed.


Before learning how garbage collection is done using Marking and deletion of objects. You must know in short what is What is Automatic Garbage Collection in JVM heap memory? And How to Identify objects which are in use in java heap memory?


B) What is Automatic Garbage Collection in JVM heap memory?
Automatic garbage collection is the process of
  • Identifying objects which are in use in java heap memory and
  • Which objects are not in use in java heap memory and
  • deleting the unused objects in java heap memory.

How to Identify objects which are in use in java heap memory?
Objects in use (or referenced objects) are those objects which is still needed by java program, some part of java program is still pointing to that object.

C) Which objects are not in use in java heap memory?
Objects not in use (or unreferenced objects or unused objects) are those objects which is not needed by java program, no part of java program is pointing to that object.
So, these unused objects can be cleaned in garbage collection process and memory used by an unreferenced object can be reclaimed.


D) Now, let’s understand how garbage collection is done using Marking and deletion of objects.

Step 1 > Marking objects
Marking is a process in which garbage collector identifies which parts of memory (occupied by objects) are in use and which are not.

Before Marking >
All the objects are shown in blue, at this stage
  • some of objects might be in use (referenced objects)  and
  • some of objects might not be in use (unreferenced objects) .

After Marking >
Objects in use (or referenced objects or Alive objects) are shown in blue.
Objects not in use (or unreferenced objects) objects are shown in Orange.


Step 2 > Deletion of objects
Step 2a > Normal Deletion
  • Normal deletion removes all the unreferenced objects and
  • leaves referenced objects and pointers to free space.
Memory Allocator holds a list of references to free spaces and searches for free space whenever an allocation is required.


Step 2b > Deletion with Compacting
Deletion with Compacting is done to improve the performance than normal deleting.

  • Deletion with Compacting removes all the unreferenced objects and
  • compacts the remaining referenced objects by moving all the referenced objects together.
  • As all the referenced objects are moved together new memory allocation becomes easier and much faster.

Memory Allocator holds the reference to the beginning of free space.



How Deletion with Compacting is faster/better performance than Normal Deletion?
In terms of Memory/space occupied -
In normal deletion Memory Allocator holds a list of references to free spaces but
in Deletion with Compacting Memory Allocator holds the reference to the beginning of free space. So, space occupied in normal deletion is less because Deletion with Compacting holds single reference not list like normal deletion.

In terms of time -
In normal deletion Memory Allocator searches for free space whenever an allocation of new object is required.
But in Deletion with Compacting Memory Allocator directly allocates the new object at holded reference of free space.



Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>



JVM in detail - Garbage collection & heap structure >

>JVM Heap memory (Hotspot heap structure) with diagram in java


eEdit
Must read for you :