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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:19:46+00:00 2026-05-17T01:19:46+00:00

Here is a great article about GC may occur at unexpected point of code

  • 0

Here is a great article about GC may occur at unexpected point of code execution:

Lifetime, GC.KeepAlive, handle recycling – by cbrumme http://blogs.msdn.com/b/cbrumme/archive/2003/04/19/51365.aspx?wa=wsignin1.0

My question is how can I reproduce forced GC at the point mentioned in the article? I tried to put GC.Collect() at beginning of OperateOnHandle(), and defined destructor for class C but seems not working. Destructor is invoked always at end of program.

@Jon, thanks for advising me running out of debugger. Now I am able to reproduce the issue described in the article, using optimised Release build out of debugger. It proves this GC behaviour doesn’t change since .NET v1.

Code I used:

class C1
{
    // Some unmanaged resource handle
    IntPtr _handle = IntPtr.Zero;

    static void OperateOnHandle(IntPtr h)
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();

        Console.WriteLine("After GC.Collect() call");

        // Use the IntPtr here. Oops, invalid operation
    }

    public void m()
    {
        OperateOnHandle(_handle);
    }

    ~C1()
    {
        // Release and destroy IntPtr here
        Console.WriteLine("In destructor");
    }
}

class Program
{
    static void Main(string[] args)
    {
        C1 aC = new C1();
        aC.m();
    }
}

Output:

In destructor
After GC.Collect() call

  • 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-05-17T01:19:47+00:00Added an answer on May 17, 2026 at 1:19 am

    Bear in mind that that article is from 2003, which was using CLR v1. We’re now on CLR v4 (although there was no v3) so I’m not entirely surprised you don’t see exactly the same behaviour.

    Currently I can’t even get to the linked page, and you haven’t included a description of what the page describes. Is it the possibility of an object being garbage collected before the end of an instance method?

    If so, the most obvious reason you might have problems reproducing it is if you’re using the debugger. The garbage collector is much more aggressive when you’re running without a debugger attached.

    Here’s a short but complete program which demonstrates the issue when running not in a debugger:

    using System;
    
    class ClassWithFinalizer
    {
        private int value;
    
        public ClassWithFinalizer(int value)
        {
            this.value = value;
        }
    
        ~ClassWithFinalizer()
        {
            Console.WriteLine("Finalizer running!");
        }
    
        public void ShowValue()
        {
            Console.WriteLine(value);
            Console.WriteLine("Calling GC.Collect()");
            GC.Collect();
            Console.WriteLine("Calling GC.WaitForPendingFinalizers()");
            GC.WaitForPendingFinalizers();
            Console.WriteLine("End of method");
        }
    }
    
    class Test
    {
        static void Main()
        {
            var x = new ClassWithFinalizer(10);
            x.ShowValue();
        }
    }
    

    Compilation (optimized and without debug symbols, just to give it the best chance!):

    csc /o+ /debug- Test.cs
    

    Now run, with output:

    c:\users\Jon\Test>test
    10
    Calling GC.Collect()
    Calling GC.WaitForPendingFinalizers()
    Finalizer running!
    End of method
    

    Note how the finalizer runs before the method has completed.

    Tested with .NET 4 and .NET 3.5.

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

Sidebar

Related Questions

Gaffer on Games has a great article about using RK4 integration for better game
According to this great article about HTTP uploads by Scott Hanselman, the browser typically
Someone posted a great little function here the other day that separated the full
Here's my PS1 variable: PS1='\u:\W$(__git_ps1 \e[32m\][%s]\e[0m\])$ ' Works great for picking up my Git
I've got 256 colors working great in my terminal ( test scripts here ),
I asked a question earlier which elicited some great responses. Here's the earlier question
Python, through it's readline bindings allows for great command-line autocompletion (as described in here
I recently ran across this great article by Chad Parry entitled DIY-DI or Do-It-Yourself
After reading the tips from this great Nettuts+ article I've come up with a
I discovered the FizzBuzz question today at coding horror . Great article. However, something

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.