Results 1 to 8 of 8
  1. #1
    Sungron is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default deleting objects? how to free a class (JVM)

    Okay, I have allocated new class from C code...
    Java Code:
    jobject obj = env->AllocObject(objClass)
    But I got no idea what should I do when "obj" is no longer needed, how to tell jvm to free it? Are there any "delete" operators in Java or "delete" functions in jdk?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Welcome to Java. This is one of the places where Java is vastly different from C/C++. You don't delete or free objects in Java. Instead you should use them in the most limited scope possible. When they are no longer needed, the JVM frees them from memory for you.

  3. #3
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    Furbarable is right.

    When there is no reference to an object it will automatically be "garbage collected" (and this is achieved with limited scope)

    Assigning null to all references for an object will also achieve this.
    ie.

    Object a = new Object();
    Object b = a;

    a = null; // won't be garbage collected yet because of b reference
    b = null; // will be "eligable" for garbage collection

    But exactly when the garbage collection process happens depends on the JVM.

    It is possible to add something akin to a destructor by providing a finalize method. But it is better practice to explicitly do your own clean up work, as you don't know when the JVM will free up un-referenced objects.

    Happy coding :-)
    Last edited by Turtle; 02-02-2010 at 02:07 PM. Reason: did not include words "null to"

  4. #4
    Sungron is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default

    Huh, I heard about it....
    but it seems strange, how can Java know when object created from C code is no longer needed?

    First I wanted to create new Java object everytime when I alloc new class in my application, but now it seem silly.
    Ill allocate 100~ objects on application init and reuse them many times... instead of "deleting" them I may call method "ClearUp()".... hmm, is this a good solution? (if you know what I mean...)


    EDIT: - I just spotted second answer.
    Quote Originally Posted by Turtle View Post
    When there is no reference to an object it will automatically be "garbage collected" (and this is achieved with limited scope)
    thats what I am worrying about...
    There is (and there will be) NO references to objects created from C code using "AllocObject".
    I wanted to do something like this...
    C++ code
    Java Code:
    foo::foo()
    { //creates new class in C++ and new object in JVM
     jo = javaEnv->AllocObject(javaClass);
    }
    void foo::do_somthing()
    { //call Java method on our object
    javaEnv->CallFloatMethodV(jo,.........);
    }
    foo::~foo()
    {//we're deleting the foo, so "jo" is no longer needed
    javaEnv->FreeObject(jo); //this function does not exists
    }
    edit2: okay, let my silly post grow a bit

    what about:
    Java Code:
    public class GarbageHeap
    {	
    	GarbageHeap me;
          public void newME()
          {
    		me = this;
    	}
          public void deleteME()
             {
    		me = null;
    	}
              public void thinkME()
           {
               System.out.print("I am still alive and well");
    	}	
    }
    this class should persist untill removeME get called?
    Last edited by Sungron; 02-02-2010 at 02:33 PM.

  5. #5
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    Hi Sungron,

    Eek, I didn't realize you were talking about JNI. I don't know much about this.

    After a quick google:
    Fields and Methods
    JNI Functions

    Perhaps, DeleteLocalRef is what you're looking for?

    Best of luck

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Ah, if you are talking about JNI, then you yourself are responsible for delocating memory and objects from within your C code. I don't believe that this can be done by Java, except for making a native method that you've created call via JNI that does this cleanup (again within C code).

  7. #7
    Sungron is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Turtle View Post
    Perhaps, DeleteLocalRef is what you're looking for?
    DeleteLocalRef... DeleteLocalRef? Oh, thats it! I just wonder, why I didnt spot this before! Am I blind? Or just work too much....
    Sorry for this true spam... and thanks for help :)

  8. #8
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    Good :-)

    You've got me wondering how a class's members are handled now.

    ie, would a variation on the singleton pattern to remove the only instance work?

    Java Code:
    class S {
      private static S instance = null;
      public static S getInstance() {
        if (instance == null) {
          instance = new S();
        }
        return instance;
      }
      public static void removeInstance() {
        instance = null;
      }
    }
    Just a curiousity. I'm glad you've got the solution.
    Last edited by Turtle; 02-02-2010 at 10:12 PM. Reason: added code formatting [code][/code]

Similar Threads

  1. Send selective objects in a class over network
    By RDReavis in forum Advanced Java
    Replies: 2
    Last Post: 01-18-2010, 09:45 AM
  2. Replies: 1
    Last Post: 01-03-2010, 10:48 PM
  3. Deleting from an array
    By Dieter in forum Advanced Java
    Replies: 13
    Last Post: 09-25-2009, 10:27 AM
  4. Replies: 1
    Last Post: 01-22-2009, 05:25 PM
  5. Getting objects of a class
    By ravian in forum New To Java
    Replies: 1
    Last Post: 12-04-2007, 01:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •