473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Consuming less memory when writing a file?

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


Nov 25 '05 #1
7 2949
Tr something like
--
Daniel Fisher(lennybacon)
http://www.lennybacon.com
"W. Jordan" <wm******@163.com> wrote in message
news:eA**************@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

Nov 25 '05 #2
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" <wm******@163.com> wrote in message
news:eA**************@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

Nov 25 '05 #3
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
"Daniel Fisher(lennybacon)" <in**@lennybacon.com> wrote in message
news:Ou*************@TK2MSFTNGP15.phx.gbl...
Tr something like
--
Daniel Fisher(lennybacon)
http://www.lennybacon.com
"W. Jordan" <wm******@163.com> wrote in message
news:eA**************@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


Nov 25 '05 #4
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" <wm******@163.com> wrote in message
news:eA**************@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

Nov 25 '05 #5
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)" <in**@lennybacon.com> wrote in message
news:Op**************@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" <wm******@163.com> wrote in message
news:eA**************@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


Nov 28 '05 #6
Hello,

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

Best Regards,
W. Jordan


"John Timney ( MVP )" <ti*****@despammed.com> wrote in message
news:uJ**************@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" <wm******@163.com> wrote in message
news:eA**************@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


Nov 28 '05 #7
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)" <in**@lennybacon.com> wrote in message
news:Op**************@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" <wm******@163.com> wrote in message
news:eA**************@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


Nov 28 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David Young | last post by:
Hi, I have been asked to write a client application for consuming a web service for a cms written in ASP 3.0. I have very little experience of developing on a microsoft platform, and very basic...
6
by: Ganesan selvaraj | last post by:
I using C# .net. i want to split the text files based of the some condition. my source text file size may be 4 kb to 4 gb. some time when i split the i got the "out of memory exception. when i...
1
by: Aravind | last post by:
Hi, I am trying to create a image object out of a bitmap file like Image img = new Bitmap(@"c-\CurDisplayImage.bmp"); ,I found the memory of the process is increased twice as the bitmap size ,...
5
by: Scott Emick | last post by:
I have rather small project that is a store/dealer locator but it is consuming about 143 MB in terms of the virtual memory I believe. Working set is 46 MB and Private Bytes 32 MB. The project...
2
by: saran | last post by:
I am having a problem with MySQL consuming a lot of memory and eventually throwing an Out of Memory error and restarting itself. The symptoms are that swap usage continues to rise until some...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
7
by: Pietro Cerutti | last post by:
Hi group, I just noticed that a malloc w/out relative free occurs in the standard C library (or at least, on my implementation of it). #include <stdio.h> int main(void) { printf("%s\n",...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
4
by: Kerem Gümrükcü | last post by:
Hi, the topic of this thread implies the question. Why does this happen. What can i do against it. App runs in admin context, then ii switch to user standard user context and open a form with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.