Thursday, November 12, 2009

Java Garbage Collector

It is known that you can allocate memory to an object or an array using the ' new ' operator.You cannot however release the memory explicitly through your code.Garbage Collection is the process that automatically frees the memory of objects that are no longer in use.There is no specification of a technique for garbage collection and the implementation has been left to the vendors.

When a programmer stops referencing an object ,the object is not required any more and becomes garbage.The space that is used by the object can be released and used for another objects.In native languages like C and C++ you can explicitly release the memory allocated to objects and data members.Since the native languages are so memory vulnerable that if programmer forgot to release the unused memory then he may run into all sorts of trouble.

Java programming language was first of its kind which has the capability of automatic garbage collection.Garbage collection frees a programmer from the burden of freeing the memory time and time again.The garbage collector itself decides the objects that are of no use or no more referenced in the program and release the memory allocated to them.

Garbage collection is one of the coolest feature Java has but the so called garbage collector has a huge problem that it does memory fragmentation.That's right he is not wise enough to make the decision to make memory the contiguous block of free addresses.What Garbage collector does is it sweeps all the unreferenced objects and makes them free but it leads to memory fragmentation in which there are space available between the chunks of data or vice verse.This memory fragmentation thing to an extent slows down the things a bit due to extra switching overhead.This burden counts to nothing if the application is small but if the application is huge then there is every possibility that your application being slow(Read about Java architecture Trade offs).

NOTE:-You can not enforce Garbage collector to run whenever you want .Rather it will run whenever it wants to.So each time you make a call to Garbage collector (System.gc();) doesn't mean that you actually deallocated the memory from unused objects.It is like a Thread in execution when you are not certain about the output or steps to output.

Suggested Reading:-

JAVA Sandbox
JAVA Architecture
Memory Management in JAVA
JAVA Class loader Architecture
How JVM Handles Strings and Literals

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP