In Java, when having some non-empty ArrayList, does
list.sublist(from,to).clear()
edit (refactored question):
reduce the internal size of the ArrayList (i.e. let the ArrayList use less memory afterwards)?
I am particularly interested in the case where from = 0, i.e. where the list is cleared from the beginning until some item. Does trimToSize() also work if from is any index inside the list (not only the first one)?
“clear” is relocating objects in the underlying native array (an Object[]), but it doesn’t resize the array. If you want reduce the array size after removing some items in the ArrayList, use trimToSize() method.
Unused element references of the array are set to null, so the elements could be garbage collected.