3

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.

3
  • There is no way to "destroy" an object. What are you trying to accomplish? Commented Oct 19, 2014 at 2:31
  • Is the reference to a running thread, and you're trying to get the thread to stop executing? Commented Oct 19, 2014 at 2:44
  • I no longer wanted the object to exist. Basically just wanted it to be garbage collected. I think I answered my own question though, see below. Commented Oct 19, 2014 at 2:54

7 Answers 7

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I called the start method. It was set to run a very large number of times (200000 I think). Giving it a boolean flag to check each time it runs and setting the flag to false when I want to get rid of the object AND also removing it from the list worked.
1

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.

Comments

1

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);

Comments

1

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

Comments

1

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().

Comments

1

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.

2 Comments

This is exactly what I was describing in my answer.
Yes, you're right. I didn't read your answer closely enough and missed the key parts, prior to researching more elsewhere. I've voted it up and accepted it as the best answer.
0

If you are trying to destroy an object in Java you just have to set it null:

object = null;

2 Comments

Tried that. Java wants a named object in order to do this: someTurtle = null;, but because my object was created in a list, it doesnt have a name, and the only way to access it is using ArrayList's .get(), and this doesn't work: turtleList.get(turtleList.size() - 1) = null. Java doesn't like that and I just get the error message that the left hand side of an assignment must be a variable.
Have you read the question? the goal is to remove it form a list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.