saroff February 2016

Is there a way to receive object, without having reference to it?

Suppose following code:

Object obj = new Object();
obj = null;

At this point, i don't have any reference to this object, but it's still on the heap, because garbage collection don't happens instantly. Is there a way to re obtain reference on this object, before it'll be collected by GC?

Only possible way that i seen so far is to use Unsafe, which provides direct memory access, but i will need to know where in memory exactly object is allocated. Also, there is Weak\SoftReference, but they are implemented by special GC behavior.

P.S. To predict questions like "Why do you need it?" - Because science is not about why, it's about why not! (c)

Answers


EJP February 2016

At this point, i don't have any reference to this object, but it's still on the heap, because garbage collection don't happens instantly.

It is undefined where it is, and it is also undefined whether or not garbage collection happens instantly.

Is there a way to re obtain reference on this object, before it'll be collected by GC?

You already had one and you threw it away. Just keep it.

I will need to know where in memory exactly object is allocated.

There is nothing in standard Java that will tell you that, and no useful way you could make use of the information if you could get it.

Also, there is Weak/SoftReference, but they are implemented by special GC behavior.

I don't see how this affects your question, whatever it is.


Holger February 2016

This is highly JVM implementation specific. In a naive implementation having memory allocation information associated with each object, you could find an object whose memory has not been freed yet and it seems you are thinking into that direction.

However, sophisticated JVMs don’t work that way. Associating allocation information with each object would create a giant overhead, given that you may have millions of objects in your runtime. Not only regarding memory requirement, but also regarding the amount of work that has to be done for maintaining these information when allocating or freeing an object.

So what makes a part of your heap memory an object? Only the reference you are holding to it. The garbage collector traverses existing references and within the objects found this way, it will find meta information (i.e. a pointer to class specific information) needed to understand how much memory belongs to the object and how to interpret the contained data (to traverse the sub-references, if any). Everything unreferenced is unused per se and might contain old objects or might have never been used at all, who knows. Once all references to an object are gone, there is no information left about the former existence of this object.

Getting to the point, there is no explicit freeing action. When the garbage collector has found surviving objects, they will be copied to a dedicated new place and their old place is considered to be free, regardless of how many objects there were before and how much memory each individual object occupied when it was alive.

When you search memory that is considered to be unused, you may find reminiscences of old objects, but without references to their starting points, it’s impossible to say whether the bit pattern that looks like an object really is a dead object or just a coincidence. Even if you managed to resurrect an object that way, it had nothing to do with your original idea of being abl

Post Status

Asked in February 2016
Viewed 2,241 times
Voted 5
Answered 2 times

Search




Leave an answer