Question

I have this site that streams images from the database (SQL2008), and I think it's the one causing the very high CPU usage on my server. The CPU usage is at least 60-90%.

I am using MVC3 and below is the code in my Controller that sends the image to the View:

 [OutputCache(Duration = 86400, VaryByParam = "GUID")]
 public FileStreamResult GetFile(string guid)
    {
        Guid id = new Guid(guid);
        Thumbnail thumbnail = thumbService.GetThumbnailByGUID(id);
        Stream stream = new MemoryStream(thumbnail.FileContent.ToArray());
        var fsr = new FileStreamResult(stream, "image");
        return fsr;
    }

The View:

 <div style="background:url('@url');background-repeat:no-repeat;background-position:50% top;background-color:#fff;" class="photoThumb">

The @url above is /GetFile/guid

Can anyone tell me what I'm doing wrong?

Thanks

Updates on the answer and another question:

The [OutputCache(Duration = 86400, VaryByParam = "GUID")] from the answer below worked and has improved the performance of the site. The CPU usage has now went down to 8-60% but I also want to make sure that everything is disposed so I want to know if FileStreamResult is doing that for me or should I do it manually?

Was it helpful?

Solution

I'd guess that there is a database hit for each image request, with multiple images per page request - this could lead to high CPU usage. You should try caching the images.

If you decorate your action with

[OutputCache( Duration = 86400, VaryByParam = "id" )]

this should cache the result image for one day, meaning fewer db queries, and hopefully less load on your server.

OTHER TIPS

You can also modify as below.

[OutputCache(Duration = 86400, VaryByParam = "GUID")]
     public FileStreamResult GetFile(string guid)
        {
            Guid id = new Guid(guid);
            Thumbnail thumbnail = thumbService.GetThumbnailByGUID(id);
            var fsr; 
            using(Stream stream = new MemoryStream(thumbnail.FileContent.ToArray()))
            {
                fsr = new FileStreamResult(stream, "image");
            }
            return fsr;
        }

As mentioned in above comment, when your page render FileStreamResult will automatically dispose.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top