-2

If I have an Array List with classes in it and I remove a class from the list, will the class be removed/deleted and the memory it used be freed up?

thanks

oliverjrose99
  • 47
  • 1
  • 8

2 Answers2

1

I think you mean that you have objects in the ArrayList, not classes.

Objects are instances of classes, for example Integer is a class, when you do Integer a = new Integer(1), then a is an object of the class Integer).

Java is garbage collected, it means that you don't have to explicitly tell the compiler when to free the memory (like in C for example), the garbage collector will periodically check if the instance is not referenced anywhere else, and if that's the case, it means that the object can no longer be accessed and the memory will be freed.

What you can do is to call System.gc() to tell the garbage collector that you want it to perform a collection. This is only an hint, there is no way to force an immediate collection.

enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
0

No. You remove reference, not actual class.

Arvy
  • 95
  • 12