System.OutOfMemoryException: Insufficient memory to continue the execution of the program

Oct 30, 2014 at 12:22 PM
When I use GhostscriptRasterizer.GetPage to get PDF page as image, I got the exception: System.OutOfMemoryException: Insufficient memory to continue the execution of the program.

System.Runtime.InteropServices.Marshal.AllocHGlobal(IntPtr cb)
SOMETIMES, ERROR throw on this function:
Ghostscript.NET.ImageMemoryHelper.FlipImageVertically(IntPtr src, IntPtr dest, Int32 height, Int32 stride)
Ghostscript.NET.Viewer.GhostscriptViewerDisplayHandler.Page(IntPtr handle, IntPtr device, Int32 copies, Int32 flush)
Ghostscript.NET.GhostscriptDisplayDeviceHandler.display_page(IntPtr handle, IntPtr device, Int32 copies, Int32 flush)
Ghostscript.NET.Interpreter.GhostscriptInterpreter.Run(String str)
Ghostscript.NET.Viewer.GhostscriptViewerPdfFormatHandler.ShowPage(Int32 pageNumber)
Ghostscript.NET.Viewer.GhostscriptViewer.ShowPage(Int32 pageNumber, Boolean refresh)
Ghostscript.NET.Rasterizer.GhostscriptRasterizer.GetPage(Int32 xDpi, Int32 yDpi, Int32 pageNumber)

This exception typically happens when using desiredDpi = 300. However, it is working fine if the desiredDpi = 96. But we really want it to be 300 DPI due to image quality issue.

Code:

using (var source = new MemoryStream(documentFileContent))
{
using (var rasterizer = new Ghostscript.NET.Rasterizer.GhostscriptRasterizer())
{
 rasterizer.Open(source, version, true);
 for (var i = 1; i <= rasterizer.PageCount; i++)
 {
    var img = rasterizer.GetPage(desiredDpi, desiredDpi, i);
I have tried to dispose img ASAP, but I still got the except sometimes (not always)
Nov 4, 2014 at 2:21 PM
I have the same error.

Can anyone help us?
Nov 4, 2014 at 7:15 PM
If you read the source code for GetPage, it is actually using memory stream to rasterize PDF file. I would think to use command line directly to avoid this exception.
Coordinator
Nov 4, 2014 at 7:58 PM
Edited Nov 4, 2014 at 8:01 PM
Hi, sorry for delay...

Can i see the PDF you are trying to rasterize?

Other system information like windows version, ram size...would be good.

Also, does this happen on the first page of the pdf, always a same page or random page?

Cheers,
Josip
Nov 5, 2014 at 11:58 AM
Hello friends,

In My Windows Forms, I use PDF files with 1 MB, 20 MB, 50 MB, 100 MB... But, in the middle of processing i got the OutOfMemoryException.
The Computer Memory growing... :/

Run the same code on Console Application works very well and memory not growing.



My Code below.
        public void Start(string file, string outputPath, int dpi)
        {
            var DLL = AppDomain.CurrentDomain.BaseDirectory + "gsdll32.dll";
            var gvi = new GhostscriptVersionInfo(DLL);
            var proc = new GhostscriptProcessor(gvi);
            var rasterizer = new GhostscriptRasterizer();

            using (rasterizer = new GhostscriptRasterizer())
            {
                rasterizer.Open(file, gvi, true);
                for (int i = 1; i <= rasterizer.PageCount; i++)
                {
                    string pageFilePath = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(file) + "-p" + i.ToString() + ".jpg");
                    Image img = rasterizer.GetPage(dpi, dpi, i);
                    img.Save(pageFilePath, ImageFormat.Jpeg);
                    Console.WriteLine(pageFilePath);
                }
            }
        }
Click Button on Windows Forms
MyClass.Start(openFileDialog1.FileName, docs.Diretorio, 100);
Coordinator
Nov 5, 2014 at 8:12 PM
Hi,

I did try your code and it works well. Memory on my laptop is stable and not growing.

Can you try to change your code to: rasterizer.Open(file, gvi, false);
(last 'false' parameter tells not to run ghostscript in it's own memory space).

Other than that. What version of native Ghostscript library do you use? A same question for Ghostscript.NET?

Cheers,
Josip