I’ve just spent the last week or so doing some testing on our ASP.NET application. We’re in the process of porting it from ASP.NET 1.1 to 2.0 and it’s been an intriguing voyage of discovery so far. But after a random period of time the software would stop working – page postbacks would fail, data we were storing in the cache was disappearing and it was driving me crazy! The only way to get things working was to reboot my machine. One step forward, two steps back.
I eventually figured out what the problem was (I had several red herrings to work through first as is always the case in such things). I put a callback onto inserts into the cache to determine why items were being cleared out of it and interestingly at certain points I’d add something and it would be removed immediately with a reason of “underused”. This is ASP.NET’s memory collection at work deciding that as my machine is so low on resources it would try and save as much as possible. The following change to web.config (in the System.Web section) sorted it out:
<caching> <cache disableMemoryCollection = "true" disableExpiration = "false" privateBytesLimit = "0" percentagePhysicalMemoryUsedLimit = "90" privateBytesPollTime = "00:02:00"/> </caching>
The reason I was getting the problem so often was that my work laptop died and the one I’m using until I get it fixed is far less powerful and has a lot less memory. So it was happening frequently on my machine, less frequently on everyone else’s, even less frequently on our test server and very rarely on our live server (the problem also effected our ASP.NET 1.1 version). Still, it’s a nice feeling to have figured it out.
Of course the real problem wasn’t the cache recycling at all – it’s perfectly natural that the cache should clear itself out when it wants. The problem was that when a particular object was no longer found in the cache by our software, the code wasn’t properly re-populating it from the database. So with that fixed we shouldn’t need to turn off the memory collection at all.
And to think, if it wasn’t for my laptop expiring this would probably have remained a random, undiagnosed crash for quite some time yet. It’s a tricky old game this debugging one…