Deleting an object in java?
I want to delete an object I created, (a oval which follows you), but how would I do this?
delete follower1;
didn't work.
EDIT:
Okay, I'll give some more context. I'm making a small game with a oval you can control, and a oval which follows you. Now I've got files named: DrawPanel.class, this class draws everything on the screen, and handles collisions, sounds, etc. I got an enemy.class, which is the oval following the player. I got an entity.class, which is the player you can control. And if the player intersects with the follower, I want my player object to get deleted. The way I'm doing it:
public void checkCollisions(){
if(player.getBounds().intersects(follower1.getBounds())){
Follower1Alive = false;
player.health = player.health - 10;
}
}
java
add a comment |
I want to delete an object I created, (a oval which follows you), but how would I do this?
delete follower1;
didn't work.
EDIT:
Okay, I'll give some more context. I'm making a small game with a oval you can control, and a oval which follows you. Now I've got files named: DrawPanel.class, this class draws everything on the screen, and handles collisions, sounds, etc. I got an enemy.class, which is the oval following the player. I got an entity.class, which is the player you can control. And if the player intersects with the follower, I want my player object to get deleted. The way I'm doing it:
public void checkCollisions(){
if(player.getBounds().intersects(follower1.getBounds())){
Follower1Alive = false;
player.health = player.health - 10;
}
}
java
1
can you provide a little more context?
– Joseph Weissman
Apr 22 '11 at 16:26
2
There is no deletion of objects in Java. But you normally don't need to: what you need is to make sure there is nothing shown at screen anymore (if this is what "following you" is doing).
– Paŭlo Ebermann
Apr 22 '11 at 16:32
Perhaps you want to try and force garbage collector to remove an object? there is a question about it here already. Or perhaps you trying something else? you can try reading a bit about the garbage collector here or here If its still no help, you will need to be more specific.
– Fawix
Apr 22 '11 at 16:35
possible duplicate of Force explicit deletion of a Java object
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Jan 29 '15 at 11:06
If you really really really want to handle object allocations manually, use JNI: create a C/C++ lib which is used from Java code but does everything (create, delete, etc.) on it's own - but inside your lib you have C/C++ code, no Java. I've not seen any way to delete a Java object manually. If your profiler tells you that there are problems, these problems often base on "forgotten" references to objects. In your case there does not seem to be a problem anywhere.
– Johannes
Jan 15 '16 at 10:12
add a comment |
I want to delete an object I created, (a oval which follows you), but how would I do this?
delete follower1;
didn't work.
EDIT:
Okay, I'll give some more context. I'm making a small game with a oval you can control, and a oval which follows you. Now I've got files named: DrawPanel.class, this class draws everything on the screen, and handles collisions, sounds, etc. I got an enemy.class, which is the oval following the player. I got an entity.class, which is the player you can control. And if the player intersects with the follower, I want my player object to get deleted. The way I'm doing it:
public void checkCollisions(){
if(player.getBounds().intersects(follower1.getBounds())){
Follower1Alive = false;
player.health = player.health - 10;
}
}
java
I want to delete an object I created, (a oval which follows you), but how would I do this?
delete follower1;
didn't work.
EDIT:
Okay, I'll give some more context. I'm making a small game with a oval you can control, and a oval which follows you. Now I've got files named: DrawPanel.class, this class draws everything on the screen, and handles collisions, sounds, etc. I got an enemy.class, which is the oval following the player. I got an entity.class, which is the player you can control. And if the player intersects with the follower, I want my player object to get deleted. The way I'm doing it:
public void checkCollisions(){
if(player.getBounds().intersects(follower1.getBounds())){
Follower1Alive = false;
player.health = player.health - 10;
}
}
java
java
edited Aug 28 '12 at 11:37
Daniel Causebrook
4401719
4401719
asked Apr 22 '11 at 16:25
StanStan
1,491122937
1,491122937
1
can you provide a little more context?
– Joseph Weissman
Apr 22 '11 at 16:26
2
There is no deletion of objects in Java. But you normally don't need to: what you need is to make sure there is nothing shown at screen anymore (if this is what "following you" is doing).
– Paŭlo Ebermann
Apr 22 '11 at 16:32
Perhaps you want to try and force garbage collector to remove an object? there is a question about it here already. Or perhaps you trying something else? you can try reading a bit about the garbage collector here or here If its still no help, you will need to be more specific.
– Fawix
Apr 22 '11 at 16:35
possible duplicate of Force explicit deletion of a Java object
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Jan 29 '15 at 11:06
If you really really really want to handle object allocations manually, use JNI: create a C/C++ lib which is used from Java code but does everything (create, delete, etc.) on it's own - but inside your lib you have C/C++ code, no Java. I've not seen any way to delete a Java object manually. If your profiler tells you that there are problems, these problems often base on "forgotten" references to objects. In your case there does not seem to be a problem anywhere.
– Johannes
Jan 15 '16 at 10:12
add a comment |
1
can you provide a little more context?
– Joseph Weissman
Apr 22 '11 at 16:26
2
There is no deletion of objects in Java. But you normally don't need to: what you need is to make sure there is nothing shown at screen anymore (if this is what "following you" is doing).
– Paŭlo Ebermann
Apr 22 '11 at 16:32
Perhaps you want to try and force garbage collector to remove an object? there is a question about it here already. Or perhaps you trying something else? you can try reading a bit about the garbage collector here or here If its still no help, you will need to be more specific.
– Fawix
Apr 22 '11 at 16:35
possible duplicate of Force explicit deletion of a Java object
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Jan 29 '15 at 11:06
If you really really really want to handle object allocations manually, use JNI: create a C/C++ lib which is used from Java code but does everything (create, delete, etc.) on it's own - but inside your lib you have C/C++ code, no Java. I've not seen any way to delete a Java object manually. If your profiler tells you that there are problems, these problems often base on "forgotten" references to objects. In your case there does not seem to be a problem anywhere.
– Johannes
Jan 15 '16 at 10:12
1
1
can you provide a little more context?
– Joseph Weissman
Apr 22 '11 at 16:26
can you provide a little more context?
– Joseph Weissman
Apr 22 '11 at 16:26
2
2
There is no deletion of objects in Java. But you normally don't need to: what you need is to make sure there is nothing shown at screen anymore (if this is what "following you" is doing).
– Paŭlo Ebermann
Apr 22 '11 at 16:32
There is no deletion of objects in Java. But you normally don't need to: what you need is to make sure there is nothing shown at screen anymore (if this is what "following you" is doing).
– Paŭlo Ebermann
Apr 22 '11 at 16:32
Perhaps you want to try and force garbage collector to remove an object? there is a question about it here already. Or perhaps you trying something else? you can try reading a bit about the garbage collector here or here If its still no help, you will need to be more specific.
– Fawix
Apr 22 '11 at 16:35
Perhaps you want to try and force garbage collector to remove an object? there is a question about it here already. Or perhaps you trying something else? you can try reading a bit about the garbage collector here or here If its still no help, you will need to be more specific.
– Fawix
Apr 22 '11 at 16:35
possible duplicate of Force explicit deletion of a Java object
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Jan 29 '15 at 11:06
possible duplicate of Force explicit deletion of a Java object
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Jan 29 '15 at 11:06
If you really really really want to handle object allocations manually, use JNI: create a C/C++ lib which is used from Java code but does everything (create, delete, etc.) on it's own - but inside your lib you have C/C++ code, no Java. I've not seen any way to delete a Java object manually. If your profiler tells you that there are problems, these problems often base on "forgotten" references to objects. In your case there does not seem to be a problem anywhere.
– Johannes
Jan 15 '16 at 10:12
If you really really really want to handle object allocations manually, use JNI: create a C/C++ lib which is used from Java code but does everything (create, delete, etc.) on it's own - but inside your lib you have C/C++ code, no Java. I've not seen any way to delete a Java object manually. If your profiler tells you that there are problems, these problems often base on "forgotten" references to objects. In your case there does not seem to be a problem anywhere.
– Johannes
Jan 15 '16 at 10:12
add a comment |
7 Answers
7
active
oldest
votes
You should remove the references to it by assigning null or leaving the block where it was declared. After that, it will be automatically deleted by the garbage collector (not immediately, but eventually).
Example 1:
Object a = new Object();
a = null; // after this, if there is no reference to the object, it will be deleted by the garbage collector
Example 2:
if (something)
{
Object o = new Object();
} // as you leave the block, the reference is deleted. Later on the garbage collector will delete he object itself.
Not something that you are currently looking for, but FYI: you can invoke the garbage collector with the call System.gc()
18
Having to manually callSystem.gc()is generally not a good idea, since it may actually be ignored by the JVM.
– cdeange
Jun 22 '13 at 22:51
2
Not a good idea, but you can also call similar to System.. Runtime.getRuntime().gc(). More Info:he garbage collection routines that Java provides are members of the Runtime class. The Runtime class is a special class that has a single object (a Singleton) for each main program. The Runtime object provides a mechanism for communicating directly with the virtual machine. To get the Runtime instance, you can use the method Runtime.getRuntime(), which returns the Singleton.
– Timo
Nov 12 '13 at 13:54
Isnullassignment a definite sign to the GC that this object is to be Garbaged Cleaned in the next GC invocation?* Assigningnullto collection objects (Lists, Arrays) marks them for cleaning? stackoverflow.com/questions/449409/…
– George Daramouskas
Mar 22 '15 at 17:21
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:44
There is no guarantee that the garbage collector will ever run, or that, if it does, it will collect (not 'delete') any specific object.
– user207421
Jan 20 '17 at 6:11
|
show 1 more comment
Your C++ is showing.
There is no delete in java, and all objects are created on the heap. The JVM has a garbage collector that relies on reference counts.
Once there are no more references to an object, it becomes available for collection by the garbage collector.
myObject = null may not do it; for example:
Foo myObject = new Foo(); // 1 reference
Foo myOtherObject = myObject; // 2 references
myObject = null; // 1 reference
All this does is set the reference myObject to null, it does not affect the object myObject once pointed to except to simply decrement the reference count by 1. Since myOtherObject still refers to that object, it is not yet available to be collected.
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:44
5
"The JVM has a garbage collector that relies on reference counts." -> that's a very simplified explanation. It checks whether objects are reachable from a specific "root"
– Johannes
Jan 15 '16 at 10:04
The JVM has a garbage collector that specifically does not rely on reference counts. It relies on reachability. If it relied on reference counts it could never collect cycles, and it does.
– user207421
Jan 20 '17 at 6:12
add a comment |
If you want help an object go away, set its reference to null.
String x = "sadfasdfasd";
// do stuff
x = null;
Setting reference to null will make it more likely that the object will be garbage collected, as long as there are no other references to the object.
Whenever I do that, when my object intersects, my whole game will freeze, and it will not delete the object.
– Stan
Apr 22 '11 at 17:00
Because you're probably throwing an uncaught null pointer exception. More context would certainly help us guide you through this.
– Joseph Weissman
Apr 22 '11 at 17:09
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:45
If you really need them to go away, then that would do it eventually.
– John Percival Hackworth
Jan 10 '16 at 1:53
add a comment |
You don't need to delete objects in java. When there is no reference to an object, it will be collected by the garbage collector automatically.
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:45
add a comment |
You can remove the reference using null.
Let's say You have class A:
A a = new A();
a=null;
last statement will remove the reference of the object a and that object will be "garbage collected" by JVM.
It is one of the easiest ways to do this.
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:45
add a comment |
Yea, java is Garbage collected, it will delete the memory for you.
32
in the least expected moment
– matcheek
Apr 22 '11 at 16:26
2
s/delete/leak/g
– Mark K Cowan
Oct 16 '16 at 20:55
add a comment |
//Just use a List
//create the list
public final List<Object> myObjects;
//instantiate the list
myObjects = new ArrayList<Object>();
//add objects to the list
Object object = myObject;
myObjects.add(object);
//remove the object calling this method if you have more than 1 objects still works with 1
//object too.
private void removeObject(){
int len = myObjects.size();
for(int i = 0;i<len; i++){
Objects object = myObjects.get(i);
myObjects.remove(object);
}
}
The reason this answer is incorrect is because it only removes a reference to the Object, and not the actual object itself.
– Ben Morris
Aug 25 '16 at 18:38
The reason why this answer is incorrect because it shows how you can remove an object from a list - this was not the question.
– flipperweid
Jan 26 '17 at 7:04
add a comment |
protected by Community♦ Dec 9 '14 at 15:37
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should remove the references to it by assigning null or leaving the block where it was declared. After that, it will be automatically deleted by the garbage collector (not immediately, but eventually).
Example 1:
Object a = new Object();
a = null; // after this, if there is no reference to the object, it will be deleted by the garbage collector
Example 2:
if (something)
{
Object o = new Object();
} // as you leave the block, the reference is deleted. Later on the garbage collector will delete he object itself.
Not something that you are currently looking for, but FYI: you can invoke the garbage collector with the call System.gc()
18
Having to manually callSystem.gc()is generally not a good idea, since it may actually be ignored by the JVM.
– cdeange
Jun 22 '13 at 22:51
2
Not a good idea, but you can also call similar to System.. Runtime.getRuntime().gc(). More Info:he garbage collection routines that Java provides are members of the Runtime class. The Runtime class is a special class that has a single object (a Singleton) for each main program. The Runtime object provides a mechanism for communicating directly with the virtual machine. To get the Runtime instance, you can use the method Runtime.getRuntime(), which returns the Singleton.
– Timo
Nov 12 '13 at 13:54
Isnullassignment a definite sign to the GC that this object is to be Garbaged Cleaned in the next GC invocation?* Assigningnullto collection objects (Lists, Arrays) marks them for cleaning? stackoverflow.com/questions/449409/…
– George Daramouskas
Mar 22 '15 at 17:21
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:44
There is no guarantee that the garbage collector will ever run, or that, if it does, it will collect (not 'delete') any specific object.
– user207421
Jan 20 '17 at 6:11
|
show 1 more comment
You should remove the references to it by assigning null or leaving the block where it was declared. After that, it will be automatically deleted by the garbage collector (not immediately, but eventually).
Example 1:
Object a = new Object();
a = null; // after this, if there is no reference to the object, it will be deleted by the garbage collector
Example 2:
if (something)
{
Object o = new Object();
} // as you leave the block, the reference is deleted. Later on the garbage collector will delete he object itself.
Not something that you are currently looking for, but FYI: you can invoke the garbage collector with the call System.gc()
18
Having to manually callSystem.gc()is generally not a good idea, since it may actually be ignored by the JVM.
– cdeange
Jun 22 '13 at 22:51
2
Not a good idea, but you can also call similar to System.. Runtime.getRuntime().gc(). More Info:he garbage collection routines that Java provides are members of the Runtime class. The Runtime class is a special class that has a single object (a Singleton) for each main program. The Runtime object provides a mechanism for communicating directly with the virtual machine. To get the Runtime instance, you can use the method Runtime.getRuntime(), which returns the Singleton.
– Timo
Nov 12 '13 at 13:54
Isnullassignment a definite sign to the GC that this object is to be Garbaged Cleaned in the next GC invocation?* Assigningnullto collection objects (Lists, Arrays) marks them for cleaning? stackoverflow.com/questions/449409/…
– George Daramouskas
Mar 22 '15 at 17:21
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:44
There is no guarantee that the garbage collector will ever run, or that, if it does, it will collect (not 'delete') any specific object.
– user207421
Jan 20 '17 at 6:11
|
show 1 more comment
You should remove the references to it by assigning null or leaving the block where it was declared. After that, it will be automatically deleted by the garbage collector (not immediately, but eventually).
Example 1:
Object a = new Object();
a = null; // after this, if there is no reference to the object, it will be deleted by the garbage collector
Example 2:
if (something)
{
Object o = new Object();
} // as you leave the block, the reference is deleted. Later on the garbage collector will delete he object itself.
Not something that you are currently looking for, but FYI: you can invoke the garbage collector with the call System.gc()
You should remove the references to it by assigning null or leaving the block where it was declared. After that, it will be automatically deleted by the garbage collector (not immediately, but eventually).
Example 1:
Object a = new Object();
a = null; // after this, if there is no reference to the object, it will be deleted by the garbage collector
Example 2:
if (something)
{
Object o = new Object();
} // as you leave the block, the reference is deleted. Later on the garbage collector will delete he object itself.
Not something that you are currently looking for, but FYI: you can invoke the garbage collector with the call System.gc()
edited Aug 1 '14 at 4:54
Adam Jensen
392925
392925
answered Apr 22 '11 at 16:30
MByDMByD
117k22229250
117k22229250
18
Having to manually callSystem.gc()is generally not a good idea, since it may actually be ignored by the JVM.
– cdeange
Jun 22 '13 at 22:51
2
Not a good idea, but you can also call similar to System.. Runtime.getRuntime().gc(). More Info:he garbage collection routines that Java provides are members of the Runtime class. The Runtime class is a special class that has a single object (a Singleton) for each main program. The Runtime object provides a mechanism for communicating directly with the virtual machine. To get the Runtime instance, you can use the method Runtime.getRuntime(), which returns the Singleton.
– Timo
Nov 12 '13 at 13:54
Isnullassignment a definite sign to the GC that this object is to be Garbaged Cleaned in the next GC invocation?* Assigningnullto collection objects (Lists, Arrays) marks them for cleaning? stackoverflow.com/questions/449409/…
– George Daramouskas
Mar 22 '15 at 17:21
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:44
There is no guarantee that the garbage collector will ever run, or that, if it does, it will collect (not 'delete') any specific object.
– user207421
Jan 20 '17 at 6:11
|
show 1 more comment
18
Having to manually callSystem.gc()is generally not a good idea, since it may actually be ignored by the JVM.
– cdeange
Jun 22 '13 at 22:51
2
Not a good idea, but you can also call similar to System.. Runtime.getRuntime().gc(). More Info:he garbage collection routines that Java provides are members of the Runtime class. The Runtime class is a special class that has a single object (a Singleton) for each main program. The Runtime object provides a mechanism for communicating directly with the virtual machine. To get the Runtime instance, you can use the method Runtime.getRuntime(), which returns the Singleton.
– Timo
Nov 12 '13 at 13:54
Isnullassignment a definite sign to the GC that this object is to be Garbaged Cleaned in the next GC invocation?* Assigningnullto collection objects (Lists, Arrays) marks them for cleaning? stackoverflow.com/questions/449409/…
– George Daramouskas
Mar 22 '15 at 17:21
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:44
There is no guarantee that the garbage collector will ever run, or that, if it does, it will collect (not 'delete') any specific object.
– user207421
Jan 20 '17 at 6:11
18
18
Having to manually call
System.gc() is generally not a good idea, since it may actually be ignored by the JVM.– cdeange
Jun 22 '13 at 22:51
Having to manually call
System.gc() is generally not a good idea, since it may actually be ignored by the JVM.– cdeange
Jun 22 '13 at 22:51
2
2
Not a good idea, but you can also call similar to System.. Runtime.getRuntime().gc(). More Info:he garbage collection routines that Java provides are members of the Runtime class. The Runtime class is a special class that has a single object (a Singleton) for each main program. The Runtime object provides a mechanism for communicating directly with the virtual machine. To get the Runtime instance, you can use the method Runtime.getRuntime(), which returns the Singleton.
– Timo
Nov 12 '13 at 13:54
Not a good idea, but you can also call similar to System.. Runtime.getRuntime().gc(). More Info:he garbage collection routines that Java provides are members of the Runtime class. The Runtime class is a special class that has a single object (a Singleton) for each main program. The Runtime object provides a mechanism for communicating directly with the virtual machine. To get the Runtime instance, you can use the method Runtime.getRuntime(), which returns the Singleton.
– Timo
Nov 12 '13 at 13:54
Is
null assignment a definite sign to the GC that this object is to be Garbaged Cleaned in the next GC invocation?* Assigning null to collection objects (Lists, Arrays) marks them for cleaning? stackoverflow.com/questions/449409/…– George Daramouskas
Mar 22 '15 at 17:21
Is
null assignment a definite sign to the GC that this object is to be Garbaged Cleaned in the next GC invocation?* Assigning null to collection objects (Lists, Arrays) marks them for cleaning? stackoverflow.com/questions/449409/…– George Daramouskas
Mar 22 '15 at 17:21
So I should make all of my textviews and imagebuttons null in the
onPause?– Ruchir Baronia
Jan 10 '16 at 0:44
So I should make all of my textviews and imagebuttons null in the
onPause?– Ruchir Baronia
Jan 10 '16 at 0:44
There is no guarantee that the garbage collector will ever run, or that, if it does, it will collect (not 'delete') any specific object.
– user207421
Jan 20 '17 at 6:11
There is no guarantee that the garbage collector will ever run, or that, if it does, it will collect (not 'delete') any specific object.
– user207421
Jan 20 '17 at 6:11
|
show 1 more comment
Your C++ is showing.
There is no delete in java, and all objects are created on the heap. The JVM has a garbage collector that relies on reference counts.
Once there are no more references to an object, it becomes available for collection by the garbage collector.
myObject = null may not do it; for example:
Foo myObject = new Foo(); // 1 reference
Foo myOtherObject = myObject; // 2 references
myObject = null; // 1 reference
All this does is set the reference myObject to null, it does not affect the object myObject once pointed to except to simply decrement the reference count by 1. Since myOtherObject still refers to that object, it is not yet available to be collected.
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:44
5
"The JVM has a garbage collector that relies on reference counts." -> that's a very simplified explanation. It checks whether objects are reachable from a specific "root"
– Johannes
Jan 15 '16 at 10:04
The JVM has a garbage collector that specifically does not rely on reference counts. It relies on reachability. If it relied on reference counts it could never collect cycles, and it does.
– user207421
Jan 20 '17 at 6:12
add a comment |
Your C++ is showing.
There is no delete in java, and all objects are created on the heap. The JVM has a garbage collector that relies on reference counts.
Once there are no more references to an object, it becomes available for collection by the garbage collector.
myObject = null may not do it; for example:
Foo myObject = new Foo(); // 1 reference
Foo myOtherObject = myObject; // 2 references
myObject = null; // 1 reference
All this does is set the reference myObject to null, it does not affect the object myObject once pointed to except to simply decrement the reference count by 1. Since myOtherObject still refers to that object, it is not yet available to be collected.
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:44
5
"The JVM has a garbage collector that relies on reference counts." -> that's a very simplified explanation. It checks whether objects are reachable from a specific "root"
– Johannes
Jan 15 '16 at 10:04
The JVM has a garbage collector that specifically does not rely on reference counts. It relies on reachability. If it relied on reference counts it could never collect cycles, and it does.
– user207421
Jan 20 '17 at 6:12
add a comment |
Your C++ is showing.
There is no delete in java, and all objects are created on the heap. The JVM has a garbage collector that relies on reference counts.
Once there are no more references to an object, it becomes available for collection by the garbage collector.
myObject = null may not do it; for example:
Foo myObject = new Foo(); // 1 reference
Foo myOtherObject = myObject; // 2 references
myObject = null; // 1 reference
All this does is set the reference myObject to null, it does not affect the object myObject once pointed to except to simply decrement the reference count by 1. Since myOtherObject still refers to that object, it is not yet available to be collected.
Your C++ is showing.
There is no delete in java, and all objects are created on the heap. The JVM has a garbage collector that relies on reference counts.
Once there are no more references to an object, it becomes available for collection by the garbage collector.
myObject = null may not do it; for example:
Foo myObject = new Foo(); // 1 reference
Foo myOtherObject = myObject; // 2 references
myObject = null; // 1 reference
All this does is set the reference myObject to null, it does not affect the object myObject once pointed to except to simply decrement the reference count by 1. Since myOtherObject still refers to that object, it is not yet available to be collected.
edited Oct 26 '14 at 4:50
Muhammad Rehan Qadri
335213
335213
answered Apr 22 '11 at 16:50
Brian RoachBrian Roach
66.2k8111140
66.2k8111140
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:44
5
"The JVM has a garbage collector that relies on reference counts." -> that's a very simplified explanation. It checks whether objects are reachable from a specific "root"
– Johannes
Jan 15 '16 at 10:04
The JVM has a garbage collector that specifically does not rely on reference counts. It relies on reachability. If it relied on reference counts it could never collect cycles, and it does.
– user207421
Jan 20 '17 at 6:12
add a comment |
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:44
5
"The JVM has a garbage collector that relies on reference counts." -> that's a very simplified explanation. It checks whether objects are reachable from a specific "root"
– Johannes
Jan 15 '16 at 10:04
The JVM has a garbage collector that specifically does not rely on reference counts. It relies on reachability. If it relied on reference counts it could never collect cycles, and it does.
– user207421
Jan 20 '17 at 6:12
So I should make all of my textviews and imagebuttons null in the
onPause?– Ruchir Baronia
Jan 10 '16 at 0:44
So I should make all of my textviews and imagebuttons null in the
onPause?– Ruchir Baronia
Jan 10 '16 at 0:44
5
5
"The JVM has a garbage collector that relies on reference counts." -> that's a very simplified explanation. It checks whether objects are reachable from a specific "root"
– Johannes
Jan 15 '16 at 10:04
"The JVM has a garbage collector that relies on reference counts." -> that's a very simplified explanation. It checks whether objects are reachable from a specific "root"
– Johannes
Jan 15 '16 at 10:04
The JVM has a garbage collector that specifically does not rely on reference counts. It relies on reachability. If it relied on reference counts it could never collect cycles, and it does.
– user207421
Jan 20 '17 at 6:12
The JVM has a garbage collector that specifically does not rely on reference counts. It relies on reachability. If it relied on reference counts it could never collect cycles, and it does.
– user207421
Jan 20 '17 at 6:12
add a comment |
If you want help an object go away, set its reference to null.
String x = "sadfasdfasd";
// do stuff
x = null;
Setting reference to null will make it more likely that the object will be garbage collected, as long as there are no other references to the object.
Whenever I do that, when my object intersects, my whole game will freeze, and it will not delete the object.
– Stan
Apr 22 '11 at 17:00
Because you're probably throwing an uncaught null pointer exception. More context would certainly help us guide you through this.
– Joseph Weissman
Apr 22 '11 at 17:09
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:45
If you really need them to go away, then that would do it eventually.
– John Percival Hackworth
Jan 10 '16 at 1:53
add a comment |
If you want help an object go away, set its reference to null.
String x = "sadfasdfasd";
// do stuff
x = null;
Setting reference to null will make it more likely that the object will be garbage collected, as long as there are no other references to the object.
Whenever I do that, when my object intersects, my whole game will freeze, and it will not delete the object.
– Stan
Apr 22 '11 at 17:00
Because you're probably throwing an uncaught null pointer exception. More context would certainly help us guide you through this.
– Joseph Weissman
Apr 22 '11 at 17:09
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:45
If you really need them to go away, then that would do it eventually.
– John Percival Hackworth
Jan 10 '16 at 1:53
add a comment |
If you want help an object go away, set its reference to null.
String x = "sadfasdfasd";
// do stuff
x = null;
Setting reference to null will make it more likely that the object will be garbage collected, as long as there are no other references to the object.
If you want help an object go away, set its reference to null.
String x = "sadfasdfasd";
// do stuff
x = null;
Setting reference to null will make it more likely that the object will be garbage collected, as long as there are no other references to the object.
answered Apr 22 '11 at 16:28
John Percival HackworthJohn Percival Hackworth
10.6k22438
10.6k22438
Whenever I do that, when my object intersects, my whole game will freeze, and it will not delete the object.
– Stan
Apr 22 '11 at 17:00
Because you're probably throwing an uncaught null pointer exception. More context would certainly help us guide you through this.
– Joseph Weissman
Apr 22 '11 at 17:09
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:45
If you really need them to go away, then that would do it eventually.
– John Percival Hackworth
Jan 10 '16 at 1:53
add a comment |
Whenever I do that, when my object intersects, my whole game will freeze, and it will not delete the object.
– Stan
Apr 22 '11 at 17:00
Because you're probably throwing an uncaught null pointer exception. More context would certainly help us guide you through this.
– Joseph Weissman
Apr 22 '11 at 17:09
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:45
If you really need them to go away, then that would do it eventually.
– John Percival Hackworth
Jan 10 '16 at 1:53
Whenever I do that, when my object intersects, my whole game will freeze, and it will not delete the object.
– Stan
Apr 22 '11 at 17:00
Whenever I do that, when my object intersects, my whole game will freeze, and it will not delete the object.
– Stan
Apr 22 '11 at 17:00
Because you're probably throwing an uncaught null pointer exception. More context would certainly help us guide you through this.
– Joseph Weissman
Apr 22 '11 at 17:09
Because you're probably throwing an uncaught null pointer exception. More context would certainly help us guide you through this.
– Joseph Weissman
Apr 22 '11 at 17:09
So I should make all of my textviews and imagebuttons null in the
onPause?– Ruchir Baronia
Jan 10 '16 at 0:45
So I should make all of my textviews and imagebuttons null in the
onPause?– Ruchir Baronia
Jan 10 '16 at 0:45
If you really need them to go away, then that would do it eventually.
– John Percival Hackworth
Jan 10 '16 at 1:53
If you really need them to go away, then that would do it eventually.
– John Percival Hackworth
Jan 10 '16 at 1:53
add a comment |
You don't need to delete objects in java. When there is no reference to an object, it will be collected by the garbage collector automatically.
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:45
add a comment |
You don't need to delete objects in java. When there is no reference to an object, it will be collected by the garbage collector automatically.
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:45
add a comment |
You don't need to delete objects in java. When there is no reference to an object, it will be collected by the garbage collector automatically.
You don't need to delete objects in java. When there is no reference to an object, it will be collected by the garbage collector automatically.
answered Apr 22 '11 at 16:34
fmucarfmucar
12.3k13948
12.3k13948
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:45
add a comment |
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:45
So I should make all of my textviews and imagebuttons null in the
onPause?– Ruchir Baronia
Jan 10 '16 at 0:45
So I should make all of my textviews and imagebuttons null in the
onPause?– Ruchir Baronia
Jan 10 '16 at 0:45
add a comment |
You can remove the reference using null.
Let's say You have class A:
A a = new A();
a=null;
last statement will remove the reference of the object a and that object will be "garbage collected" by JVM.
It is one of the easiest ways to do this.
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:45
add a comment |
You can remove the reference using null.
Let's say You have class A:
A a = new A();
a=null;
last statement will remove the reference of the object a and that object will be "garbage collected" by JVM.
It is one of the easiest ways to do this.
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:45
add a comment |
You can remove the reference using null.
Let's say You have class A:
A a = new A();
a=null;
last statement will remove the reference of the object a and that object will be "garbage collected" by JVM.
It is one of the easiest ways to do this.
You can remove the reference using null.
Let's say You have class A:
A a = new A();
a=null;
last statement will remove the reference of the object a and that object will be "garbage collected" by JVM.
It is one of the easiest ways to do this.
edited Dec 2 '15 at 15:50
jiaweizhang
6911725
6911725
answered Dec 26 '13 at 10:11
LakhanLakhan
367
367
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:45
add a comment |
So I should make all of my textviews and imagebuttons null in theonPause?
– Ruchir Baronia
Jan 10 '16 at 0:45
So I should make all of my textviews and imagebuttons null in the
onPause?– Ruchir Baronia
Jan 10 '16 at 0:45
So I should make all of my textviews and imagebuttons null in the
onPause?– Ruchir Baronia
Jan 10 '16 at 0:45
add a comment |
Yea, java is Garbage collected, it will delete the memory for you.
32
in the least expected moment
– matcheek
Apr 22 '11 at 16:26
2
s/delete/leak/g
– Mark K Cowan
Oct 16 '16 at 20:55
add a comment |
Yea, java is Garbage collected, it will delete the memory for you.
32
in the least expected moment
– matcheek
Apr 22 '11 at 16:26
2
s/delete/leak/g
– Mark K Cowan
Oct 16 '16 at 20:55
add a comment |
Yea, java is Garbage collected, it will delete the memory for you.
Yea, java is Garbage collected, it will delete the memory for you.
answered Apr 22 '11 at 16:25
Jake KalstadJake Kalstad
1,748920
1,748920
32
in the least expected moment
– matcheek
Apr 22 '11 at 16:26
2
s/delete/leak/g
– Mark K Cowan
Oct 16 '16 at 20:55
add a comment |
32
in the least expected moment
– matcheek
Apr 22 '11 at 16:26
2
s/delete/leak/g
– Mark K Cowan
Oct 16 '16 at 20:55
32
32
in the least expected moment
– matcheek
Apr 22 '11 at 16:26
in the least expected moment
– matcheek
Apr 22 '11 at 16:26
2
2
s/delete/leak/g
– Mark K Cowan
Oct 16 '16 at 20:55
s/delete/leak/g
– Mark K Cowan
Oct 16 '16 at 20:55
add a comment |
//Just use a List
//create the list
public final List<Object> myObjects;
//instantiate the list
myObjects = new ArrayList<Object>();
//add objects to the list
Object object = myObject;
myObjects.add(object);
//remove the object calling this method if you have more than 1 objects still works with 1
//object too.
private void removeObject(){
int len = myObjects.size();
for(int i = 0;i<len; i++){
Objects object = myObjects.get(i);
myObjects.remove(object);
}
}
The reason this answer is incorrect is because it only removes a reference to the Object, and not the actual object itself.
– Ben Morris
Aug 25 '16 at 18:38
The reason why this answer is incorrect because it shows how you can remove an object from a list - this was not the question.
– flipperweid
Jan 26 '17 at 7:04
add a comment |
//Just use a List
//create the list
public final List<Object> myObjects;
//instantiate the list
myObjects = new ArrayList<Object>();
//add objects to the list
Object object = myObject;
myObjects.add(object);
//remove the object calling this method if you have more than 1 objects still works with 1
//object too.
private void removeObject(){
int len = myObjects.size();
for(int i = 0;i<len; i++){
Objects object = myObjects.get(i);
myObjects.remove(object);
}
}
The reason this answer is incorrect is because it only removes a reference to the Object, and not the actual object itself.
– Ben Morris
Aug 25 '16 at 18:38
The reason why this answer is incorrect because it shows how you can remove an object from a list - this was not the question.
– flipperweid
Jan 26 '17 at 7:04
add a comment |
//Just use a List
//create the list
public final List<Object> myObjects;
//instantiate the list
myObjects = new ArrayList<Object>();
//add objects to the list
Object object = myObject;
myObjects.add(object);
//remove the object calling this method if you have more than 1 objects still works with 1
//object too.
private void removeObject(){
int len = myObjects.size();
for(int i = 0;i<len; i++){
Objects object = myObjects.get(i);
myObjects.remove(object);
}
}
//Just use a List
//create the list
public final List<Object> myObjects;
//instantiate the list
myObjects = new ArrayList<Object>();
//add objects to the list
Object object = myObject;
myObjects.add(object);
//remove the object calling this method if you have more than 1 objects still works with 1
//object too.
private void removeObject(){
int len = myObjects.size();
for(int i = 0;i<len; i++){
Objects object = myObjects.get(i);
myObjects.remove(object);
}
}
answered Dec 21 '13 at 7:43
user3124852user3124852
1
1
The reason this answer is incorrect is because it only removes a reference to the Object, and not the actual object itself.
– Ben Morris
Aug 25 '16 at 18:38
The reason why this answer is incorrect because it shows how you can remove an object from a list - this was not the question.
– flipperweid
Jan 26 '17 at 7:04
add a comment |
The reason this answer is incorrect is because it only removes a reference to the Object, and not the actual object itself.
– Ben Morris
Aug 25 '16 at 18:38
The reason why this answer is incorrect because it shows how you can remove an object from a list - this was not the question.
– flipperweid
Jan 26 '17 at 7:04
The reason this answer is incorrect is because it only removes a reference to the Object, and not the actual object itself.
– Ben Morris
Aug 25 '16 at 18:38
The reason this answer is incorrect is because it only removes a reference to the Object, and not the actual object itself.
– Ben Morris
Aug 25 '16 at 18:38
The reason why this answer is incorrect because it shows how you can remove an object from a list - this was not the question.
– flipperweid
Jan 26 '17 at 7:04
The reason why this answer is incorrect because it shows how you can remove an object from a list - this was not the question.
– flipperweid
Jan 26 '17 at 7:04
add a comment |
protected by Community♦ Dec 9 '14 at 15:37
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
1
can you provide a little more context?
– Joseph Weissman
Apr 22 '11 at 16:26
2
There is no deletion of objects in Java. But you normally don't need to: what you need is to make sure there is nothing shown at screen anymore (if this is what "following you" is doing).
– Paŭlo Ebermann
Apr 22 '11 at 16:32
Perhaps you want to try and force garbage collector to remove an object? there is a question about it here already. Or perhaps you trying something else? you can try reading a bit about the garbage collector here or here If its still no help, you will need to be more specific.
– Fawix
Apr 22 '11 at 16:35
possible duplicate of Force explicit deletion of a Java object
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Jan 29 '15 at 11:06
If you really really really want to handle object allocations manually, use JNI: create a C/C++ lib which is used from Java code but does everything (create, delete, etc.) on it's own - but inside your lib you have C/C++ code, no Java. I've not seen any way to delete a Java object manually. If your profiler tells you that there are problems, these problems often base on "forgotten" references to objects. In your case there does not seem to be a problem anywhere.
– Johannes
Jan 15 '16 at 10:12