Does Garbage Collector remove all objects or only those that have no reference?

Asked

Viewed 187 times

2

In a Java application there are objects that are not being used, among these objects there are those that have no reference. Like the Garbage Collector deals with it? He removes all or only those who have no reference?

  • Several possible related (or duplicates) in this answer

  • Only those who have no reference are removed

  • Although it is a little on the subject I think this doubt is more specific and does not have and probably will not have when I finish there (I need to resume the answer... :))

  • @Isac but among the unused objects, there are also those that have reference

  • If it is not being used but has reference means it can still be used, so it cannot be removed.

  • @Isac you said right "can still come to used", but eventually it also can not be used

  • 2

    I think your confusion lies precisely in the concept of "used". What is for you an "unused object" ? Can you exemplify in code ? The rule to eliminate an object of memory focuses only on valid references and guarantee of non-accession in the rest of code from a certain point.

Show 3 more comments

1 answer

3


The situation is a little more complicated than that. Of course, the reference is never "removed".

First, let’s understand that the Java GC uses a mechanism of copying, then "never" objects (in the last implementation I know, that’s detail) are removed, they are abandoned. GC copies those who survive and throws away those who no longer need to remain active.

GC works by reading "all" memory looking for reference to objects in heap. Of course there is optimization not to read all it, depends on the generation that is needing to be cleaned.

Today Java may have reference to objects outside the heap, these objects are not free from GC, it can be read to see if it has any reference to another object within it, but references to it do not matter.

It is enough to have a reference to an object and it is already considered to be alive. And it is possible to have false positives. There are heuristics to avoid having to analyze the entire reference graph. You can’t have false negatives, you’d risk killing something that needs to continue. Some objects happen to survive even without a reference. It is not usually long and lasts little, in general this occurs more in Gen0 that needs to be very fast.

In the analysis the GC looks at everything that is in the so-called roots, for example those registered and the stack. From there he looks at all the objects that he knows is alive, or that already have references to him in the roots. He builds a graph, walks through all living objects. Objects that have no references to it are never analyzed to see if they have references to other objects. In general the analysis ends in Gen0. Unless you fire a cleaning process in Gen1, you go to it. And the same goes for Gen2.

I saw that Java did as . NET, which would have a heap of large objects that works slightly different, but the basis is that same.

There is a special treatment when an object is above Gen0. Any attempt to write references contained in these objects has a writing barrier and analyzes if you need to take any extra action. If you don’t need it and fast, if you need it, you have an extra control. Log in to slow path whether the new reference points to an object in a previous generation. These cases need to create an extra reference list for the previous generation to look at when they will do their cleaning. This is necessary for the GC not to have to analyze the whole memory and at least in the lower generations that are cleaned more frequently.

Looking at all this, roughly speaking, if an object does not have a reference to it, it is not analyzed whether it has references to other objects, and if the only reference to another object was that which is no longer analyzed, or all references are in objects that will no longer be active, so that previously pointed object no longer has references that force it to stay alive, so it will probably be abandoned and its memory will no longer be occupied. If there remains a single reference to an object, even if unintentionally, it will still be kept and copied to a new memory arena, probably in a generation higher than it is, until Gen2 which is currently the last.

If there is a reference to it then being used, even if it is not being accessed at this time.

Java has added new Gcs that work a little differently.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.