System.OutOfMemoryException while Creating PDF files

System.OutOfMemoryException while Creating PDF files

am 23.10.2007 12:09:58 von Aryan

Hi,

I am using C# with framework 2.0 and creating PDF files on-fly, along
with this I am using Windows 2003 Server.

I am using Byte[] to take the data input and then save into pdf format
on harddrive location.

Now after creating few successful pdf files, I am getting
"System.OutOfMemoryException".

Is this problem coming because of large data is been sent to Byte[] to
create PDF file or something else could be the cause.

If so, then how to resolve this issue??

Thanks in Advance,

Manoj

Re: System.OutOfMemoryException while Creating PDF files

am 23.10.2007 12:19:11 von Marc Gravell

A lot of factors could be in play here. Trying to use enormous arrays
is a big contender. Accidentally holding on to large arrays (directly
or indirectly) will compound this.

Where possibly, the most memory-efficiet way to deal with large binary
data is using a streamed approach - whether that means to/from a
file/database/network etc depends on the scenario. But it means only
having to handle a small buffer of working data.

Can you give any more information on your setup? Size of byte[]? Any
chance you are keeping old instances alive? (perhaps subscribing to
long-lived events, such as static events). Would streaming be an
option, etc.

Marc

Re: System.OutOfMemoryException while Creating PDF files

am 23.10.2007 15:00:27 von Aryan

On 23 Oct, 15:19, "Marc Gravell" wrote:
> A lot of factors could be in play here. Trying to use enormous arrays
> is a big contender. Accidentally holding on to large arrays (directly
> or indirectly) will compound this.
>
> Where possibly, the most memory-efficiet way to deal with large binary
> data is using a streamed approach - whether that means to/from a
> file/database/network etc depends on the scenario. But it means only
> having to handle a small buffer of working data.
>
> Can you give any more information on your setup? Size of byte[]? Any
> chance you are keeping old instances alive? (perhaps subscribing to
> long-lived events, such as static events). Would streaming be an
> option, etc.
>
> Marc

Mark, Thanks for prompt reply.

Here is the sample code snippet.

I am using SQLReport Server 2005 to get the data.

Byte[] results;
ReportExecutionService rsExec;
results = rsExec.Render(format, deviceInfo,
out extension, out encode,
out mimeType, out warnings, out streamIDs);

SavePDF(results, filename); // calling SavePDF method to save the
result array output in PDF form.

private bool SavePDF(Byte[] results, string fileName)
{
try
{

using (FileStream stream = File.OpenWrite(fileName))
{
stream.Write(results, 0, results.Length);
stream.Close();
stream.Dispose();
}

return true;
}
catch(Exception ex)
{
throw;
}
}


Now,
1> If I get large stream from ReportExecutionService then how to split
it in chunks and create a pdf file(say pdf has something around 3000 /
5000 pages in it)

Thanks in Advance.
Manoj