App Engine 进程因“内存过多”而随机终止


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

在过去的一年里,我一直在使用谷歌应用引擎制作PDF,然后将它们输出到页面上。直到今天,他们一直完美无缺地工作。

现在,我收到以下错误 - 并非一直如此,但可能大约 40% 的请求返回有关它被终止的 500 错误。

有谁知道为什么会发生这种情况以及如何避免它?

在处理此请求

时,发现处理此请求的进程使用了过多的内存并被终止。这可能会导致新进程用于对应用程序的下一个请求。如果您经常看到此消息,则您的应用程序中可能存在内存泄漏。


这是我的剧本。有时我会使用$pdf->MemImage();向其添加图像 或者我添加带有$pdf->Cell()的文本,但这是唯一的区别。

<?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");
?>

我通过更新实例类和 app.yaml 中的基本缩放来解决此问题。这只是实验,可能需要根据用例而有所不同,但它确实为我解决了问题。感谢在上述评论中提供帮助的所有人。

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