Javatpoint Logo
Javatpoint Logo

Stack.TrimExcess Method in C#

In this article, we will discuss the TrimExcess() in C# with its syntax and examples.

What is the Stack<T>.TrimExcess Method?

A stack is a linear data structure that performs operations in a specific sequence. The sequence might be LIFO (Last In, First Out) or FILO (First In, Last Out). LIFO denotes that the last element added comes out first, whereas FILO implies that the first element put comes out last.

In C#, the TrimExcess function is part of System.Collections. The generic namespace is related to the Stack<T> class. This function optimizes memory use of a Stack<T> instance by adjusting the amount of memory of the underlying array to be equal to the total amount of items in the stack if that number is fewer than a certain threshold. The major goal of this approach is to lower the stack's memory footprint once it has been significantly decreased in size via a sequence of push and pop operations.

The Stack<T>.TrimExcess Method is executed to set a limit for the actual number of items in the Queue<T>, if that amount is less than 90% of the existing capacity. A stack is an unlimited data structure, and in C#, there is no technique for calculating its capacity. It is dynamic and dependent on system memory. This strategy is often used in the memory management of huge stacks.

Stack Properties:

  • The capacity of a Stack refers to the amount of components it can hold. As pieces are added to a Stack, its capacity is dynamically raised as needed via reallocation.
  • If the count is fewer than the stack's capacity, Push is an O(1) operation. If the capacity must be expanded to handle the additional element, Push becomes an O(n) operation, where n represents Count. Pop is an O(1) operation.
  • Stack accepts null as a valid value and allows element duplication.

Syntax:

It has the following syntax:

Key Points To Remember:

  • If no new elements are added to the collection, this technique can be used to reduce its memory load.
  • In order to return a Stack<T> to its original state, use the Clear method before executing the TrimExcess function.
  • Trimming an empty Stack<T> resets its capacity to the default value.

Example:

Let us take an example to illustrate the Stack<T>TrimExcess() method in C#.

Output:

5
0

Explanation:

In this example, a Stack called mystk is initialized to contain string elements, and five elements are inserted using the Push function. After that, the program shows the stack's starting count of items, removes all elements using the Clear method, and then sets out to trim excess capacity with the TrimExcess method, which is not useful.

Finally, the program outputs the number of elements following its cleaning and cutting stages. It's crucial to note that the TrimExcess method is normally used with collections like List<T> to decrease unneeded capacity, but it has no practical effect on a Stack because it doesn't allocate excess memory beyond what's necessary to store its members.

Advantages of using TrimExcess method

Several advantages of TrimExcess() method are as follows:

  • Memory efficiency: TrimExcess allows us to trim the collection's internal capacity to match its real size. It is especially useful when the initial capacity was increased to meet future expansion but that development did not occur.
  • Optimized Resource allocation: Reducing a collection's capacity might result in a lower memory footprint. It is significant in situations where memory utilization is a crucial concern, particularly in resource-constrained situations.
  • Optimized Resource Utilization: Reducing excess capacity reduces memory consumption and maximizes resource efficiency. It is particularly beneficial when memory allocation is constrained.
  • Improving Performance: In collections with dynamic resizing, excess capacity can slow down processes like resizing and copying pieces. Excess capacity can be reduced to enhance performance by preventing inefficient reallocations.
  • Predictable Resource consumption: The collection's memory consumption becomes more consistent and closely matched to its real content by removing an excess capacity. This predictability can be essential in situations involving critical resource management.
  • Improved Workload Modification: When a collection's size varies, TrimExcess' dynamic capacity adjustment makes it possible for the collection to change with workloads more effectively without retaining extra memory.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA