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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:40:19+00:00 2026-05-22T17:40:19+00:00

I have a loop variable that does not appear to be getting garbage collected

  • 0

I have a loop variable that does not appear to be getting garbage collected (according to Red–Gate ANTS memory profiler) despite having gone out of scope.

The code looks something like this:

while (true)
{
    var item = blockingQueue.dequeue(); // blocks until an item is added to blockingQueue
    // do something with item
}

As far as I can tell, a reference to item remains until blockingQueue.dequeue() returns. Is this intended behaviour, or could it be a bug in the memory profiler?

Secondly, if this is intended behaviour how would I force item to get collected at the end of the loop body? Setting it to null does not appear to cause it to get collected. This is important as the queue could potentially block for a long time and item references a fairly large object tree.

Note, the documentation of the profiler says that a GC is performed before taking a memory snapshot, and the reference is not on the finalizer queue.

I was able to reproduce the same problem with the code here.

Update

The code in the gist was slightly flawed in that it legitimately held on to a reference in GetFoo(). Having changed it the object does now get collected when explicitly set to null. However, I believe Hans’ answer explains the situation I’m seeing in my actual code.

  • 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-22T17:40:20+00:00Added an answer on May 22, 2026 at 5:40 pm

    The jitter optimizer is the likely source for this problem. Here’s an example:

    class Program {
        static void Main(string[] args) {
            while (true) {
                var input = Console.ReadLine();
                Console.WriteLine(input);
                input = null;
            }
        }
    }
    

    Generates this machine code:

                while (true) {
                    var input = Console.ReadLine();
    00000000  push        ebp                    ; setup stack
    00000001  mov         ebp,esp 
    00000003  push        esi  
    00000004  call        6E0208F0               ; Console.In property getter
    00000009  mov         ecx,eax 
    0000000b  mov         eax,dword ptr [ecx] 
    0000000d  call        dword ptr [eax+64h]    ; TextReader.ReadLine()
    00000010  mov         esi,eax                ; assign input variable
                    Console.WriteLine(input);
    00000012  call        6DB7BE38               ; Console.Out property getter
    00000017  mov         ecx,eax
    00000019  mov         edx,esi
    0000001b  mov         eax,dword ptr [ecx] 
    0000001d  call        dword ptr [eax+000000D8h] ; TextWriter.WriteLine()
    00000023  jmp         00000004               ; repeat, note the missing null assigment
    

    The esi register stores the input variable. Note how it is never set back to null, it always stores a reference to the last entered string. The optimizer has removed the null assignment statement. The garbage collector gets lifetime hints from the jitter, it will say that the reference is live for the duration of the loop.

    The problem occurs on the second and subsequent pass, when you never type something then ReadLine() will block (similar to your blocking queue) and the esi register value continues referencing the string. It will never be garbage collected for the duration of the loop, at least until it gets reassigned.

    There’s no clean fix for this. Here’s an ugly one:

        [MethodImpl(MethodImplOptions.NoInlining)]
        public static void NullReference<T>(ref T obj) where T : class {
            obj = null;
        }
    

    and use:

            while (true) {
                var input = Console.ReadLine();
                Console.WriteLine(input);
                NullReference(ref input);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a variable that is built in loop. Something like: $str = ;
I have always wondered if, in general, declaring a throw-away variable before a loop,
I have a loop that reads each line in a file using getline() :
I have a loop on page to update an access database that takes 15-20
In my WPF client, I have a loop that calls a WCF service to
I'm trying to solve the 3n+1 problem and I have a for loop that
I have a thread that, when its function exits its loop (the exit is
I have a query that I need to loop. query=select '$dbserver' as server; while
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
If I have a for loop which is nested within another, how can I

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.