Consuming less memory when writing a file?

W

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.
 
D

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();
}
 
D

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();
}
 
W

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




Daniel Fisher(lennybacon) said:
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 said:
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.
 
W

W. Jordan

Hello,

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


--

Best Regards,
W. Jordan
 
W

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



Daniel Fisher(lennybacon) said:
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 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top