Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

GC + JNI + finalize ??

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic