Home All Groups Group Topic Archive Search About

Consuming less memory when writing a file?

Author
25 Nov 2005 9:43 AM
W. Jordan
Hello,

I am using Response.WriteFile (filename) to write a file that on
the server to my web client.

I discover that the while a client request a big file,
for example, a 14m-bytes one, the memory usage of the
w3wp.exe increases the same amount of 14m.

That might be horrible if I allow the users to download a 32m
or greater file. Is it possible to conserve a little memory while
writing files to the client.

--

Best Regards,
W. Jordan

Author
25 Nov 2005 10:10 AM
Daniel Fisher(lennybacon)
Tr something like


--
Daniel Fisher(lennybacon)
http://www.lennybacon.com


Show quoteHide quote
"W. Jordan" <wmjor***@163.com> wrote in message
news:eAxYAUa8FHA.1864@TK2MSFTNGP12.phx.gbl...
> Hello,
>
> I am using Response.WriteFile (filename) to write a file that on
> the server to my web client.
>
> I discover that the while a client request a big file,
> for example, a 14m-bytes one, the memory usage of the
> w3wp.exe increases the same amount of 14m.
>
> That might be horrible if I allow the users to download a 32m
> or greater file. Is it possible to conserve a little memory while
> writing files to the client.
>
> --
>
> Best Regards,
> W. Jordan
>
>
>
>
Are all your drivers up to date? click for free checkup

Author
25 Nov 2005 10:13 AM
Daniel Fisher(lennybacon)
Try something like the following (set this.m_bufferSize to the size you want
to allocate in memory):

context.Response.Buffer = false;
   FileStream _fileStream = null;
   byte[] _buffer = new byte[this.m_bufferSize];
   long _byteCount;
   try
   {
    _fileStream = File.OpenRead(_filePath);
    while ((_byteCount = _fileStream.Read(_buffer, 0, _buffer.Length)) > 0)
    {
     if(context.Response.IsClientConnected)
     {
      context.Response.OutputStream.Write(_buffer, 0, _buffer.Length);
      context.Response.Flush();
     }
     else
     {
      return;
     }
    }
   }
   catch(Exception _ex)
   {
    throw _ex;
   }
   finally
   {
    _fileStream.Close();
    context.Response.End();
   }


--
Daniel Fisher(lennybacon)
http://www.lennybacon.com


Show quoteHide quote
"Daniel Fisher(lennybacon)" <i***@lennybacon.com> wrote in message
news:OuRrfha8FHA.500@TK2MSFTNGP15.phx.gbl...
> Tr something like
>
>
> --
> Daniel Fisher(lennybacon)
> http://www.lennybacon.com
>
>
> "W. Jordan" <wmjor***@163.com> wrote in message
> news:eAxYAUa8FHA.1864@TK2MSFTNGP12.phx.gbl...
>> Hello,
>>
>> I am using Response.WriteFile (filename) to write a file that on
>> the server to my web client.
>>
>> I discover that the while a client request a big file,
>> for example, a 14m-bytes one, the memory usage of the
>> w3wp.exe increases the same amount of 14m.
>>
>> That might be horrible if I allow the users to download a 32m
>> or greater file. Is it possible to conserve a little memory while
>> writing files to the client.
>>
>> --
>>
>> Best Regards,
>> W. Jordan
>>
>>
>>
>>
>
>
Author
25 Nov 2005 10:12 AM
Daniel Fisher(lennybacon)
Try something like the following (set this.m_bufferSize to the size you want
to allocate in memory):

context.Response.Buffer = false;
   FileStream _fileStream = null;
   byte[] _buffer = new byte[this.m_bufferSize];
   long _byteCount;
   try
   {
    _fileStream = File.OpenRead(_filePath);
    while ((_byteCount = _fileStream.Read(_buffer, 0, _buffer.Length)) > 0)
    {
     if(context.Response.IsClientConnected)
     {
      context.Response.OutputStream.Write(_buffer, 0, _buffer.Length);
      context.Response.Flush();
     }
     else
     {
      return;
     }
    }
   }
   catch(Exception _ex)
   {
    throw _ex;
   }
   finally
   {
    _fileStream.Close();
    context.Response.End();
   }


