How to prevent an object from getting garbage collected?
Are there any approaches by finalize or phantom reference or any other approaches?
I was asked this question in an interview. The interviewer suggested that finalize()
can be used.
How to prevent an object from getting garbage collected?
Are there any approaches by finalize or phantom reference or any other approaches?
I was asked this question in an interview. The interviewer suggested that finalize()
can be used.
This sounds like one of those interview-only-time-you'll-see-it questions. finalize() is run when your object is getting garbage collected, so it'd be pretty perverse to put something in there to prevent collection. Normally you just hold a reference and that's all you need.
I'm not even sure what would happen if you'd create a new reference for something in the finalizer - since the garbage collector's already decided to collect it would you then end up with a null ref? Seems like a poor idea, in any case. e.g.
I doubt this would work, or it might work but be dependant on the GC implenetation, or be "unspecified behavior". Looks evil, though.
The key point is if we set the real reference variable pointing to the object null,although we have instance variables of that class pointing to that object not set to null. The object is automatically eligible for garbage collection.if save the object to GC, use this code...
In finalize method.
In finalize :ID :1001
In finalize :ID :Praveen
------- After called GC () --------
Id :1001
Name :Praveen
I wonder if what they're going for is the pattern with resource pools (e.g. for network/db connections, or threads) where you use finalize to return a resource to the pool so that the actual object holding the resource isn't GC'ed.
Stupid example, in Java-like pseudocode and missing any kind of synchronization:
The best way is to use Unsafe, although
ByteBuffer
might be a possible workaround for some cases.Also search for the keyword "off-heap" memory.
Unsafe
Advantages over
ByteBuffer
:It is not however easy to get working. The method is described in the following articles:
They all consist of the following steps:
we need a
sizeof
operator, which Unsafe does not have. How to make one was asked at: In Java, what is the best way to determine the size of an object?. The best options is likely theinstrument
API, but that requires you to create a Jar and use special command line options...once we have
sizeof
, allocate enough memory withUnsafe#allocateMemory
, which is basically amalloc
and returns an addresscreate a regular on heap object, copy it to the allocated memory with
Unsafe#copyMemory
. To do this, you need to the address of the on-heap object, and the size of the objectset an
Object
to point to the allocated memory, then cast theObject
to your class.It does not seem possible to set the address of a variable directly with Unsafe, so we need to wrap the object into an array or wrapper object, and use
Unsafe#arrayBaseOffset
orUnsafe#objectFieldOffset
.once you are done, free the allocated memory with
freeMemory
If I ever get this to not segfault I will post an example :-)
ByteBuffer
Advantages over Unsafe:
JLS says:
Example of usage with primitives:
Related threads: