How does a java app get rid of non-garbage ?

Not sure if I should rather post to "New to Java", but it's related to JNI as well:
I have a dynamic library, a corresponding Java singleton class and some other classes declaring native methods in that dll.
"All" is fine for the moment, except that eventually a test application does not terminate when exiting main().
I had the fear it has to do with some static Java objects which are never gc'ed and I built a test scenario like this:
public class Test1 {
     public static void main(String[] args) {
          boolean explicitGC = false;
          createObj();
          createObj();
          createObj();
          createObj();
          if (B.getIt() == null)
               System.out.println("All is gone");
          else
               System.out.println("leftover: "+B.getIt().toString());
          // B.getIt().destroy();
          if (explicitGC) {
               Runtime rt = Runtime.getRuntime();
               rt.gc();
               rt.runFinalization();
          if (B.getIt() == null)
               System.out.println("All is gone");
          else
               System.out.println("leftover: "+B.getIt().toString());
     static void createObj() {
          A myA= new A();
          System.out.println("Created " + myA.toString());
class A {
     B b;
     public A() {
          b=B.getSingle(this);
     public void finalize() {
          System.out.println(this.toString() +" finalizing");
     public String toString() {
          return "A@"+hashCode()+" referencing "+b.toString();
class B {
     private static B myself;
     private A a;
     private B(A _a) {
          a = _a;
          try {
               System.load("c:\\path\\MyNative.dll");
          } catch (UnsatisfiedLinkError e) {
               e.printStackTrace();
     public static B getSingle(A creator){
          if (myself==null) {
               myself = new B(creator);
          return myself;
     public static B getIt() {
          return myself;
     public String toString() {
          return "[B created by A@"+ a.hashCode()+"]";
     public void destroy() {
          System.out.println( toString()+" being destroyed now");
          myself = null;
     public void finalize() {
          destroy();
} <Sorry for the long code.>
This test does not show any problem, although the static B.myself is never destroyed explicitly, and neither the creating B.a object, if I remove the
B.getIt().destroy(); and the block contolled by explicitGC .
Now, it's a miracle for me, how JVM manages this nicely in this test app, and even unloads the DLL automatically.
My original problem is just slightly more complicated:
class B is extended by
     private native long Create(String url, String usr, String pwd);
     private native void Destroy(long obj);
                private static long obj;
     private B(A _a) {
          obj = Create(myURL, usr, pwd);
     public void destroy() {
          Destroy(obj);
     }Intention was to clean the native objects whenever the corresponding Java singleton object disappears.
A wise rule says "Leave the GC do its work alone, never rely on finalize() being called", but what else could be a solution?
Of course, in my test app I can add the B.getIt().destroy();
but in "real life" things are hidden like in main() above, and an end user of class A can easily forget (and won't understand why he should) call B.destroy()
It's perhaps a general design question about native singletons and how to get rid of them as nicely as Java does it.
Perhaps an explanation how it works in pure Java does help me already.
Do you need more explanations to get the problem? Any feedback is welcome.
Thanks!

jschell wrote:
eventually a test application does not terminate when exiting main(). Then a java thread or C thread is still running. Stop it from running.Thanks. So it's not related to static (native) objects but to some background (native) thread?
Well, but my issue is how to get notice of the final cleanup.
If I get the chance to run my native Destroy() method, all is fine.
However, the sample code - which can be exercised without a native lib - shows that it's easy (or possible, at least) to never run into the finalize() method of a static object and any other object it references.
Do I have to build my own reference counting?
What would be the most robust mechanism for this task: How to get notice of an application ending?
Sorry, now it's rather "New to Java" than JNI.
But I rather don't crosspost, OK ? ;)

Similar Messages

  • HT3529 My iPhone is showing that I have one unread messages but when I click on it, it does not show this so-called unread message. How do I go about getting rid of this symbol, or find this unread message?

    My iPhone is showing that I have one unread message but when I click on it, it does not show this so-called unread message. How do I go about getting rid of this symbol, or find this unread message?

    Try This...
    Close All Open Apps...  Perform a Reset... Try again...
    Reset  ( No Data will be Lost )
    Press and hold the Sleep/Wake button and the Home button at the same time for at least ten seconds, until the Apple logo appears. Release the Buttons.
    http://support.apple.com/kb/ht1430

  • HT1386 I have double and triples of photo's on my iPad. Not all my pics but most of them. I was told that I screwed up in synching with iTunes when I initally set up. How in the **** do I get rid of all the dups? I cannot delete them from my iPad. HELP ME

    I have double and triples of photo's on my iPad. Not all my pics but most of them. I was told that I screwed up in synching with iTunes when I initally set up. How in the **** do I get rid of all the dups? I cannot delete them from my iPad. HELP ME!

    If the shows are in fact still on the pad, they will be found in the videos app, not the music or I tunes app.  Look there and see if they are still there.
    If you don't see them there, go to the searchlight screen ( farthest left home page) and type in the name of one of the shows.  If it is on the pad, hiding somewhere, this will find it.
    If they are not there, they should not be using up space.
    To get them back onto your pad, plug in and sync again.  Make sure you transfer purchases first.   Before you sync, take a look at the video tab in your I tunes, and check what you want and don't want synced.

  • Something was hidden in a download and now these double green underlined hyperlinks show up everywhere, and pop ups too whenever I click on ANYTHING. I can't figure out how to find it and get rid of it.

    Something was hidden in a download and now these double green underlined hyperlinks show up everywhere, and pop ups too whenever I click on ANYTHING. I can't figure out how to find it and get rid of it.

    You installed the "DownLite" trojan, perhaps under a different name. Remove it as follows.
    Malware is constantly changing to get around the defenses against it. The instructions in this comment are valid as of now, as far as I know. They won't necessarily be valid in the future. Anyone finding this comment a few days or more after it was posted should look for more recent discussions or start a new one.
    Back up all data.
    Triple-click anywhere in the line below on this page to select it:
    /Library/LaunchAgents/com.vsearch.agent.plist
    Right-click or control-click the line and select
    Services ▹ Reveal in Finder (or just Reveal)
    from the contextual menu.* A folder should open with an item named "VSearch" selected. Drag the selected item to the Trash. You may be prompted for your administrator login password.
    Repeat with each of these lines:
    /Library/LaunchDaemons/com.vsearch.daemon.plist
    /Library/LaunchDaemons/com.vsearch.helper.plist
    /Library/LaunchDaemons/Jack.plist
    Restart the computer and empty the Trash. Then delete the following items in the same way:
    /Library/Application Support/VSearch
    /Library/PrivilegedHelperTools/Jack
    /System/Library/Frameworks/VSearch.framework
    Some of these items may be absent, in which case you'll get a message that the file can't be found. Skip that item and go on to the next one.
    From the Safari menu bar, select
    Safari ▹ Preferences... ▹ Extensions
    Uninstall any extensions you don't know you need, including any that have the word "Spigot" in the description. If in doubt, uninstall all extensions. Do the equivalent for the Firefox and Chrome browsers, if you use either of those.
    This trojan is distributed on illegal websites that traffic in pirated movies. If you, or anyone else who uses the computer, visit such sites and follow prompts to install software, you can expect much worse to happen in the future.
    You may be wondering why you didn't get a warning from Gatekeeper about installing software from an unknown developer, as you should have. The reason is that the DownLite developer has a codesigning certificate issued by Apple, which causes Gatekeeper to give the installer a pass. Apple could revoke the certificate, but as of this writing, has not done so, even though it's aware of the problem. This failure of oversight is inexcusable and has compromised the value of Gatekeeper and the Developer ID program. You can't rely on Gatekeeper alone to protect you from harmful software.
    *If you don't see the contextual menu item, copy the selected text to the Clipboard by pressing the key combination  command-C. In the Finder, select
    Go ▹ Go to Folder...
    from the menu bar and paste into the box that opens by pressing command-V. You won't see what you pasted because a line break is included. Press return.

  • HT4946 if my phone is disabled, how can I restore to get rid of a passcode

    If my phone is disabled, how can I restore to get rid of a passcode that was entered by mistake.

    Options for when an iOS device gets locked because of forgotten password:
    Restore (and reset password) on your device by connecting it to the last computer to which it was connected:
    iTunes: Backing up, updating, and restoring iOS software - http://support.apple.com/kb/HT1414
    If you cannot connect it to the computer to which the device was last connected you will have to use recovery mode to completely reset the device, losing all data:
    iOS: Unable to update or restore - http://support.apple.com/kb/HT1808 - recovery mode (e.g., cannot connect to computer last used to sync device, iTunes still asks for a password)

  • How can I find and get rid of key loggers on my iPhone and ipad

    How can I find and get rid of key loggers on my iPhone and ipad

    Why do you think you have them on there? Have you allowed someone to physically have each device and jailbreak it?

  • If I'm suspecting that I'm hacked, how can I confirm it and how can I Un-hack/get rid of it ?

    If I'm suspecting that I'm hacked, how can I confirm it and how can I Un-hack/get rid of it ?
    Knowing that when I turn On my Macpro, the Apple logo appeares late and always when Im connected to the WiFi It notifys me that there is someone using my IP address!!

    You aren't being hacked. Your wireless network is probably misconfigured and/or hacked. Turn on WPA security and then reboot it.

  • How does a Purchase Order get into 'Approved, Reserved' status?

    How does a Purchase Order get into 'Approved, Reserved' status?

    Hi,
    When an approved purchase order is unreserved and the GL date is modified and re-submitted for approval, the document (PO) gets reserved and approved status.
    I believe you put this question in a wrong thread (Financial), next time onwards, please put your question in correct thread and I am sure you will get the solution faster :).
    Regards,
    S.P DASH

  • How does the MDKP table gets updates

    Dear PP Guru's,
    I am new to SAP PP,
    We have a query that how does the MDKP table gets updates,
    Some time If we create new material by coping the material which is showing in table MDKP,
    But new material is not showing in table MDKP,
    Based on table MDKP we are fetching some data, due to above problem we are not able to fetch the data for some materials,
    Please suggest what could be the reason,
    Any help will be highly appreciated,
    Thanks in advance,
    Sachin.

    Thanks Thaila,
    I have checked the same by doing MRP through MD02 & the entry is getting update in MDKP,
    But for some of our materials MRP (MD02) is done earlier, but these materials are not showing in MDKP,
    Please suggest, when the entries in MDKP are removed, are there any conditions for removing the entries from MDKP.
    please pour some light on the issue,
    Thanks once again,
    Sachin.

  • How does the ituns app on my ipad2 work?

    How does the itunes app on my 1pad2 work?

    The iTunes app on your iPad is just a portal to the iTunes Store.  To play music or videos, use the Music App and the Videos App.
    Here's a link to iPad iOS5 User Guide

  • In iPhoto 11, how do I find and get rid of duplicate photos?

    in iPhoto 11, how do I find and get rid of doplicate photos?

    For dealing with duplicates in iPhoto check out Duplicate Annihilator

  • I downloaded something called MPlayerX and it is ruining my computer (a macbook pro) and now on safari theres pop up adds that shouldn't be there and on the google homepage theres adds. I want to know how to fix this and get rid of MPlayerX :( PLEASE

    I downloaded something called MPlayerX and it is ruining my computer (a macbook pro) and now on safari theres pop up adds that shouldn't be there and on the google homepage theres adds. I want to know how to fix this and get rid of MPlayerX PLEASE HELP ME!!!!

    MPlayerX isn't itself the problem; it was downloaded from a source that packaged adware with it. Click here and follow the instructions, or if desired, run Adware Medic.
    (114466)

  • I have TWO FILE MANGER ICONS appearing in the Google Icon on my home page. It is a LG g3 how do I delete or get rid of one of them?

    I have TWO FILE MANGER ICONS appearing in the Google Icon on my home page. It is a LG g3 how do I delete or get rid of one of them?

    Tap and hold an icon.  It will bring a menu drop down and bring the icon to remove.

  • Hello how I want to help get rid of the remaining balance in the account, I want to change the state

    Hello how I want to help get rid of the remaining balance in the account, I want to change the state

    Go to www.apple.com/emea/support/itunes/contact.html and ask the iTunes Store staff to zero your account balance.
    (108244)

  • HT201209 ive sent a gift how does the other person get it

    ive sent a gift how does the other person get it

    Resending iTunes Gifts and Gift Certificates
              http://support.apple.com/kb/HT1541

Maybe you are looking for

  • Understanding Xcode Documentation Reference Library

    I'm trying to understand the API documentation that comes with Xcode (Help->Documentation). I'd like to look at OpenGL ES API documentation. So, I downloaded "Developer Tools Library" under the "Apple Xcode 3.1" Doc Set. When browsing, it's not clear

  • Can you find the imported Library ? My system can't !

    Dear People, In making a first attempt to do an applet in JBuilder7 rather than from the Dos prompt my imported library is not being found . Here are the steps I did. 1) deleted all the premade source code on the scrren and keyed in my applet code "M

  • Importing .mov into FCE

    Hello, I just installed Final Cut Express to try and edit videos I capture through the Kodak Zi6 handheld video camera, but when I try to drag and drop into FCE I get an error message saying "Unknown file"? Any ideas? thanks.

  • Notification light on 2720 missed calls / texts

    Hi Does the 2720 have an external notification light when a call or text is missed? I need to set the phone to silent when at work, and can't keep picking the phone up to see if I have missed a call when I return to my desk. If the 2720 doesn't have

  • Uninstall Final Cut Studio 1

    Helllo, I want to remove FCS 1 from my iMac in order to install FCS 3. How could do that?