Minimize GC memory footprint

rikki cattermole rikki at cattermole.co.nz
Sat Feb 6 09:42:38 UTC 2021


On 06/02/2021 3:32 PM, frame wrote:
> 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;

This won't do anything.

>          }
> 
>          foreach (i; 0 .. 50_000_00) {
>              s ~= "a";
>          }
>      }
> 
>      foreach (i; 0 .. uint.max) {
>          writefln("Round: %d", i + 1);

Don't forget to stdout.flush; Otherwise stuff can get caught in the 
buffer before erroring out.

>          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

Turn on the precise GC, 32bit is a bit too small of a range and you can 
get false positives like in this case (at least looks like it).


More information about the Digitalmars-d-learn mailing list