Click here to Skip to main content
14,492,338 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have generated an rdlc report with about 50 parameters. I have exported this report to image dynamically through localreport.render method. System.outofmemory exception occurs when the report is frequently exported. Plz Help.
Updated 18-Apr-19 16:08pm
Comments 30-Oct-15 12:43pm
My case about ***OutOfMemoryException*** not memory problem, I don't know yet _Maybe configuration IIS, Pool, web.config issues ?_
Rate this:
Please Sign up or sign in to vote.

Solution 1

AFAIK, there is not much(code wise) that you can do about it.
Refer the following support article: MS Support: You may receive the "System.OutOfMemoryException" error message when you use SQL Server Reporting Services[^]

Look at this discussion on similar lines: Export To Excel Error throws System.OutOfMemoryException[^]

Try to upgrade your web server - look at the RAM available on the server, the processor utilization when the report is called, and the amount of server disk space (for temp storage).
Rate this:
Please Sign up or sign in to vote.

Solution 2

I'm pretty late to this, but I have a real solution and can explain why!

It turns out that LocalReport here is using .NET Remoting to dynamically create a sub appdomain and run the report in order to avoid a leak internally somewhere. We then notice that, eventually, the report will release all the memory after 10 to 20 minutes. For people with a lot of PDFs being generated, this isn't going to work. However, the key here is that they are using .NET Remoting. One of the key parts to Remoting is something called "Leasing". Leasing means that it will keep that Marshal Object around for a while since Remoting is usually expensive to setup and its probably going to be used more than once. LocalReport RDLC is abusing this.

By default, the leasing time is... 10 minutes! Also, if something makes various calls into it, it adds another 2 minutes to the wait time! Thus, it can randomly be between 10 and 20 minutes depending how the calls line up. Luckily, you can change how long this timeout happens. Unluckily, you can only set this once per app domain... Thus, if you need remoting other than PDF generation, you will probably need to make another service running it so you can change the defaults. To do this, all you need to do is run these 4 lines of code at startup:

    LifetimeServices.LeaseTime = TimeSpan.FromSeconds(5);
    LifetimeServices.LeaseManagerPollTime = TimeSpan.FromSeconds(5);
    LifetimeServices.RenewOnCallTime = TimeSpan.FromSeconds(1);
    LifetimeServices.SponsorshipTimeout = TimeSpan.FromSeconds(5);

You'll see the memory use start to rise and then within a few seconds you should see the memory start coming back down. Took me days with a memory profiler to really track this down and realize what was happening.

You can't wrap ReportViewer in a using statement (Dispose crashes), but you should be able to if you use LocalReport directly. After that disposes, you can call GC.Collect() if you want to be doubly sure you are doing everything you can to free up that memory.

Hope this helps!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100