When the garbage collector eliminates the object, where does that object go?

Bhrugs :

Let's say garbage collector has identified an instance of a class (an object) - which is not being used by the Java Program. So the Garbage Collector decides to eliminate that object since it is not being used anywhere in the code. Now, when the elimination of an object happens, where does the object go inside the memory? What actually happens to that object inside the memory? In general term - when the object is eliminated - how the computer handles the elimination? how the elimination works?

My question is more about - what happens after the elimination of an object inside the heap happens. (in Java)

Few things I can think of which may be happening here are: the memory of that object (inside the heap) now becomes free, but the object still remains in the system, and since the memory is available, newly created objects can take its space. so if I think in binary format: 1s and 0s of previous object are going to be overwritten by new sequences of 1s and 0s (new object).

In your answer, please provide answers in this format:

  1. What actually happens to the garbage collected object?
  2. What are the names of these processes called in Java or in computer science terms?

Note: I tried to find the similar question/answer on stackoverflow, but this question is specific, so I couldn't find the answer, thus I had to post a question.

cmlonder :

What actually happens to that object inside the memory? In general term - when the object is eliminated - how the computer handles the elimination? how the elimination works?

Memory is not given back to the operating system directly. Memory is now free to use for future object allocations for the JVM. This means there is no explicit deletion, the memory of the object that references now free to use. Because memory allocation from OS is already done by JVM, only in some GC memory is given back to the system which also its frequency/behavior can be set explicitly with GC params but it is complicated operation since it comes with a cost.

There are ongoing works for optimizing this behavior, e.g: look at the openjdk 12 hotspot work for it: https://openjdk.java.net/jeps/346

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=141691&siteId=1