--
Daniel Fisher(lennybacon)
http://www.lennybacon.com


Show quoteHide quote
"W. Jordan" <wmjor***@163.com> wrote in message
news:eAxYAUa8FHA.1864@TK2MSFTNGP12.phx.gbl...
> Hello,
>
> I am using Response.WriteFile (filename) to write a file that on
> the server to my web client.
>
> I discover that the while a client request a big file,
> for example, a 14m-bytes one, the memory usage of the
> w3wp.exe increases the same amount of 14m.
>
> That might be horrible if I allow the users to download a 32m
> or greater file. Is it possible to conserve a little memory while
> writing files to the client.
>
> --
>
> Best Regards,
> W. Jordan
>
>
>
>
Author
28 Nov 2005 2:16 AM
W. Jordan
Hello,

I changed the code slightly to the following and monitored
the memory usage with the task manager.
I am sad to find that during the downloading, the memory
usage goes up still. The sample file used for download testing
was 14mb, and the memory consumption delta went up more than
twice of the file size.

Response.Clear ();
Response.Buffer = false;
Response.AppendHeader("Content-Type", "octet-stream");
Response.AppendHeader("Content-Disposition","attachment");
Response.Flush ();
int bufferSize = 8192;
byte[] buffer = new byte[bufferSize];
long byteCount;
using (FileStream fs = File.OpenRead (AppPath.MapPath (fileName)))
{
  while ((byteCount = fs.Read (buffer, 0, buffer.Length)) > 0)
  {
   if (Response.IsClientConnected)
   {
    Response.OutputStream.Write (buffer, 0, buffer.Length);
    Response.Flush ();
   }
   else
   {
    return ;
   }
  }
}


--

Best Regards,
W. Jordan




Show quoteHide quote
"Daniel Fisher(lennybacon)" <i***@lennybacon.com> wrote in message
news:OpPdZia8FHA.2176@TK2MSFTNGP14.phx.gbl...
> Try something like the following (set this.m_bufferSize to the size you
> want to allocate in memory):
>
> context.Response.Buffer = false;
>   FileStream _fileStream = null;
>   byte[] _buffer = new byte[this.m_bufferSize];
>   long _byteCount;
>   try
>   {
>    _fileStream = File.OpenRead(_filePath);
>    while ((_byteCount = _fileStream.Read(_buffer, 0, _buffer.Length)) > 0)
>    {
>     if(context.Response.IsClientConnected)
>     {
>      context.Response.OutputStream.Write(_buffer, 0, _buffer.Length);
>      context.Response.Flush();
>     }
>     else
>     {
>      return;
>     }
>    }
>   }
>   catch(Exception _ex)
>   {
>    throw _ex;
>   }
>   finally
>   {
>    _fileStream.Close();
>    context.Response.End();
>   }
>
>
> --
> Daniel Fisher(lennybacon)
> http://www.lennybacon.com
>
>
> "W. Jordan" <wmjor***@163.com> wrote in message
> news:eAxYAUa8FHA.1864@TK2MSFTNGP12.phx.gbl...
>> Hello,
>>
>> I am using Response.WriteFile (filename) to write a file that on
>> the server to my web client.
>>
>> I discover that the while a client request a big file,
>> for example, a 14m-bytes one, the memory usage of the
>> w3wp.exe increases the same amount of 14m.
>>
>> That might be horrible if I allow the users to download a 32m
>> or greater file. Is it possible to conserve a little memory while
>> writing files to the client.
>>
>> --
>>
>> Best Regards,
>> W. Jordan
>>
>>
>>
>>
>
>
Author
28 Nov 2005 2:27 AM
W. Jordan
Hi Danial,

I tried to change Response.Flush to Response.OutputStream.Flush.

The performance appears to be a little better, however, the
memory usage problem is still there. :P
Perhaps, there're still a huge buffer inside the FileStream.
Thus, the while loop will fill it up and thus the memory consumption
goes up accordingly.

--

