Delete object that was created in arraylist in java loop

I am trying to delete an object that I created in the 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? Just deleting from the list doesn't work and Java won't let me invalidate in the above way. I'm sure there is only one reference to this Turtle object, as they are only created this way.

PS: The Turtle class is a thread, if that matters.

+3


source to share


7 replies


The JVM garbage collector automatically frees the memory associated with an object when there are no remaining references.

In this case, removing it from the list will work fine, as long as you haven't saved another reference to that object.



If you passed this thread in ExecutorService

to run (or called a method start

), it will not be destroyed until the method ends run

. If this is the case and you want to stop the thread immediately, there are various ways to exit the method run

.

+2


source


using:

turtles.remove(turtles.size() -1 );

      

because:



turtles.get(turtles.size() - 1) = null;

      

won't work because you can assign a value to a variable

The method .remove(int)

removes the object form ArrayList

with the specified id. the method description is here .remove()

: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#remove%28int%29

+1


source


Just use ArrayList.remove(int)

for example

turtles.remove(turtles.size() - 1);

      

The method that you use to create individual instances in turtles

doesn't matter, when the instance is no longer available, it is eligible for garbage collection.

+1


source


when you use an operator =

it is evaluated rvalue

and then assigned lvalue

.
But in code, you were expecting an estimate lvalue

, which is not possible. This is why you are getting errors.
ArrayList

allocates its own method to remove an object from its list.

turtles.remove(turtles.size() - 1);

+1


source


In JAVA, we only have to create objects, destroy them at the discretion of the Garbage Harvester (GC).

When you execute the line turtles.add (new Turtle ()) , the JVM creates a new Turtle object in java HEAP and adds the corresponding link to the list.

Likewise, when you do turtles.get (turtles.size () - 1) == null , you essentially get a reference to that object and reference null. You are not actually deleting the object. Only the GC takes care of object disposal.

Note that the GC is a lower priority thread and we have no control over it to enter. But we can ask the GC to use it using System.gc ().

+1


source


Thanks everyone for your help.

I've tried just using

turtles.remove(turtles.size() -1 );

      

and while he removed it from the list, I could tell the stream was still running because it was still producing output. I'm pretty sure the problem is that it works (remember turtle is a thread) and won't die until it's done. I think I solved this problem by giving the stream an isAlive boolean, which it checks every time the call is triggered, and setting it to false, then removing it from the list when I want to remove it:

turtles.get(turtles.size() - 1).selfDestruct();
turtles.remove(turtles.size() - 1);

      

where selfDestruct does it:

isAlive = false;

      

I'm pretty sure it worked, I haven't seen any leaks in perfmon.

+1


source


If you are trying to destroy an object in Java, you just need to set it null

:

object = null;

      

0


source







All Articles