garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. An in use object, or a referenced object, means that some part of your program still maintains a pointer to that object. An unused object, or unreferenced object, is no longer referenced by any part of your program. So the memory used by an unreferenced object can be reclaimed.
In a programming language like C, allocating and deallocating memory is a manual process. In Java, process of deallocating memory is handled automatically by the garbage collector.
This actually varies depending on the JVM implementation, but I’m assuming you’re talking about Oracle (Sun) Version 6. Java itself does not specify a particular method of garbage collection.
The JVM uses a form of garbage collector called a tracing collector, which essentially operates by first stopping the world, marking all root objects, or objects that are referenced directly by running threads, and following references, marking each object it hits along the way.
Java 6 implements something called a generational garbage collector—based upon the generational hypothesis assumption, which states that the majority of objects that are created are quickly discarded, and that objects that are not quickly collected are likely to be around for some time.
Based upon these assumptions, Java therefore partitions objects into two different generations, and then operates differently upon them.
finalize()method of that object and gives an opportunity to perform any sort of cleanup required.
Runtime.gc()which is used to send request of Garbage collection to JVM but it’s not guaranteed that garbage collection will happen.
An object becomes eligible for Garbage collection or GC if its not reachable from any live threads or by any static references. In other words you can say that an object becomes eligible for garbage collection if its all references are null. Cyclic dependencies are not counted as reference so if object A has reference of object B and object B has reference of Object A and they don’t have any other live reference then both Objects A and B will be eligible for Garbage collection.
Young Generation: This is where objects start out. It has two subgenerations: Eden - Objects start out here. Survivor - Objects that survive Eden end up here. There are two of these, and only one is in use at any given time. One is designated as empty, and the other as live. This switched every GC cycle. Tenured Generation: Older objects with longer lifetimes end up here. Java is smart enough to apply different garbage collection methods to each generation. The young generation is handled using a tracing, copying collector called the Parallel New Collector. This collector stops the world, but because the young generation is generally small, the pause is short.