title: "Out of memory while expanding memory stream"
body: <p>I need to read files directly from drive without any system buffering. I tried this code here <a href="https://stackoverflow.com/questions/983931/how-to-unload-a-file-from-cache">How to unload a file from cache?</a> unfortunately I get error message saying "Out of memory while expanding memory stream". It happens with any file I tried. Later I've noticed that SrcStream.Size returns always value -1. So obviously problem is here and the question is why is this happening?</p>
  
  <pre><code>uses
    MMSystem;
  
  function GetTimeForRead(ABuffered: boolean): single;
  const
    FileToRead = // name of file with maybe 500 MByte size
  var
    FlagsAndAttributes: DWORD;
    FileHandle: THandle;
    SrcStream, DestStream: TStream;
    Ticks: DWord;
  begin
    if ABuffered then FlagsAndAttributes := FILE_ATTRIBUTE_NORMAL
    else FlagsAndAttributes := FILE_FLAG_NO_BUFFERING;
    FileHandle := CreateFile(FileToRead, GENERIC_READ, FILE_SHARE_READ, nil,OPEN_EXISTING, FlagsAndAttributes, 0);
   if FileHandle = INVALID_HANDLE_VALUE then 
   begin
     Result := 0.0;
     exit;
   end;
  
   SrcStream := THandleStream.Create(FileHandle);
   try
     DestStream := TMemoryStream.Create;
     try
     DestStream.Size := SrcStream.Size;
  
     Ticks := timeGetTime;
     DestStream.CopyFrom(SrcStream, SrcStream.Size);
     Result := 0.001 * (timeGetTime - Ticks);
  
   finally
    DestStream.Free;
   end;
    finally
      SrcStream.Free;
    end;
  end;
  </code></pre>