|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
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 Tr something like
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 > > > > 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(); } 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 >> >> >> >> > > 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(); } 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 > > > > 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 ; } } } -- Show quoteHide quoteBest Regards, W. Jordan "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 >> >> >> >> > > 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. -- Show quoteHide quoteBest Regards, W. Jordan "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 >> >> >> >> > > 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 -- Show quoteHide quoteRegards 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 > > > > Hello,
I am downloading the code and will examine it. Thank you for providing this information! -- Show quoteHide quoteBest Regards, W. Jordan "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 >> >> >> >> > >
Other interesting topics
How to add a small HTML table under a row of a GridView
run asp.net without a web server Visual Web Developer Express How do I build Code-behind into DLL in bin folder Downloading file from database A translatable website ASP classic -> ASP.NET 2.0 Problem Running ASP.NET/ASP App Under VS 2005 Cannot display Chinese data from database Application and Session state stored in SQL Server XML/XSL transform problem |
|||||||||||||||||||||||