Search code examples
phpgoogle-app-enginepdffpdffpdi

App Engine Process randomly terminated because of "too much memory"


For the past year, I've been using google app engine to make PDF's and then output them onto the page. They have always been working flawlessly until today.

Now, I get the following error - not all the time, but probably around 40% of my requests come back with a 500 error about it being terminated.

Does anyone know why this would be happening and how to avoid it?

While handling this request, the process that handled this request was found to be using too much memory and was terminated. This is likely to cause a new process to be used for the next request to your application. If you see this message frequently, you may have a memory leak in your application.


This is my script. Sometimes I add images to it using $pdf->MemImage(); or I add text with $pdf->Cell() but that is about the only difference.

<?php
    define('FPDF_FONTPATH','lib/fpdi/fonts');
    require_once('lib/fpdf.php');
    require_once('lib/fpdi/fpdi.php');

    $pdfPath = file_get_contents("gs://bucket/template.pdf");
    $temp_id = "12345";
    $max_pages = 5;

    //put the pdf image into a temp directory in google drive
    $object_url = "gs://bucket2/template.pdf";
    file_put_contents($object_url, $pdfPath);

    //Start the FPDI
    $pdf = new FPDI('P', 'pt');
    $pdf->SetAutoPageBreak(false);

    //Set the source PDF file
    $source_file = $pdf->setSourceFile("gs://bucket2/template.pdf");

    for($page_count = 1; $page_count <= $max_pages; $page_count++)
    {
        //Import the first page of the file
        $tppl = $pdf->importPage($page_count);
        $pdf->AddPage();

        //get size of pdf page
        $size = $pdf->getTemplateSize($tppl);

        $pdf->useTemplate($tppl, null, null, $size['w'], $size['h'], true);
    }

    header('Content-Type: application/pdf');
    $pdf->Output("template.pdf", "I");
    unlink("gs://bucket2/template.pdf");
?>

Solution

  • I solved this by updating the instance class and basic scaling in app.yaml. This is just experimentation and might need to be different based on use case but it did solve the problem for me. Thanks to everyone who helped in the above comments.

    application: <APPLICATION NAME>
    version: 1
    runtime: php55
    api_version: 1
    instance_class: B2
    basic_scaling:
      max_instances: 5