Reputation: 547
I'm trying to delete an object that I created in an ArrayList:
turtles.add(new Turtle());
I want to get rid of the last turtle in the ArrayList, something like this:
turtles.get(turtles.size() - 1) = null;
Any ideas? Simply removing from the list doesn't work, and Java won't let me nullify in the above way. I'm pretty sure there is only be a single reference to a given Turtle object, as they are only ever created in this way.
P.S: The Turtle class is a thread, in case that matters.
Upvotes: 3
Views: 1273
Reputation: 812
If you are trying to destroy an object in Java you just have to set it null
:
object = null;
Upvotes: 0
Reputation: 547
Thanks to all for your help.
I've tried just using
turtles.remove(turtles.size() -1 );
and while it removed it from the list, I could tell that the thread was still running because it was still producing output. I'm pretty sure that the problem actually was that it was running (remember Turtle is a thread), and wouldn't die until it was done. I think I solved this problem by giving the thread an isAlive boolean that it checks each time run is called, and setting it to false and then removing it from the list when I want to delete it:
turtles.get(turtles.size() - 1).selfDestruct();
turtles.remove(turtles.size() - 1);
where selfDestruct does this:
isAlive = false;
I'm pretty sure this worked, I didn't see any leaks in perfmon.
Upvotes: 1
Reputation: 2737
In JAVA, only creation of objects is in our hands, destroying them is at the discretion of Garbage Collector (GC).
When you execute the line turtles.add(new Turtle()), the JVM creates a new Turtle object in the java HEAP and adds the corresponding reference to list.
Similarly, when you execute turtles.get(turtles.size() - 1) == null, you are essentially getting the reference to that object and making the reference null. You are not actually deleting the object. Only GC takes care of deleting the object.
Note that GC is a lower priority thread, and we do not have any control to make it kick in. But we can request for GC to kick in using System.gc().
Upvotes: 1
Reputation: 2031
The JVM garbage collector automatically frees memory associated with an object when there are no remaining references.
In this case, removing it from the list will work perfectly fine unless you've kept another reference to that object.
If you've passed this thread to an ExecutorService
to be ran (or called the start
method) then it wont be destroyed until after the run
method finishes. If that's the case and you want to stop the thread immediately, there are various ways to break out of the run
method.
Upvotes: 2
Reputation: 4199
use:
turtles.remove(turtles.size() -1 );
because:
turtles.get(turtles.size() - 1) = null;
will not work because you can only assign a value to a variable
The method .remove(int)
removes the object form the ArrayList
with the specified id.
here is a 'description' of the .remove()
method: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#remove%28int%29
Upvotes: 1
Reputation: 8652
when you use =
operator, the rvalue
is evaluated and is then assigned to the lvalue
.
But in you code you were expecting the lvalue
to be evaluated, which is not possible. That is why you are getting errors.
ArrayList
privides its own method to remove an object from its list.
turtles.remove(turtles.size() - 1);
Upvotes: 1
Reputation: 201429
Just use ArrayList.remove(int)
like,
turtles.remove(turtles.size() - 1);
The method you use to create individual instances in turtles
doesn't matter, when an instance is no longer reachable it is eligible for garbage collection.
Upvotes: 1