I have a very weird problem with one of my IIS application pool processes. Ive been getting a System.OutOfMemoryException error lately and ive been trying to figure out exactly what is going on. Basically I have a script that uses a web service to get a file from our DAM. It then checks the file stores it a byte array, then uses the Response to output the file. The only one ive been having problems with is the PDF's when they are over 20MB now it seems that they cause an error sometimes. If I increase the memory in the app pool it fixes the problem temporarily. I watched the w3wp.exe process and seen that sometimes when I run this script it increase the memory up to 400MB the largest file we have is 45MB what would be causing this type of behavior to happen. The problem seems to go away every night and in the morning it will work for a while and then start doing the same thing again. This application is c# asp.net application. It run inside of sharepoint.
After watching the service for a while I did notice that since these PDF's are rendered in the browser window that until the file downloads completely it doesn't release from memory. which makes sense but I can see that this is somewhat of my problem. If I have several people loading the file, with a average (no file downloading) memory usage at 385,000 kb.It can easily get to 900,000-1,100,000 KB which is the limit of the application pool.
Im not so much looking for an exact answer but more like a direction to head because I am all out of ideas.
When you bring the file data into memory as an array of bytes, you are putting a lot of pressure on the web server.
Instead of storing the entire file data in an array of bytes, you should try writing the file stream into the reponse stream in chunks.
Pseudo example:
The idea here being that you only bring a chunk of the file data into memory on each read of the file stream and then write it to the response. Note that response buffering has been disabled, and that you can substitute using a file stream with another Stream data source (I have used this approach while reading binary data from a SQL database).
Edit: (Response to how to stream data from SQL to HTTP Response)
In order to stream data from a SQL server database table (e.g. a varbinary(max) column), you use sequential access on SqlCommand:
Oppositional has very good advice about handling the file itself. I would also look at references you may be hanging on to in your web processing. Do store anything in Session State or Application State? If so, trace those carefully to make sure they don't in turn point to the page or somethign else involved with handling your files.
I mention this becuase we had a nasty 'leak' a few years ago caused by placing an object in application state. Turns out that object subscribed to page events, and since the object never died neither did all the pages! oops