8

I have a simple arraylist like this: ..

ArrayList<SP> sps = new ArrayList<SP>();
sps.add(new SP("1"));
sps.add(new SP("2"));
sps.add(new SP("3"));

.. When I remove 1 object from that list. What happen next? That list just remove reference of that object and then object auto release that memory zone ( because nothing referenced to that memory of that object) or that object was released directly from that memory zone by that list ?

P/s:Sorry for my bad English.

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
Phúc Sáng
  • 81
  • 1
  • 1
  • 2

4 Answers4

14

If you call ArrayList.remove() the element at the given index is removed from the list (and all elements following it move down one index). The object itself still exists, no memory has been freed yet.

If there are no remaining references to that object in your application after the object has been removed from the list - meaning it's now unused by your program - the Garbage Collector will free up the memory associated with that object automatically so it can be reused.

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • 1
    i think there is a typo, elements following move up one index – Amit Kumar Feb 25 '17 at 09:00
  • 1
    @AmitKumar I'm not sure what you mean. If you have a three-element list and call `list.remove(0)` the element at index `0` will be removed and the two elements previously at indices `1` and `2` will now be located at indices `0` and `1` - they've moved down. – dimo414 Feb 25 '17 at 09:25
  • @dimo414 , i guess you phrased it differently, but i meant the same thing, – Amit Kumar Feb 25 '17 at 09:36
4

A couple things happen, both to the ArrayList and the removed object.

When ArrayList.remove() is called, it does indeed remove reference to that object, from the ArrayList. In addition, the contents proceeding the removed element are shifted down by one.

If there are no other remaining references to that object, the memory allocated for the object is now eligible to be freed in an automated process called Garbage Collection

Example:

ArrayList<Integer> example = otherArrayList; // {2,3,10,6}
example.remove(1);                           // Remove second element
System.out.println(example.toString());

> [2, 10, 6] 
// The space to which '3' is allocated to is now eligible for GC

Example of an object being ineligible for GC:

ArrayList<Integer> example = otherArrayList; // {2,3,10,6}
int ref = example.get(1);                    // '3' is assigned to ref
example.remove(1);                           // Remove second element
System.out.println(example.toString());
System.out.print(ref);                      

> [2, 10, 6] 
> 3

/* Java is based on pass-by-value, but it passes references 
   when we pass existing object values. Which means the
   address space allocated for ref (and formerly example.get(1)) 
   is ineligible for GC.
*/ 
JoryAnderson
  • 103
  • 7
1

That list just remove reference of that object and then object auto release that memory zone ( because nothing referenced to that memory of that object) or that object was released directly from that memory zone by that list ?

JVM doesn't work in this way.

1) While a object is referenced by a living object in the memory, it is not eligible for GC.

2) Even if an object is not referenced any longer by a living object in the memory, the memory release is not automatic.
It will be released only when a Gargbage collection (minor or full) is performed by the Garbage collector.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0
ArrayList<SP> sps = new ArrayList<SP>();

What this means is that, there is an ArrayList of type SP and every index of this ArrayList will be a type of SP, in short it will be a reference variable of SP.

Now when you call new SP("1");, an object to SP will be created and the constructor returns the reference of that object. But here there is no compatible reference variable assigned to new SP("1");. So that object can't be accessed anymore.

But in your ArrayList (for the same statement) there will be a reference to accept the return from the constructor of SP. So when you remove that object from list, it is loosing its only reference and becomes eligible for garbage collection.

Refernce : Calling methods on reference variable vs Calling methods on a new object

Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52