Best Regards,
W. Jordan



Show quoteHide quote
"Daniel Fisher(lennybacon)" <i***@lennybacon.com> wrote in message
news:OpPdZia8FHA.2176@TK2MSFTNGP14.phx.gbl...
> Try something like the following (set this.m_bufferSize to the size you
> want to allocate in memory):
>
> context.Response.Buffer = false;
>   FileStream _fileStream = null;
>   byte[] _buffer = new byte[this.m_bufferSize];
>   long _byteCount;
>   try
>   {
>    _fileStream = File.OpenRead(_filePath);
>    while ((_byteCount = _fileStream.Read(_buffer, 0, _buffer.Length)) > 0)
>    {
>     if(context.Response.IsClientConnected)
>     {
>      context.Response.OutputStream.Write(_buffer, 0, _buffer.Length);
>      context.Response.Flush();
>     }
>     else
>     {
>      return;
>     }
>    }
>   }
>   catch(Exception _ex)
>   {
>    throw _ex;
>   }
>   finally
>   {
>    _fileStream.Close();
>    context.Response.End();
>   }
>
>
> --
> Daniel Fisher(lennybacon)
> http://www.lennybacon.com
>
>
> "W. Jordan" <wmjor***@163.com> wrote in message
> news:eAxYAUa8FHA.1864@TK2MSFTNGP12.phx.gbl...
>> Hello,
>>
>> I am using Response.WriteFile (filename) to write a file that on
>> the server to my web client.
>>
>> I discover that the while a client request a big file,
>> for example, a 14m-bytes one, the memory usage of the
>> w3wp.exe increases the same amount of 14m.
>>
>> That might be horrible if I allow the users to download a 32m
>> or greater file. Is it possible to conserve a little memory while
>> writing files to the client.
>>
>> --
>>
>> Best Regards,
>> W. Jordan
>>
>>
>>
>>
>
>
Author
25 Nov 2005 10:20 AM
John Timney ( MVP )
Someone has done a very good job of explaining the cause of the problem, and
how to work out a solution here:

http://www.devx.com/dotnet/Article/22533

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

Show quoteHide quote
"W. Jordan" <wmjor***@163.com> wrote in message
news:eAxYAUa8FHA.1864@TK2MSFTNGP12.phx.gbl...
> Hello,
>
> I am using Response.WriteFile (filename) to write a file that on
> the server to my web client.
>
> I discover that the while a client request a big file,
> for example, a 14m-bytes one, the memory usage of the
> w3wp.exe increases the same amount of 14m.
>
> That might be horrible if I allow the users to download a 32m
> or greater file. Is it possible to conserve a little memory while
> writing files to the client.
>
> --
>
> Best Regards,
> W. Jordan
>
>
>
>
Author
28 Nov 2005 2:18 AM
W. Jordan
Hello,

I am downloading the code and will examine it.
Thank you for providing this information!


--

Best Regards,
W. Jordan




Show quoteHide quote
"John Timney ( MVP )" <timneyj@despammed.com> wrote in message
news:uJLUyma8FHA.3876@TK2MSFTNGP09.phx.gbl...
> Someone has done a very good job of explaining the cause of the problem,
> and how to work out a solution here:
>
> http://www.devx.com/dotnet/Article/22533
>
> --
> Regards
>
> John Timney
> ASP.NET MVP
> Microsoft Regional Director
>
> "W. Jordan" <wmjor***@163.com> wrote in message
> news:eAxYAUa8FHA.1864@TK2MSFTNGP12.phx.gbl...
>> Hello,
>>
>> I am using Response.WriteFile (filename) to write a file that on
>> the server to my web client.
>>
>> I discover that the while a client request a big file,
>> for example, a 14m-bytes one, the memory usage of the
>> w3wp.exe increases the same amount of 14m.
>>
>> That might be horrible if I allow the users to download a 32m
>> or greater file. Is it possible to conserve a little memory while
>> writing files to the client.
>>
>> --
>>
>> Best Regards,
>> W. Jordan
>>
>>
>>
>>
>
>

Bookmark and Share