how tha garbage collector know that the object will be
deleted? on which algorithm the garbage collector works?
what is the working principle of garbage collector? How
manay types of garbage collectors r there?



how tha garbage collector know that the object will be deleted? on which algorithm the garbage col..

Answer / abul kalam

An object is eligible for garbage collection when it
determines no more references to the object exists.


Algorithms that are used for garbage collection.

1 – Reference Counting:

Each object has an associated reference count. This count
indicates the number of active references to that object.
If this count is zero, it is garbage and can be recycled.
Whenever the reference is modified, the count is updated.
Once this count is zero, the memory is reclaimed.

2 – Tracing Collectors:

Mostly the standard garbage collectors do not use Reference
Counting. They will use some form of tracing collector’s
algorithms. This algorithm will trace all objects starting
from root until all reachable objects have been examined.

3 – Mark-Sweep collectors:

This is most basic form of collector algorithm. In this
case the collector visits each node starting from root and
marks each node. Once there are no any references, the
collection is complete. The heap is swept and the objects
not marked are reclaimed and returned to free list.

4 – Copying Collectors:

In this case, the heap is divided into equally sized semi
spaces. One with active data and another with unused. Once
the active space fills up, the objects are copied from
active to unused space and the roles are flipped becoming
unused space as active. This has advantages as it examines
only active data. But will have a overhead of copying data
from active to unused space.

5 – Heap Compaction:

In the copying collectors, the set of live objects can be
compacted at the bottom of heap. This improves locality of
reference and eliminates heap fragmentation and greatly
reduces the cost of object allocation which eliminates the
need to maintain free lists or look-aside lists or perform
best-fit or first-fit algorithms and allocating N bytes is
simple to add N to heal pointer.

Is This Answer Correct ?    5 Yes 0 No

Post New Answer

More Core Java Interview Questions

What are Font and FontMetrics classes?

1 Answers  


what is the difference between Cpp And Java

15 Answers   Infosys,


10. What is the output of the following Java program? class Main { public static void main(String args[]){ final int i; i = 10; System.out.println(i); } } 10. What is the output of the following Java program? class Main { public static void main(String args[]){ final int i; i = 10; System.out.println(i); } }

0 Answers  


Is it possible for a yielded thread to get chance for its execution again?

0 Answers  


How will you calculate the depth of a binary tree if the tree contains 15 nodes?

0 Answers   Aricent,


What are the two parts of a conditional statement?

0 Answers  


WHAT IS JDK,JVM,CLASS DEFINE ALL?

2 Answers  


What is super in java?

0 Answers  


What are the 3 types of control structures?

0 Answers  


Can you use abstract and final both with a method?

0 Answers  


What is this keyword used for?

0 Answers  


When should we create our own custom exception classes?

0 Answers  


Categories