Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if object can be fetched by garbage collector

is there a way to check if an object can be fetched by the garbage collector?

Somewhere in my code I've got a reference to an object:

MyObject mo = myObject;

Then, via Eclipse Debugger, I get the objects memory location. Afterwards, I set the reference null:

mo = null;

Is there any way to check if the previously referenced object is now suitable for garbage collection or if there's somewhere another reference to it?

Thanks a lot,

Stefan

like image 416
swalkner Avatar asked Nov 19 '10 10:11

swalkner


People also ask

How do you know if an object is eligible for garbage collection?

An object is eligible to be garbage collected if its reference variable is lost from the program during execution. Sometimes they are also called unreachable objects. What is reference of an object? The new operator dynamically allocates memory for an object and returns a reference to it.

How do you make an object eligible for garbage collection GC in Java?

In java, the memory allocated at runtime i.e. heap area can be made free by the process of garbage collection. It is nothing but just a method of making the memory free which is not being used by the programmer. Only the objects who have no longer reference to them are eligible for garbage collection in java.

How do you ensure that an instance is not garbage collected?

We have three ways to achieve same - 1) Increasing the Heap -Eden space size . 2) Create Singleton class with Static reference . 3) Override finalize() method and never let that object dereference.


1 Answers

You cannot do this at runtime with an arbitrary object, and in fact it's not fully possible to do this deterministically. However, there are two options that may be suitable depending on your needs:

  1. Take a heap dump after you set the reference to null, and then load it up in a heap analyzer tool such as jhat or a profiler that supports this. These tools should let you traverse the path from the GC roots and thus check if your object is still reachable or not.
  2. Wrap the object in a PhantomReference with a given ReferenceQueue. When the reference is enqueued, you know that the object has been garbage collected. (Unfortunately, if the reference is unqueued it could be because the object is still reachable, or it could be because the GC just hasn't inspected the object yet. As with all GC-related questions, garbage collection is not a deterministic process!)

On the whole though, I agree that the best option is to be aware of memory leak issues and design your application to avoid them. If you do have a memory leak it should be obvious enough, and you can then focus your energies on finding the problem (again by dumping and analysing the heap for objects that are incorrectly reachable).

The steps above are relatively time-consuming, and shouldn't be something that you do after every change just to reassure yourself, but rather are tools you'd use to investigate a specific problem.

like image 171
Andrzej Doyle Avatar answered Sep 22 '22 04:09

Andrzej Doyle