Shrink the capacity of a list to match its current size precisely.
import public |
The trimToSize() method adjusts the capacity of a list to exactly match the number of items it contains. While this method doesn’t have an immediate visible effect, it can be used to reduce the list’s memory usage.
When an ArrayList is created, it reserves capacity for 10 items by default unless a different number is specified in the constructor. Even if the list contains fewer than 10 items, this space remains reserved. Removing items from the list may still leave their space reserved. This can lead to wasted memory, especially if your program uses many ArrayLists. To reclaim this unused memory, you can use the trimToSize() method.
public |