On 2014-05-17 12:46, sdvcn wrote:
int main(string[] argv)
{
auto flog = File("results.txt", "w");

     size_t[string] bary;

     for(size_t i=0;i<(size_t.max -1);i++)
     {
         bary["Key:" ~  to!(string)(i)] = i;
         flog.write("stop i=" ~text(i));
         flog.seek(0);
         flog.flush();
     }
     return 0;
}

results:
start i=0
stop i=36495998
---------------
start i=0
stop i=36495992
----------------
start i=36495998
stop i=72991099

I guess not see why Overflow.


This code will always make you run out of memory. Why are you surprised?

Each key in the hash table is a string in the form "Key: 1234", so at stop (i = 
36495998) the string has 13 bytes. Add to that 16 bytes for slice of that string 
(assuming 64-bit architecture), 8 bytes for the value, some space wasted on alignment, 
and don't forget the extra memory needed to store the tree for fast key look-up in the 
hash array.

You said that you have 16 GB of memory. At i = 36495998 that means at most 470 
bytes per item.

As for capturing the problem, you can catch the Out-of-memory error but you 
cannot do that by catch(Exception e). OutOfMemory is not an Exception. It is an 
Error. Here is the updated example:


    import std.stdio, std.string, std.conv, core.exception;

    int main(string[] argv) {
        size_t[string] bary;
        size_t i = 0;
        try {
            for (; i < (size_t.max - 1); i++)
                bary["Key:" ~  to!(string)(i)] = i;
        } catch (OutOfMemoryError e) {
            writeln(e);
        }
        writefln("Last index was: %d", i);
        return 0;
    }


Reply via email to