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


suppose following code:

object obj = new object(); obj = null; 

at point, don't have reference object, it's still on heap, because garbage collection don't happens instantly. there way re obtain reference on object, before it'll collected gc?

only possible way seen far use unsafe, provides direct memory access, need know in memory object allocated. also, there weak\softreference, implemented special gc behavior.

p.s. predict questions "why need it?" - because science not why, it's why not! (c)

this highly jvm implementation specific. in naive implementation having memory allocation information associated each object, find object memory has not been freed yet , seems thinking direction.

however, sophisticated jvms don’t work way. associating allocation information each object create giant overhead, given may have millions of objects in runtime. not regarding memory requirement, regarding amount of work has done maintaining these information when allocating or freeing object.

so makes part of heap memory object? reference holding it. garbage collector traverses existing references , within objects found way, find meta information (i.e. pointer class specific information) needed understand how memory belongs object , how interpret contained data (to traverse sub-references, if any). unreferenced unused per se , might contain old objects or might have never been used @ all, knows. once references object gone, there no information left former existence of object.

getting point, there no explicit freeing action. when garbage collector has found surviving objects, copied dedicated new place , old place considered free, regardless of how many objects there before , how memory each individual object occupied when alive.

when search memory considered unused, may find reminiscences of old objects, without references starting points, it’s impossible whether bit pattern looks object dead object or coincidence. if managed resurrect object way, had nothing original idea of being able resurrect reference, because gc didn’t run yet.

note modifications ordinary life time work holding reference object. e.g., when class defines non-trivial finalize() method, jvm has add reference queue of objects needing finalization. similarly, soft, weak , phantom references encapsulate reference object in question. debugger may keep reference object, once has seen it.

but trivial code object obj = new object(); obj = null;, assuming there’s no breakpoint set in-between, there no additional reference , hence, no way of resurrecting object. jvm may elide entire allocation when optimizing code @ runtime. wouldn’t find remainings of object in ram when searching object never existed.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -