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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:18:20+00:00 2026-05-21T12:18:20+00:00

Objects with no more references are not immediately garbage collectable with GC.collect(), however an

  • 0

Objects with no more references are not immediately garbage collectable with GC.collect(), however an intermediate call of e.g. new, writeln or Thread.sleep will make the unreferenced object reachable with GC.collect().

import std.stdio;
import core.thread;
import core.memory;

class C
{
    string m_str;
    this(string s) {this.m_str = s;}
    ~this() { writeln("Destructor: ",this.m_str); }
}

void main()
{
    {
        C c1 = new C("c1");
    }   
    {
        C c2 = new C("c2");
    }
    //writeln("Adding this writeln means c2 gets destructed at first GC.collect.");
    //Thread.sleep( 1 ); // Similarly this call means c2 gets destructed at first GC.collect.
    //int x=0; for (int i=0; i<1000_000_000;++i) x+=2*i; // Takes time, but does not make c2 get destructed at first GC.collect.
    GC.collect();
    writeln("Running second round GC.collect");
    GC.collect();
    writeln("Exiting...");
}

The above code returns:

Destructor: c1
Running second round GC.collect
Destructor: c2
Exiting…

Can anyone explain this reachability of objects during garbage collection?

  • 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-21T12:18:21+00:00Added an answer on May 21, 2026 at 12:18 pm

    I’m not familiar with the details of D’s garbage collection, but the general technique is to start from all “root pointers” to figure out which objects are live. When things are compiled down to machine code, this means starting from the function call stack and the CPU registers.

    The code above might compile down to something like:

    $r0 = new C("c1")
    $r0 = new C("c2")
    GC.collect()
    writeln("Running second round GC.collect")
    GC.collect()
    writeln("Exiting...")
    

    When the first GC.collect() there are no more references to the first object (since $r0 was overwritten). And even though the second object isn’t used, there’s still a pointer to it in $r0, so the GC conservatively assumes it’s reachable. Note that the compiler could conceivably clear out $r0 after variable c2 goes out of scope, but this would make the code run slower.

    When the first writeln call executes, it probably uses register $r0 internally and so it clears the reference to the second object. That’s why the second object is reclaimed after the second call to GC.collect().

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

Sidebar

Related Questions

Why can't Delphi variants hold objects? More importantly, what's the reason behind this limitation?
Yacc does not permit objects to be passed around. Because the %union can only
--Edit with more bgnd information-- A (black box) COM object returns me a string.
Does anyone have an example using the table object in YUI library. More specifically,
Business Objects Web Services returns error codes and I have yet to find a
When objects from a CallList intersect the near plane I get a flicker..., what
All objects in my program inherit from a Container class. The Container class has
Many people use Mock Objects when they are writing unit tests. What is a
I have two DateTime objects: StartDate and EndDate . I want to make sure
Assume my objects are in perfect working order (i.e. TDD makes me think they

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.