views:

727

answers:

3

Web application storing images in database.

In our first setup we were storing (and serving) from images from database resized in asp.net cache. This was all good, but as web started getting more and more hits, we started experiencing weird problems, and by weird, I do mean weird - ranging from w3p crashing without any details to random OutOfMemoryExceptions occurring everywhere couple of times a day.

I suspected that asp.net cache my be misused by us here, so I rewrote image caching to go to filesystem. Our image handler now checks for resized image in cache dir, if no such image exists, fetches it from database and saves it in cache dir, and serves it from there.

The thing that's bothering me is memory usage of w3p for this web site. Before, when we were using asp.net cache object, typical memory usage for w3p was around 600mb. Now, when handler serves images from filesystem typical memory usage is around 750mb. Somehow I expected it go down (at least a bit), but definitely not up. Is there any explanation for this?

Second, is there a way to inspect w3p process in order to see how much memory is used for what? One thing that comes to mind is performance counters, but haven't had the time to check it out.

+1  A: 

This is just speculation on my part, but if the file I/O is buffered, wouldn't that use a lot of memory? More so perhaps than the ASP cache as each file served up requires a buffer instead of taking a shared resource from the cache.

As for memory profiling, there are a number of profiler programs out there you could use them to analyse what's going on.

nickd
So you're saying that serving file from filesystem uses more memory than caching it in asp.net's cache? I don't know if it's just me, but this sounds wrong/weird. Can anyone confirm this?
Vnuk
I did say it was conjecture -- I was just thinking out loud.
nickd
I've ticked your answer for reminding me of profiler tools. Using .net memory profiler it seems that now there is more pages (strings) in cache then when it was full of images. Thanks.
Vnuk
A: 

Are you sure that you've disabled the OutputCache for those items? It seems like you still execute dynamic code to determine whether to create the resized image if necessary, so there still could be an active OutputCache

chris166
Please specify what do you mean by "disabled OutputCache"? Images are served via asp.net handler (ashx).
Vnuk
I haven't used .ashx much, and I don't know if it's possible to have an OutputCache on them. Using a profiler is certainly the best way to find the cause, though it might take you some time. Good luck with that!
chris166
If anyone wonders about this, I think that ashx'es don't have OutputCache on them
Vnuk
A: 

That many images into cache causes it to grow and grow. Default behaviour of asp.net is restart when process riches 60% of available memory.

OutOfMemoryException can be throwed when one process will be recycled and second will be created but first doesn't be finished for a long time. Then you have 2 processes taking a lot of memory. If application is busy and there is a lot of requests then could be more than 2 processes at time. This lead to OutOfMemoryException.

I rewrote image caching to go to filesystem. Our image handler now checks for resized image in cache dir, if no such image exists, fetches it from database and saves it in cache dir, and serves it from there.

This is the best solution for this. FileSystem is cached at OS level.

dario-g
I also think that this is the best solution, but how do you explain increased memory usage of w3p?
Vnuk