Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Java solve retain cycles in garbage collection?

I know that a retain cycle (at least in Objective-C and Swift) is when two objects claim ownership of one another (they have references to each other). And in Objective-C we can solve the issue by declaring one of them weak.

From what I have read and understood, the Java GC is not affected by retain cycles, and we do not have to worry about weak references. How does it solve it?

like image 584
Anders Avatar asked Jan 08 '14 11:01

Anders


People also ask

Does Java garbage collector handle cyclic references?

yes Java Garbage collector handles circular-reference!

How do you retain retaining cycle?

In order to prevent this retain cycle, we need to declare at least one of the variable as weak or unowned. We can break the retain cycle with adding a weak keyword before either the driver property of the Car class or the car property of the Person class.

How does garbage collection prevent a Java application from going out of memory?

Garbage Collection makes memory management in Java efficient as it removes unreferenced objects from the heap memory without the interference of the programmer. As Garbage collection is automatic and is a part of JVM, no extra efforts are needed from the programmer to reclaim memory or destruct objects.


3 Answers

The Java (JVM) garbage collector works by looking for "reachable" objects - from the root(s) of the object tree. If they can't be reached (if they have no outside object references) then entire object graphs can be discarded.

Essentially it just just traverses the tree from root(s) to leaf nodes and marks all objects it encounters. Any memory not taken up by marked objects in the heap is swept (marked as free). This is called mark and sweep. img src

Mark and sweep in action

This can't be done easily in objective-c because it uses reference counting, not mark and sweep which has it's flaws

The reason there can be no retain cycles is because if they aren't linked to the "tree" anywhere, they aren't marked and can be discarded.

like image 91
Ross Drew Avatar answered Oct 13 '22 01:10

Ross Drew


The garbage collector looks for reachable objects, starting from the roots (typically: variables on the call stack or global variables). So if two objects reference each other but are not otherwise reachable they won't be flagged as "live" and will be collected.

like image 27
assylias Avatar answered Oct 13 '22 00:10

assylias


As the name suggests, Garbage Collection refers to removing of objects which are no longer in use. It is a well known fact that irrespective of their scope objects, Java stores objects in heap. Thus, if we keep on creating objects without clearing the heap, our computers might run out of heap space and we get ‘Out of Memory’ error. Garbage Collection in Java is a mechanism which is controlled and executed by the Java Virtual Machine (JVM) to release the heap space occupied by the objects which are no more in use. In contrast to C++, garbage collection in java relives the developer from the Memory Management related activities. The JVM executes this process with the help of a demon thread called the ‘Garbage Collector’. The garbage collector thread first invokes the finalize method of the object. This performs the cleanup activity on the said object. As a developer we cannot force the JVM to run the garbage collector thread. Though there are methods e.g Runtime.gc () or System.gc(), but none of these assures the execution of garbage collector thread. These methods are used to send garbage collection requests to the JVM. It is up to the Java Virtual machine when it will initiate the garbage collection process.

Take a look at this stuff

How Garbage Collection works in Java

like image 1
Nidhish Krishnan Avatar answered Oct 12 '22 23:10

Nidhish Krishnan