Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ensuring a Java object is destroyed

The situation is a bit complex. I have a web container which controls another app in another machine using RMI. RMI server side has a HashMap which contains user information that the web app side needs to control.

When user logs out from web app side, there will be an RMI call to RMI server, telling the server to logout user. Then I just remove the item in the HashMap.

Now I realize the GC will not collect the object I removed in HashMap because maybe other thread is still using it and the object also contains some subobjects such as user contacts.

I really have no idea how to destroy the whole object including its properties and properties' properties.

like image 1000
Shisoft Avatar asked Jan 20 '26 07:01

Shisoft


2 Answers

Well, you have to remove all hard references to your object to make it eligible for garbage collection. There's no other way out. What is the other thread in your case?

like image 55
Alexander Pavlov Avatar answered Jan 21 '26 21:01

Alexander Pavlov


If this is a case of Distributed Garbage Collection (i.e. the client has a remote reference to an object which exists on the server), this system works by having the server keep track of which clients have requested access to remote objects running on the server. When a reference is made, the server marks the object as "dirty" and when a client drops the reference, it is marked as being "clean".

In these cases, if the client doesn't regularly notify the server that the objects are still in use, the server should automatically garbage collect them after a period of time.

like image 21
lucrussell Avatar answered Jan 21 '26 22:01

lucrussell