Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
queue
trimexcess
communitycreator

How to update C# queue size based on the current number of items

Surabhi Sharma

The Queue<T> generic class in the System.Collections.Generic namespace provides the TrimExcess() method, which can be used to set the capacity of an existing queue to the actual number of elements present in the queue, if the number of elements is less than 90 percent of current capacity of the queue.

The queue maintains an internal array to store the data with an initial default size of four items. This array is doubled every time the number of elements inserted in the queue reaches the capacity.

When we call TrimExcess(), the internal array is replaced by a new array with size as the current number of elements in the queue and the existing elements are copied to this new array. The previously allocated array is automatically garbage collected.

For example, if the queue’s capacity is 100 and it currently has 95 elements, calling the TrimExcess() method will not do anything.

However, if the queue has 70 elements, its capacity will be updated to 70 after the TrimExcess() method is called.

Syntax

public void TrimExcess ();

Things to note

  • If we are sure that no new elements will be added to the queue, we can use this method to minimize the queue’s memory overhead.

  • If the queue is already near capacitynumber of elements is more than 90 percent of capacity, the cost to reallocate and copy a large queue can be expensive. Therefore, TrimExcess() does nothing if the queue is at more than 90 percent of capacity.

  • This is an O(n) operation, where n is the Count of elements in the queue.

Setting Queue capacity to number of elements using TrimExcess()

Example

In this example, we created a queue of strings with the initial capacity of one hundred elements and have added the names of the first six months to it. We print all the elements of the queue.

We then call the TrimExcess() method which resets the queue’s capacity to the current number of elements, which is six. However, we can access the queue’s capacity, as the internal array used by the the queue to store data is a private member, and its length property is not accessible.

It should be noted that this method is used to reduce the memory overhead of large existing collections, and should be called only when we are sure that no new elements will be added to it.

using System;
using System.Collections.Generic;

class QueueResizer
{
    static void Main()
    {
        //queue is created with space to hold 100 strings.
        Queue<string> months = new Queue<string>(100);
        months.Enqueue("January");
        months.Enqueue("February");
        months.Enqueue("March");
        months.Enqueue("April");
        months.Enqueue("May");
        months.Enqueue("June");
        //current queue capacity : 100 
        Console.WriteLine("Queue Items : {0}", string.Join(",", months.ToArray()));
        months.TrimExcess();
        //updated queue capacity : 6
        //queue capacity is reduced from 100 to the number of elements in queue,  that is six.
    }
}
Set queue capacity to the current number of elements with TrimExcess()

RELATED TAGS

c#
queue
trimexcess
communitycreator
RELATED COURSES

View all Courses

Keep Exploring