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 117727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:22:55+00:00 2026-05-11T03:22:55+00:00

The general advice is that you should not call GC.Collect from your code, but

  • 0

The general advice is that you should not call GC.Collect from your code, but what are the exceptions to this rule?

I can only think of a few very specific cases where it may make sense to force a garbage collection.

One example that springs to mind is a service, that wakes up at intervals, performs some task, and then sleeps for a long time. In this case, it may be a good idea to force a collect to prevent the soon-to-be-idle process from holding on to more memory than needed.

Are there any other cases where it is acceptable to call GC.Collect?

  • 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-11T03:22:56+00:00Added an answer on May 11, 2026 at 3:22 am

    If you have good reason to believe that a significant set of objects – particularly those you suspect to be in generations 1 and 2 – are now eligible for garbage collection, and that now would be an appropriate time to collect in terms of the small performance hit.

    A good example of this is if you’ve just closed a large form. You know that all the UI controls can now be garbage collected, and a very short pause as the form is closed probably won’t be noticeable to the user.

    UPDATE 2.7.2018

    As of .NET 4.5 – there is GCLatencyMode.LowLatency and GCLatencyMode.SustainedLowLatency. When entering and leaving either of these modes, it is recommended that you force a full GC with GC.Collect(2, GCCollectionMode.Forced).

    As of .NET 4.6 – there is the GC.TryStartNoGCRegion method (used to set the read-only value GCLatencyMode.NoGCRegion). This can itself, perform a full blocking garbage collection in an attempt to free enough memory, but given we are disallowing GC for a period, I would argue it is also a good idea to perform full GC before and after.

    Source: Microsoft engineer Ben Watson’s: Writing High-Performance .NET Code, 2nd Ed. 2018.

    See:

    • https://msdn.microsoft.com/en-us/library/system.runtime.gclatencymode(v=vs.110).aspx
    • https://msdn.microsoft.com/en-us/library/dn906204(v=vs.110).aspx
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I've seen this question , but I'm looking for some more general advice:
Background With respect to cryptography in general, the following advice is so common that
can i have some general advice on converting a classic asp site to asp.net?
For general protocol message exchange, which can tolerate some packet loss. How much more
In general I think I can convey most programming related concepts quite well. Yet,
I have a simple polygon (convex or concave, but no holes) that I need
I'm wanting a non-reference counted smart pointer that can combine some of the useful
Though the question is very specific, I'd also really appreciate general advice and other
(Please no advise that I should abstract X more and add another method to
I need some specific and some general advice. I'm a fairly proficient Java programmer

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.