Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it more efficient to once build a large string and pass it to response.write or call response.write for each piece [closed]

We've been running into daily OutOfMemoryExceptions in our ASP.Net 4.0 website. We suspect one of the problems is LOH fragmentation so we've been looking into code changes that would allocate memory more efficiently.

For example, we're generating a large string (2mb) that we want to return to the browser. Paging the data is not an option.

Is it more efficient to:

  1. build the string in a StringBuilder and then make a single call to Response.Write(bigString) or
  2. write the string piecemeal by repeated calls to Response.Write(smallString)

If I follow option 1 then I've got the one big string taking space on the LOH that then is copied to the Response object's internal buffer. So this seems like I've now got 2 big blocks on the LOH at least temporarily.

If I follow option 2 then I'm dealing with lots of small strings that get garbage collected and only the one large block on the LOH for the Response object's buffer.

So it seems to me option 2 is better.

Am I understanding this correctly?

The server has 4gb of Ram and is running Windows 2003 32bit. This is the only site running on the server. So each process has a 4gb address space but only 2gb is usable. We start getting OOM errors when virtual bytes hits about 1.8Gb and then we recycle the site which resolves the problem for about 24 hours. Private bytes varies between 500-800mb. I don't think the problem is that we are running out of physical memory.

like image 618
user3704846 Avatar asked Nov 11 '22 07:11

user3704846


1 Answers

Without knowing the implementation, my answer may be a little less helpful than what I would hope. I'll venture a go, though.

If it were me, I would leverage two tactics:

  1. Take advantage of hard disk space and write to a text file stored in a cache directory on the server (assuming only one web server)
  2. Use an old-school Server.Execute("~/filename.txt"); to spit out the text file contents onto your page.
like image 105
user3720856 Avatar answered Nov 14 '22 23:11

user3720856