General Problems for GC'ed Applications?

Karen Lanrap karen at digitaldaemon.com
Fri Jul 28 15:56:05 PDT 2006


Walter Bright wrote:

> I think you omitted the code.

Sorry, did not want to send that out with all those mistakes.

Anyway, here ist the code:

static this(){
    writefln("This code demonstrates some behaviour of the GC");
    writefln("Author: Karen Lanrap");
    writefln("License: public domain");
}
void usage(char[] name){
    writefln("usage: %s alloc vital ([-lg] | [-l] [-g]) ", 
getBaseName(name));
    writefln("  alloc: size of all allocated memory");
    writefln("  vital: size of vital memory");
    writefln("  -l: leak memory");
    writefln("  -g: use GC");
}

const uint chunksize = 1_000_000;
const uint chainsize = 4;

class Hold{
    byte[chunksize / chainsize] data;
    Hold[chainsize] next;
}
Hold[] h;
Hold strongConnect(){
    for(int i=0; i< chainsize; i++) h[i] = new Hold;
    for(int i=0; i< chainsize; i++)
        for(int j = 0; j < chainsize; j++)
            h[i].next[j] = h[j];
    return h[rnd(0,chainsize)];
}

void main(char[][] args)
{
    h.length = chainsize;
    if(args.length < 3) usage(args[0]);
    assert(args.length > 2, "needs sizes of all and vital data");
    auto all = atoi(args[1]);
    auto vital = atoi(args[2]);
    assert(all>= vital, "vital data cannot be greater than all 
data");
    args.length = 5;
    auto leak = args[3]== "-l" || args[4]== "-l"|| args[3]== "-lg";
    auto useGC = args[3]== "-g" || args[4]== "-g"|| args[3]== "-
lg";

    if(!useGC)
    {
        std.gc.disable();
    };
    fwritef(stderr, "initializing... ");
    Hold[] arr;
    arr.length = all;
    for(int i = 1; i < all; i++)
        arr[i] = strongConnect();
    fwritefln(stderr, "done.");
    do
    {
        fwritef(stderr, "[");
        for(int v = 0; v< vital; v++){
            uint inx = rnd(all-vital, vital);
            if(!leak)
            { // delete the strongConnect in arr[inx]
                for(int i=0 ; i< chainsize; i++)
                    if(arr[inx] != arr[inx].next[i])
                        delete arr[inx].next[i];
                delete arr[inx];
            }
            arr[inx] = strongConnect();
            fwritef(stderr, ".");
        }
        fwritef(stderr, "]");
    } while(true);
}
uint rnd(uint base, uint range)
{
    return
        cast(uint)(
            base
            + (1.0*range*rand()) / (uint.max+1.0)
        );
}
import std.gc, std.random, gcstats;
import std.stdio, std.string, std.outofmemory, std.conv, std.path; 



More information about the Digitalmars-d mailing list