Results 1 to 8 of 8
- 02-02-2010, 12:49 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 10
- Rep Power
- 0
deleting objects? how to free a class (JVM)
Okay, I have allocated new class from C code...
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?Java Code:jobject obj = env->AllocObject(objClass)
-
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.
- 02-02-2010, 01:05 PM #3
Member
- Join Date
- Nov 2007
- Location
- New Zealand
- Posts
- 36
- Rep Power
- 0
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 01:07 PM. Reason: did not include words "null to"
- 02-02-2010, 01:08 PM #4
Member
- Join Date
- Jan 2010
- Posts
- 10
- Rep Power
- 0
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.
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
edit2: okay, let my silly post grow a bitJava 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 }
what about:
this class should persist untill removeME get called?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"); } }Last edited by Sungron; 02-02-2010 at 01:33 PM.
- 02-02-2010, 01:36 PM #5
Member
- Join Date
- Nov 2007
- Location
- New Zealand
- Posts
- 36
- Rep Power
- 0
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
-
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).
- 02-02-2010, 01:56 PM #7
Member
- Join Date
- Jan 2010
- Posts
- 10
- Rep Power
- 0
- 02-02-2010, 02:07 PM #8
Member
- Join Date
- Nov 2007
- Location
- New Zealand
- Posts
- 36
- Rep Power
- 0
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?
Just a curiousity. I'm glad you've got the solution.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; } }Last edited by Turtle; 02-02-2010 at 09:12 PM. Reason: added code formatting [code][/code]
Similar Threads
-
Send selective objects in a class over network
By RDReavis in forum Advanced JavaReplies: 2Last Post: 01-18-2010, 08:45 AM -
Exception Class for class that compares objects variables. Help Please.
By darkblue24 in forum New To JavaReplies: 1Last Post: 01-03-2010, 09:48 PM -
Deleting from an array
By Dieter in forum Advanced JavaReplies: 13Last Post: 09-25-2009, 09:27 AM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM -
Getting objects of a class
By ravian in forum New To JavaReplies: 1Last Post: 12-04-2007, 12:23 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks