Slices and GC

Vladimir Panteleev vladimir at thecybershadow.net
Thu Apr 5 08:30:44 PDT 2012


On Thursday, 5 April 2012 at 15:00:04 UTC, BLM wrote:
> Recently I've been working on some projects that involve 
> parsing binary files. I've mainly been using std.file.read() to 
> get the whole file as a huge array and then extracting slices. 
> I had initially assumed that the GC would free any chunks of 
> the array that didn't end up being referenced by these slices, 
> but after reading some more, it looks like the whole array is 
> kept in memory even if only a few elements are actually 
> referenced. Is this actually the case? If so, might the 
> language be extended to handle this situation?

The GC can't really know which parts of the array you're using. 
For example, your only reference to the array might be a pointer, 
and you might be traversing the array in either direction, only 
keeping count of the remaining bytes until the array boundary.

Consider .dup-ing the slices you're going to need, or using 
std.mmfile to map the file into memory - in that case, the OS 
won't load the unnecessary parts of the file into memory in the 
first place.


More information about the Digitalmars-d mailing list