Get Off My Lawn
Get Off My Lawn

Reputation: 36299

Remove ArrayList Object from Memory

I have a bunch of Objects in an ArrayList, if I call ArrayList.remove(object) Do I need to do anything else to remove the object from memory? I am adding and removing objects from this list at a fairly very quick pace, so if it doesn't get removed from memory I it will start taking up space and start to slow down the Game.

Upvotes: 3

Views: 10450

Answers (5)

kindageeky
kindageeky

Reputation: 41

if you chew through the heap quick enough, you can nudge the gc along with some jvm args ... we have an app that handles billions of operations a day and have tuned it heavily with the ergonomic gc settings I'd encourage you to play with the adaptive size policy, and the max pause setting primarily. Run the program with a profiler exercising the arraylist as you normally would for a while (several minutes) and see how the ambient state of the various heap generations looks. You may end up having to also tweak the memory allocations to generations.

Upvotes: 1

Jayamohan
Jayamohan

Reputation: 12924

ArrayList.remove removes the object from the array, and then if the object is not referenced to by any other objects, the GC will delete that object.

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

- When you call ArrayList.remove(object), you just remove the objects from the List Not from the Memory.

- It will depend on the Garbage Collector to decide when its gonna remove the object from the heap, under normal circumstances its an object is ready for garbage collection as it has No reference to it anymore.

- There is a classic example of why String which is an object in Java should not be used for storing password instead char[] should be used.

See this link...

Why is char[] preferred over String for passwords?

Upvotes: 5

Ryan Stewart
Ryan Stewart

Reputation: 128789

No, you don't have to do anything else, as long as that's the only place that's referencing the object. Welcome to the joys of a garbage-collected language! Java will clean up old, unreferenced objects when it decides that it needs to reclaim some memory.

Upvotes: 1

Amir T
Amir T

Reputation: 2758

Java does automatic garbage collection. So once an object is no longer referred to, it can be deleted, that doesn't mean it will be deleted. Garbage collection is automatic, you can ask that it be done by calling System.gc() however this is just a sugestion to run it.

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/System.html#gc%28%29

Upvotes: 1

Related Questions