Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java - How can Garbage Collector quickly know which objects do not have references to them any more?

Tags:

I understand that in Java, if an object doesn't have any references to it any more, the garbage collector will reclaim it back some time later.

But how does the garbage collector know that an object has or has not references associated to it?

Is garbage collector using some kind of hashmap or table?


Edit:

Please note that I am not asking how generally gc works. really, I am not asking that.

I am asking specifically that How gc knows which objects are live and which are dead, with efficiencies.

That's why I say in my question that is gc maintain some kind of hashmap or set, and consistently update the number of references an object has?

like image 721
Jackson Tale Avatar asked May 14 '12 17:05

Jackson Tale


People also ask

How does garbage collector know which objects to free Java?

When the garbage collector performs a collection, it releases the memory for objects that are no longer being used by the application. It determines which objects are no longer being used by examining the application's roots.

How does garbage collector remove unused references?

As long as an object is being referenced, the JVM considers it alive. Once an object is no longer referenced and therefore is not reachable by the application code, the garbage collector removes it and reclaims the unused memory.

How does the Java garbage collector work?

When Java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program. Eventually, some objects will no longer be needed. The garbage collector finds these unused objects and deletes them to free up memory.


2 Answers

A typical modern JVM uses several different types of garbage collectors.

One type that's often used for objects that have been around for a while is called Mark-and-Sweep. It basically involves starting from known "live" objects (the so-called garbage collection roots), following all chains of object references, and marking every reachable object as "live".

Once this is done, the sweep stage can reclaim those objects that haven't been marked as "live".

For this process to work, the JVM has to know the location in memory of every object reference. This is a necessary condition for a garbage collector to be precise (which Java's is).

like image 142
NPE Avatar answered Dec 28 '22 10:12

NPE


Java has a variety of different garbage collection strategies, but they all basically work by keeping track which objects are reachable from known active objects.

A great summary can be found in the article How Garbage Collection works in Java but for the real low-down, you should look at Tuning Garbage Collection with the 5.0 Java[tm] Virtual Machine

An object is considered garbage when it can no longer be reached from any pointer in the running program. The most straightforward garbage collection algorithms simply iterate over every reachable object. Any objects left over are then considered garbage. The time this approach takes is proportional to the number of live objects, which is prohibitive for large applications maintaining lots of live data.

Beginning with the J2SE Platform version 1.2, the virtual machine incorporated a number of different garbage collection algorithms that are combined using generational collection. While naive garbage collection examines every live object in the heap, generational collection exploits several empirically observed properties of most applications to avoid extra work.

The most important of these observed properties is infant mortality. ...

I.e. many objects like iterators only live for a very short time, so younger objects are more likely to be eligible for garbage collection than much older objects.

For more up to date tuning guides, take a look at:

  • Java SE 6 HotSpot[tm] Virtual Machine Garbage Collection Tuning
  • Java Platform, Standard Edition HotSpot Virtual Machine Garbage Collection Tuning Guide (Java SE 8)

Incidentally, be careful of trying to second guess your garbage collection strategy, I've known many a programs performance for be trashed by over zealous use of System.gc() or inappropriate -XX options.

like image 32
Mark Booth Avatar answered Dec 28 '22 11:12

Mark Booth