Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 131789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:09:38+00:00 2026-05-11T06:09:38+00:00

Garbage collection in .NET leads many to believe that lightweight objects can be treated

  • 0

Garbage collection in .NET leads many to believe that lightweight objects can be treated as temporary. This seems especially true of arrays that hold object references to objects that are instantiated outside of the context of the array initialization.

Consequently, it would seem that it should not really matter if a new array is initialized within an iteration of a state machine, particularly if that state blocks in any fashion, such as with a call to WaitHandle.WaitAny().

I would much rather create a new array as a lightweight container on-the-fly to hold my WaitHandle objects, than to resize the WaitHandle[] array using Array.Resize<T>().

Why? Well, assuming that memory is cheap and that Array.Resize<T>() will have to perform a memory allocation or a memory copy of some sort anyhow, it seems more efficient to simply implicitly discard the array by passing in a new one every time into the static method like this:

// using C# 3.0 array initialization syntax eventIndex = WaitHandle.WaitAny(new[] { _stateStopEvent }, 5000); 

(Check out this link for details on the array syntax)

_stateStopEvent is one of several potential events that may apply to any or all states, and those ManualResetEvent objects are declared at a higher scope, so it seems that managing the array is actually more work than it’s worth.

Furthermore, if the code is declaring a temporary reference variable for the array and simply assigning a new array instance to it within each iteration of the state machine, what’s the difference, from a performance perspective? Why not simply skip that step as shown in the code snippet above?

Is resizing the WaitHandle[] array by using Array.Resize<T>(), a better approach?

Taking byte[] arrays out of the picture, if I need to resize an array, wouldn’t I just use a List or a Dictionary instance as is recommended here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:09:39+00:00Added an answer on May 11, 2026 at 6:09 am

    This is what Array.Resize does:

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public static void Resize<T>(ref T[] array, int newSize) {     if (newSize < 0)     {         throw new ArgumentOutOfRangeException('newSize', Environment.GetResourceString('ArgumentOutOfRange_NeedNonNegNum'));     }     T[] sourceArray = array;     if (sourceArray == null)     {         array = new T[newSize];     }     else if (sourceArray.Length != newSize)     {         T[] destinationArray = new T[newSize];         Copy(sourceArray, 0, destinationArray, 0, (sourceArray.Length > newSize) ? newSize : sourceArray.Length);         array = destinationArray;     } } 

    So besides some range checking, it just creates a new array and copies the elements from the previous array to the new array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I'm understanding this correctly about garbage collection in .NET CLR, then GC occurs
How should I perform garbage collection in a program that consists of multiple threads
I'm not asking this question because of the merits of garbage collection first of
I keep hearing people complaining that C++ doesn't have garbage collection. I also hear
I was told by a rather smart person that you cannot implement garbage collection
I have an ASP.NET website that seems to be using a lot of memory.
I have simple question about .net garbage collection. In the following code I create
How does garbage collection work in JavaScript? Is it similar to .NET garbage collection?
In .Net 4 there is improved Garbage Collection which is important for our systems.
I'm concerned about garbage collection in the case where two objects reference each-other ...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.