Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

Java ArrayList trimToSize() Method

Posted on July 24, 2025July 24, 2025 By Admin No Comments on Java ArrayList trimToSize() Method

The trimToSize() method is an important feature in Java’s ArrayList class that helps optimize memory usage by reducing the internal capacity of the list to match exactly the number of elements it contains. This is especially useful when you have finished adding elements to the list and want to minimize the memory footprint.

What Is the trimToSize() Method in ArrayList?

  • The trimToSize() method trims the capacity of the ArrayList instance to be equal to the current size of the list.
  • It frees any unused allocated space beyond what is necessary to store the elements.
  • This method does not affect the number of elements in the list — only the internal storage capacity.

Syntax

public void trimToSize()

Parameters

  • The method does not take any parameters.

Return Value

  • void: The method does not return any value.

Exceptions

  • The method does not throw any exceptions in normal use.

How Does trimToSize() Work Internally?

  • Internally, ArrayList uses an array to store its elements.
  • When more elements are added than its current capacity, this backing array is expanded (usually by a factor of 1.5).
  • trimToSize() resizes this array down to exactly the current list size, removing any unused space.
  • This helps reduce memory consumption, especially if the list will no longer grow.

Examples of the trimToSize() Method

1. Trim Capacity of an ArrayList After Adding Elements

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {

        // Initial capacity is 50
        ArrayList<String> cars = new ArrayList<>(50);
        cars.add("Volvo");
        cars.add("BMW");
        cars.add("Ford");
        cars.add("Mazda");

        System.out.println("Before trim: Size = " + cars.size());

        cars.trimToSize(); // Trims the capacity to current size (4)

        System.out.println("After trim: Size = " + cars.size());
        System.out.println(cars);
    }
}

Output:

Before trim: Size = 4
After trim: Size = 4
[Volvo, BMW, Ford, Mazda]

Note: The size() remains 4 before and after trimming; trimming affects the internal capacity, not the visible size.

2. Memory Optimization with trimToSize() method

import java.util.ArrayList;

public class MemoryExample {
    public static void main(String[] args) {

        // Large initial capacity
        ArrayList<Integer> numbers = new ArrayList<>(100);
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);

        // Trim excess capacity to minimize memory usage
        numbers.trimToSize();

        System.out.println("Size of list: " + numbers.size()); // Output: 3
        // Capacity has been reduced internally
    }
}

Important Notes

  • The default initial capacity of ArrayList is 10, but can be set via the constructor.
  • As elements are added beyond capacity, internal resizing happens automatically.
  • You call trimToSize() after bulk additions when you know you won’t add more elements to free unused space.
  • trimToSize() does not affect your ability to add new elements later, but new additions might trigger another resize.
  • This method is useful for memory-sensitive applications where minimizing heap usage matters.

Summary

AspectDetails
Method PurposeTrim internal capacity to current list size
SyntaxarrayList.trimToSize()
Return ValueNone (void)
ParametersNone
Effect on SizeNone; only capacity reduces
Typical Use CasesMemory optimization after bulk insertions
ExceptionsNone

Typical Use Cases

  • Reducing memory footprint after loading or modifying data.
  • Optimizing applications with many dynamically sized ArrayLists.
  • Performance tuning in large-scale Java applications.
Java Tags:Java-ArrayList, Java-ArrayList-Method, Java-Collections

Post navigation

Previous Post: Java ArrayList toArray() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Java ArrayList
  • Java ArrayList Methods
  • Java ArrayList add() Method
  • Java ArrayList addAll() Method
  • Java ArrayList clear() Method
  • Java ArrayList clone() Method
  • Java ArrayList contains() Method
  • Java ArrayList ensureCapacity() Method
  • Java ArrayList forEach() Method
  • Java ArrayList get() Method
  • Java ArrayList indexOf() Method
  • Java ArrayList isEmpty() Method
  • Java ArrayList iterator() Method
  • Java ArrayList lastIndexOf() Method
  • Java ArrayList listIterator() Method
  • Java ArrayList remove() Method
  • Java ArrayList removeAll() Method
  • Java ArrayList removeIf() Method
  • Java ArrayList replaceAll() Method
  • Java ArrayList retainAll() Method
  • Java ArrayList set() Method
  • Java ArrayList size() Method
  • Java ArrayList sort() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList subList() Method
  • Java ArrayList toArray() Method
  • Java ArrayList trimToSize() Method

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme