C# Queue.TrimExcess() Method with Examples

The Queue.TrimExcess() method in C# is used to set the capacity to the actual number of elements in the Queue<T>, if that number is less than 90 percent of current capacity. This method helps optimize memory usage by reducing the internal array size when the queue has significantly fewer elements than its current capacity.

Syntax

public void TrimExcess();

Parameters: This method takes no parameters.

Return Value: This method does not return any value (void).

How It Works

The TrimExcess() method only reduces capacity when the current element count is less than 90% of the capacity. If the queue has 90% or more elements relative to its capacity, the method does nothing. This prevents frequent resizing operations that could impact performance.

Queue Capacity vs Elements Before TrimExcess() 5 elements unused Capacity: 16, Count: 5 (31%) After TrimExcess() 5 elements Capacity: 5, Count: 5 (100%) TrimExcess() reduces capacity when elements < 90% of capacity Memory optimization: 16 ? 5 capacity

Using TrimExcess() After Clearing Elements

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Queue<int> queue = new Queue<int>();
      queue.Enqueue(100);
      queue.Enqueue(200);
      queue.Enqueue(300);
      queue.Enqueue(400);
      queue.Enqueue(500);
      queue.Enqueue(600);
      queue.Enqueue(700);
      queue.Enqueue(800);
      queue.Enqueue(900);
      queue.Enqueue(1000);
      Console.WriteLine("Queue...");
      foreach(int i in queue) {
         Console.WriteLine(i);
      }
      Console.WriteLine("Count of elements in the Queue = " + queue.Count);
      queue.Clear();
      queue.TrimExcess();
      Console.WriteLine("Count of elements in the Queue [Updated] = " + queue.Count);
   }
}

The output of the above code is −

Queue...
100
200
300
400
500
600
700
800
900
1000
Count of elements in the Queue = 10
Count of elements in the Queue [Updated] = 0

Using TrimExcess() with String Queue

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Queue<string> queue = new Queue<string>();
      queue.Enqueue("Gary");
      queue.Enqueue("Jack");
      queue.Enqueue("Ryan");
      queue.Enqueue("Kevin");
      queue.Enqueue("Mark");
      queue.Enqueue("Jack");
      queue.Enqueue("Ryan");
      queue.Enqueue("Kevin");
      Console.Write("Count of elements = ");
      Console.WriteLine(queue.Count);
      Console.WriteLine("Does the queue has element Jack? = " + queue.Contains("Jack"));
      queue.TrimExcess();
      queue.Clear();
      Console.Write("Count of elements (updated) = ");
      Console.WriteLine(queue.Count);
   }
}

The output of the above code is −

Count of elements = 8
Does the queue has element Jack? = True
Count of elements (updated) = 0

Practical Memory Optimization Example

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Queue<int> queue = new Queue<int>(50); // Initial capacity of 50
      
      // Add only 5 elements
      for(int i = 1; i <= 5; i++) {
         queue.Enqueue(i * 10);
      }
      
      Console.WriteLine("Elements in queue:");
      foreach(int val in queue) {
         Console.Write(val + " ");
      }
      Console.WriteLine();
      Console.WriteLine("Count: " + queue.Count);
      
      // TrimExcess will reduce capacity since 5 < 90% of 50 (45)
      queue.TrimExcess();
      Console.WriteLine("After TrimExcess() - Count remains: " + queue.Count);
      Console.WriteLine("Memory optimized: capacity reduced to match element count");
   }
}

The output of the above code is −

Elements in queue:
10 20 30 40 50 
Count: 5
After TrimExcess() - Count remains: 5
Memory optimized: capacity reduced to match element count

Common Use Cases

  • After bulk removals: When you've dequeued many elements and want to free unused memory.

  • After Clear(): When you've cleared the queue but plan to reuse it with fewer elements.

  • Memory-sensitive applications: When optimizing memory usage is important for performance.

Conclusion

The Queue.TrimExcess() method optimizes memory usage by reducing the internal capacity to match the actual number of elements, but only when the element count is less than 90% of the current capacity. This method is particularly useful after clearing elements or in memory-sensitive applications where reducing unused capacity is beneficial.

Updated on: 2026-03-17T07:04:36+05:30

368 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements