Follow by Email

Monday, 15 October 2012

Garbage Collection in Java (Mark and Sweep Algorithm)

Garbage ,in case of Computer Science refers to data , object or any other part of Memory that will not be used in any further program.
Normally memory has much space according to programmer's requiremet but sometimes when there is a lot of unused but non-empty space in memory area, it results in slow processing

Garbage Collection-

Garbage Collection is a form of Memory Management, that is job of system.
Thats why it is called Automated Memory Management.
Garbage Collector try to free memory space occupied by programs and data object that are no longer accessed by any Program or by system communication.

In java-

In Java,Garbage collector runs automatically in the lifetime of a java program, It frees the allocated memory that is not used by running process.
In C language and other old languages , it is the responsibility of coder(Programmer) to free memory time to time using free(); function
You can manually run garbage collector in java, there is a method in System class named void gc();
It suggests JVM(java virtual machine) to collect currently unused memory.
System.gc(); is logically equal to Runtime.getRuntime.gc();

Mark and Sweep-

There are two algotithm for garbage collection- Mark and Sweep also known as Marking and Collection phase of garbage collector.
When all available memory has been exhausted and execution of the program is suspended for a while then mark-and-sweep algorithm marks and collects all the garbage values and memory space.
Once all garbage is marked then it is collected and process resumes .
This is the process of Garbage Collection in java, and same for many other Object Oriented languages

No comments :

Post a Comment