Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

GC + JNI + finalize ??  RSS feed

 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


Can someone point me to a good site for GC and finalize in Java 5?

I have JNI comm layer and its leaking memoey. My java heap is fine.

I noticed that data i get from JNi does not get deref. (that code is in finilize of swig wrapper of jni object)

So I wonder y it is not called and can I deref in my java objects once it is done with the jni event.

Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
finalize() is always called before an object is garbage-collected. The thing is that there is absolutely no guarantee that any particular object will ever be collected. Objects are garbage-collected only when the JVM needs to reuse their memory. This means that finalize() is not an especially good way to reclaim resources. Although the finalize() method isn't deprecated, it might as well be, because it has no truly useful purpose.

In general, if a Java object is a proxy for some native resource, then what you should do is give that class a public method named dispose(), and make it a requirement that clients of that class call dispose() when they are through with the object.
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply.

I already wrote cleanup() as a hack for the bug. but since app does not process all events in 1 thread. its a big change on the application and future developers should be forced to call it after they are done with the object. (boring)

The finilize on my java objects is called since they do not leak. I leak JVM memory.

Found this site:

http://devresource.hp.com/drc/resources/jmemmodel/index.jsp

If i add all messages to the static list and remove it from the static list in my java's finilize, it gurantees that the native object exist and i can deref it.

Makes bug fix localized to one class.

I still think there is a better way.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's an awfully complex solution for a simple problem, and worst of all, it gets one major thing wrong: the external resource may well be exhausted before the Java heap. If each 32-byte Java object has a pointer to one megabyte of native memory, then allocating a thousand objects might use only 32K of Java heap, but exhaust 1GB of native memory. GC won't be triggered, but the external resource could well be used up. You can't use finalize() to manage external resources, period.

But in any case, you seem to understand the issues. The only real answer is to have your clients use the dispose() method. A "finally" block is a handy way to make sure dispose() gets called in the face of errors.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!