How to free memory allocated via double[][] using dmd-2.0.12?

Bill Baxter dnewsgroup at billbaxter.com
Tue Apr 8 17:05:41 PDT 2008


Markus Dittirich wrote:
> Bill Baxter Wrote:
> 
>> Markus,  you do not show us what either read_data or process_data do. 
>> It is possible that one of those is somehow holding on to references to 
>> the data.  This would prevent the GC from collecting the memory.
>>
>> Another problem is if you allocate the memory initially as void[] then 
>> the GC will scan it for pointers, and in a big float buffer you'll get a 
>> lot of false hits.  To prevent that, allocate the buffer initially as 
>> byte[] (or double[] --- just not void).
>>
>> Anyway, if you want a speedy fix, you'll need to distill this down into 
>> something that is actually reproducible by Walter.
>>
>> --bb
> 
> Hi Bill,
> 
> You're of course absolutely correct! Below is a proof of concept code
> that still exhibits the issue I was describing. The parse code needs 
> to handle row centric ascii data with a variable number of columns.
> The file "data_random.dat" contains a single row of random integers.
> After a few iterations the code runs out of memory on my machine
> and no deleting seems to help.
> 
> import std.stream;
> import std.stdio;
> import std.contracts;
> import std.gc;
> 
> 
> public double[][] parse(BufferedFile inputFile)
> {
> 
>   double[][] array;
>   foreach(char[] line; inputFile)
>   {
>     double[] temp;
> 
>     foreach(string item; std.string.split(assumeUnique(line)))
>     {
>        temp ~= std.string.atof(item);
>     }
> 
>     array ~= temp;
>   }
> 
>   /* rewind for next round */
>   inputFile.seekSet(0);
> 
>   return array;
> }
> 
> 
> 
> int main()
> {
>   BufferedFile inputFile = new BufferedFile("data_random.dat");
> 
>   while(1)
>   {
>     double[][] foo = parse(inputFile);
>   }
> 
>   return 1;
> }

Ok.  You should add that to the bug report.

However, that test program works fine for me on Windows.
I tried it with
   DMD/Phobos 1.028,
   DMD/Tango/Tangobos 1.028, and
   DMD/Phobos 2.012.

--bb


More information about the Digitalmars-d-learn mailing list