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

The Archive Base Latest Questions

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

I admit I have no deep understanding of D at this point, my knowledge

  • 0

I admit I have no deep understanding of D at this point, my knowledge relies purely on what documentation I have read and the few examples I have tried.

In C++ you could rely on the RAII idiom to call the destructor of objects on exiting their local scope.

Can you in D?

I understand D is a garbage collected language, and that it also supports RAII.
Why does the following code not cleanup the memory as it leaves a scope then?

import std.stdio;

void main() {
      {
            const int len = 1000 * 1000 * 256; // ~1GiB

            int[] arr;
            arr.length = len;
            arr[] = 99;
      }

      while (true) {}
}

The infinite loop is there so as to keep the program open to make residual memory allocations easy visible.

A comparison of a equivalent same program in C++ is shown below.
C++ v D

It can be seen that C++ immediately cleaned up the memory after allocation (the refresh rate makes it appear as if less memory was allocated), whereas D kept it even though it had left scope.

Therefore, when does the GC cleanup?

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

    No, you cannot assume that the garbage collector will collect your object at any point in time.

    There is, however, a delete keyword (as well as a scope keyword) that can delete an object deterministically.

    scope is used like:

    {
        scope auto obj = new int[5];
        //....
    } //obj cleaned up here
    

    and delete is used like in C++ (there’s no [] notation for delete).

    There are some gotcha’s, though:

    • It doesn’t always work properly (I hear it doesn’t work well with arrays)

    • The developers of D (e.g. Andrei) are intending to remove them in later versions, because it can obviously mess up things if used incorrectly. (I personally hate this, given that it’s so easy to screw things up anyway, but they’re sticking with removing it, and I don’t think people can convince them otherwise although I’d love it if that was the case.)

    • In its place, there is already a clear method that you can use, like arr.clear(); however, I’m not quite sure what it exactly does yet myself, but you could look at the source code in object.d in the D runtime if you’re interested.


    As to your amazement: I’m glad you’re amazed, but it shouldn’t be really surprising considering that they’re both native code. 🙂

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

Sidebar

Related Questions

I have to admit that having not even tried to code this myself this
I've seen this and I have to admit I'm pretty impressed: (source: blogspot.com )
This is my first post and I have to admit, I am terrible at
I've been doing project Euler for a few days, and I have to admit
I have this simple script that I'm working on. I must admit, I'm totally
I will have to admit the title of this question sucks... I couldn't get
I fully admit must have a faulty understanding of the construction of an associative
this is the code that i have but i have to admit i have
Okay, so I will admit I have no knowledge of windows API or even
I have to admit, this one has me foxed. Consider this code: use version;

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.