1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc)

Java Tutorials

Q.What are the Garbage collection algorithms in Java?

Any garbage collection algorithm must do two basic things.
First, it must detect garbage objects.
Second, it must reclaim the heap space used by the garbage objects and make it available to the program.
Garbage detection is ordinarily accomplished by defining a set of roots and determining reachability from the roots.
An object is reachable if there is some path of references from the roots by which the executing program can access the object.
The roots are always accessible to the program. Any objects that are reachable from the roots are considered live.
Objects that are not reachable are considered garbage, because they can no longer affect the future course of program execution.

    a. Reference counting collectors
Reference counting was an early garbage collection strategy.
here a reference count is maintained for each object.
When an object is first created its reference count is set to one.
When any other object or root is assigned a reference to that object, the object's count is incremented.
When a reference to an object goes out of scope or is assigned a new value, the object's count is decremented.
Any object with a reference count of zero can be garbage collected.
When an object is garbage collected, any objects that it refers to has their reference counts decremented.
In this way the garbage collection of one object may lead to the subsequent garbage collection of other objects.

b. Tracing collectors
Tracing garbage collectors trace out the graph of object references starting with the root nodes.
Objects that are encountered during the trace are marked in some way.
Marking is generally done by either setting flags in the objects themselves or by setting flags in a separate bitmap.
After the trace is complete, unmarked objects are known to be unreachable and can be garbage collected.
The basic tracing algorithm is called mark and sweep.
This name refers to the two phases of the garbage collection process.
In the mark phase, the garbage collector traverses the tree of references and marks each object it encounters.
In the sweep phase unmarked objects are freed, and the resulting memory is made available to the executing program.
In the JVM the sweep phase must include finalization of objects.

    c. Compacting collectors
Garbage collectors of JVMs will likely have a strategy to combat heap fragmentation.
Two strategies commonly used by mark and sweep collectors are compacting and copying.
Both of these approaches move objects on the fly to reduce heap fragmentation.
Compacting collectors slide live objects over free memory space toward one end of the heap.
In the process the other end of the heap becomes one large contiguous free area.
All references to the moved objects are updated to refer to the new location.

    d. Copying collectors
Copying garbage collectors move all live objects to a new area.
As the objects are moved to the new area, they are placed side by side,
thus eliminating any free spaces that may have separated them in the old area.
The old area is then known to be all free space.
The advantage of this approach is that objects can be copied as they are discovered by the traversal from the root nodes.
There are no separate mark and sweep phases.
Objects are copied to the new area on the fly, and forwarding pointers are left in their old locations.
The forwarding pointers allow objects encountered later in the traversal that refer to already copied objects to know the new location of the copied objects.

Reviews and Comments


PMP, CAPM, PMI is a registered certification mark of the Project Management Institute, Inc

Copyright © www.techfaq360.com 2016


About US | Contact US | Privacy Policy | Terms and Conditions  | Website disclaimer  | Cancellation and Refund Policy  | Shipping & Delivery Policy