1

this question is for either android or java . suppose i have an instance of a class (even a thread) .

i wish that in order to make it fool proof against forgetting to dispose/close the instance (and avoid any possible memory leaks) , if there are no more references to this instance , it will automatically call a specific method , disposing/closing itself (in case of a thread , it will probably interrupt itself) .

is it possible to enforce such a thing? if needed , i don't mind that such a thing will occur only during GC .

android developer
  • 114,585
  • 152
  • 739
  • 1,270

3 Answers3

3

if there are no more references to this instance , it will automatically call a specific method , disposing/closing itself (in case of a thread , it will probably interrupt itself)

finalize() does what you describe here. You very rarely see it used though, and there are some pitfalls when using it. As you cant control garbage collection, you cant be sure when finalize will be run, if ever! From the api:

the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

You cant enforce garbage collection. You can only suggest the JVM to do so using System.gc(), but it is not guaranteed that it will be done.

Zavior
  • 6,412
  • 2
  • 29
  • 38
  • i didn't talk about this . i've written about being notified when there is no reference to the instance . i don't care about when the GC will occur . please read the question again . – android developer May 22 '12 at 14:49
  • When there are no more references to an object, it is going to be garbage collected when the GC gets around to it. Your not going to find memory leaks by looking for already dereferenced objects...its the ones that arn't dereferenced that will get you! – Ancantus May 22 '12 at 14:59
  • so suppose i create a new thread and run it , but i don't save any reference to it , will it ever call finalize , even if the thread is still running ? if so, can you please write a sample code that demonstrates it ? – android developer May 22 '12 at 15:09
  • 1
    http://stackoverflow.com/a/6409290/879114 see this answer. In short, the thread wont be garbage collected until it finishes or dies. – Zavior May 22 '12 at 15:21
1

I know this is very late but I hope it might help someone someday.

You can receive such events this by using the using the library that i am developing called gcRadar. It provides events when an object is orphaned and after the actual garbage collection of the object.

Any suggestions for improvements in the library are welcome.

R.daneel.olivaw
  • 2,681
  • 21
  • 31
  • the library uses the implementations of the java Reference class to derive callbacks by looping through the status of different objects at run-time. I you are interested further, you can check-out the code. – R.daneel.olivaw Dec 14 '13 at 10:19
  • it seems to be closed source, and that there isn't even a sample code of it, and no explanation of how it really works. – android developer Dec 14 '13 at 16:00
  • Well it's a work in progress and I can assure you that it's not closed source. – R.daneel.olivaw Dec 14 '13 at 17:48
  • Hi, if you are still interested I have moved the code to a GitHub repository which should be more accessible. Please check my modified answer. – R.daneel.olivaw Dec 18 '13 at 16:22
0

There is no way to access the references held by the VM. As Zavior suggested, the only way to know for sure that an object, or an "island" of objects is inaccessible, is to use the finalize method.

Please note that you will only get notified during GC runs. So it does not really help closing/disposing resources that are still referenced but should be closed. If you want to do that as well and do not want to use such constructs as try/catch/finally, you should write a manager class for the resources.

With any of the possibillities, including a manager class, you will not get a "bulletproof" way to consolidate your resources. Being careful is the best solution IMHO.

EDIT:

I have found this thread that may be useful.

Community
  • 1
  • 1
kostja
  • 60,521
  • 48
  • 179
  • 224
  • so there is no way to tell the VM to notify me when it found out that the object is no longer accessible ? maybe phantom reference ? – android developer May 22 '12 at 15:12
  • As stated earlier, the VM does not expose anything like a refcount. the only way to emulate it, is to do the counting yourself (e.g manager class) and to be aware of the fact that it is far away from being bulletproof. – kostja May 22 '12 at 15:28
  • what about this link , which i got using your link : http://java.dzone.com/articles/letting-garbage-collector-do-c ? how do you use it? do you know if it works? – android developer May 22 '12 at 15:42
  • I do not know if it works as I have not tried it, I just thought that it may help you try it for yourself. – kostja May 22 '12 at 15:46
  • i've tried to read it and check how it works . sadly i don't understand even how to use it. if anyone else could tell me how to make it work , i would give the answer a "V" . – android developer Jun 01 '12 at 08:21