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

  • SEARCH
  • Home
  • 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 8574045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:23:14+00:00 2026-06-11T19:23:14+00:00

C# variables are instantiated where the type is declared (eg string s; ) and

  • 0

C# variables are instantiated where the type is declared (eg string s;) and freed at the closing brace of the current scope:

// Operates with Q memory
void FantasyMethod() {
    var o = new BigObject();

    {
        var temp = new BigObject();
        Populate(temp); // Populates o1 with N megabytes of data

        o = PerformSomeOperationsOn(temp); // Returns a BigObject of size M (M is close to N)

        // Currently, M+N memory is occupied, we have Q-M-N free
    }

    // Let's tell the garbage collector to catch up
    GC.Collect();
    GC.WaitForPendingFinalizers();

    // Currently, M memory is occupied

    DoUsefulStuffWith(o); // This method can only work if at least Q-M-N/2 memory is free
}

One benefit of this is that I can free large variables before the function returns. In the above (trivial) block, I have husbanded my limited available memory by disposing a large variable as soon as it is no longer needed.

  1. Is the above correct?
  2. Is doing this a good idea (I am interested in arguments for and against, not personal opinion or preference)? Would extracting the naked brace block as a method use memory less efficiently? What if I don’t want to make a new method for readability reasons?
  • 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. Editorial Team
    Editorial Team
    2026-06-11T19:23:15+00:00Added an answer on June 11, 2026 at 7:23 pm

    One benefit of this is that I can free large variables before the function returns.

    No. C# isn’t C++, objects don’t have destructors, and you are not guaranteed that an object will be reclaimed the moment it leaves its declaring scope and no valid references to it exist.

    If you need that level of predictability then you shouldn’t be using a managed language, period. Techniques do exist which can help to alleviate memory pressure in C#, but they are not often needed and you will never get the level of control that a language like C or C++ will give you.

    Per your edit:

    GC.Collect will attempt to run a GC pass, it doesn’t guarantee it. GC.WaitForPendingFinalizers blocks until all objects which have been marked for finalization have run their finalizers.

    If an object implements a finalizer and has not disabled finalization by calling SuppressFinalize, the object is placed in a list of objects that are marked as ready for finalization. The garbage collector calls the Finalize methods for the objects in this list and removes the entries from the list. This method blocks until all finalizers have run to completion.

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

Sidebar

Related Questions

//assign variables string measurementIn; //read in file in array string[] lines = File.ReadAllLines(../../convert.txt); //ask
Are variables declared inside a static block accessible anywhere else? What kind of member
I have these variables: boost::regex re //regular expression to use std::string stringToChange //replace this
I have declared three variables namely TemplateData tData; TaskInstance tInstance;int tID; in my program.
If I declare a static field in a type instantiated within an ASP.NET application,
For example, a simple MVC type system: /api/class/method rewritten into PHP variables using .htaccess/nginx.conf
I have two variables in a jquery code and if item.a not exist, it
update: I mistyped 2 variables...so embarrassing. thanks everyone for the effort! sorry i find
Possible Duplicate: Global Variables in Cocoa/Objective-C? problem with declare a global variable in objective
I am using static variables in my app, lots of them. My question is,

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.