Skip to content

Instantly share code, notes, and snippets.

@cbecerescu
Last active April 21, 2020 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbecerescu/e6606a8530c56ae06c52e5b1cd32b31f to your computer and use it in GitHub Desktop.
Save cbecerescu/e6606a8530c56ae06c52e5b1cd32b31f to your computer and use it in GitHub Desktop.

Test code

import std.stdio;
import core.memory;

void printGCStats()
{
    auto sts = GC.stats();
    writefln("\n==\nGC Stats\n");
    writefln("GC.allocatedInCurrentThread = %s", sts.allocatedInCurrentThread);
    writefln("GC.freeSize = %s", sts.freeSize);
    writefln("GC.usedSize = %s\n==\n", sts.usedSize);
}

void printGCProfileStats()
{
    auto psts = GC.profileStats();
    writefln("\n==\nGC ProfileStats\n");
    writefln("GC.numCollections = %s", psts.numCollections);
    writefln("GC.totalCollectionTime = %s", psts.totalCollectionTime);
    writefln("GC.totalPauseTime = %s", psts.totalPauseTime);
    writefln("GC.maxPauseTime = %s", psts.maxPauseTime);
    writefln("GC.maxCollectionTime = %s", psts.maxCollectionTime);
}

void main(string[] args)
{
    int[] v = [1, 2];
    int n = 1 << 30;
    printGCStats();
    printGCProfileStats();
    for (int i = 0; i < n - 2; ++i)
    {
        v ~= i;
    }
    writefln("v len: %s cap: %s\n", v.length, v.capacity);
    printGCStats();
    printGCProfileStats();
    GC.collect();
    printGCStats();
    printGCProfileStats();
}

Output

Before collect()
==
GC Stats
GC.allocatedInCurrentThread = 7329336480
GC.freeSize = 690925360
GC.usedSize = 7528243408
==
==
GC ProfileStats
GC.numCollections = 12
GC.totalCollectionTime = 1 ms, 50 μs, and 5 hnsecs
GC.totalPauseTime = 901 μs and 8 hnsecs
GC.maxPauseTime = 352 μs and 2 hnsecs
GC.maxCollectionTime = 352 μs and 6 hnsecs


After collect()
==
GC Stats
GC.allocatedInCurrentThread = 7329336672
GC.freeSize = 3462635488
GC.usedSize = 4756533280
==
==
GC ProfileStats
GC.numCollections = 13
GC.totalCollectionTime = 1 ms, 154 μs, and 9 hnsecs
GC.totalPauseTime = 981 μs and 8 hnsecs
GC.maxPauseTime = 352 μs and 2 hnsecs
GC.maxCollectionTime = 352 μs and 6 hnsecs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment