Minimize GC memory footprint

frame frame86 at live.com
Sat Feb 6 02:32:04 UTC 2021


On Friday, 5 February 2021 at 22:46:05 UTC, Bastiaan Veelo wrote:

> ?? Do you mean no collections happen? 32bit GC should just work.

No, it doesn't - this code fails on memory allocation and works 
fine with -m64 switch:


import std.stdio;
import core.memory : GC;

void main() {

     void usage() {
         writefln("Usage: %.2f MiB / collected: %d", (cast(double) 
GC.stats.usedSize) / 1_048_576, GC.profileStats.numCollections);
     }

     void foo() {
         string[] s;

         scope (exit) {
             s.length = 0;
         }

         foreach (i; 0 .. 50_000_00) {
             s ~= "a";
         }
     }

     foreach (i; 0 .. uint.max) {
         writefln("Round: %d", i + 1);
         foo();
         GC.collect();
         usage();
     }
}

...
Round: 24
Usage: 1603.57 MiB / collected: 27
Round: 25
Usage: 1691.64 MiB / collected: 28
Round: 26
Usage: 1729.50 MiB / collected: 29
Round: 27

core.exception.OutOfMemoryError at src\core\exception.d(647): Memory 
allocation failed


More information about the Digitalmars-d-learn mailing list