木偶/铬:处理崩溃的大量内存页面?

Ric*_*ard 3 chromium puppeteer

我有以下代码:

puppeteer.launch().then(async browser => {
    for (let id of ids) {
        try {
            const page = await browser.newPage();
            //const url = 'chrome://crash';
            await page.goto(url + id)
            await page.waitFor(5000);
            await page.screenshot({
                path: path.join(__dirname, "../public/images/screenshots/" + id + ".png"),
                clip: { x: 10, y: 70, width: 780, height: 470}
            });
        } catch (error) {
            console.log('Exception', id, error.message);
            page.close();
        }
    };
    browser.close();
});
Run Code Online (Sandbox Code Playgroud)

通常情况下还可以,但是我在特定页面上遇到了问题(不幸的是我无法共享该URL)。

该页面尝试加载GB的数据并导致Chrome崩溃,所以我想这也导致Chromium崩溃。

我从此页面看到的错误是:Exception 6766 Navigation Timeout Exceeded: 30000ms exceeded。很好,但是现在似乎还没有停止-我想这会导致整个服务器挂起,因为它试图在OS级别上使用过多的内存。

如何停止服务器挂起并优雅地处理此问题?我可以提供一些标志给Chromium,以限制使用的内存并优雅地放弃吗?我也不确定我的错误处理总体上是否正确,因此任何提示都将不胜感激。

Ram*_*iro 5

一些想法:

首先,这可能与您的最初问题无关,但可以节省一些内存。您正在打开许多选项卡(页面)而没有关闭它们,如果您的ID列表很长,则会占用大量内存。

尝试通过加载新的URL而不是创建新页面来遍历ID。

您也可以延长超时时间,以检查是否有更多时间最终可以解决问题。

启动木偶时可以传递铬标志。这是完整的Chromium开关列表

您可以尝试:--unlimited-storage--force-gpu-mem-available-mb

此外,这可能会方便调试: --full-memory-crash-report

您可以在那里搜索所有其他与内存相关的标志。

这是将标志传递给人偶的方法,以及我提到的其他建议:

puppeteer.launch({args: ['--unlimited-storage', '--full-memory-crash-report']}).then(async browser => {
  const page = await browser.newPage();

  for (let id of ids) {
      try {
          //const url = 'chrome://crash';
          // Timeout will be 2 min and will wait till network is idle before taking the screenshot
          await page.goto(url + id, {timeout: 120000, waitUntil: 'networkidle0'}); 
          await page.screenshot();
      } catch (error) {
          console.log('Exception', id, error.message);
          // If you catch an error you should throw it and handle it on the parent function
          // or set up an event listener so you can now what's the state of your app
          throw new Err (error);

          // Since we are only opening one page and browsing from here
          // no need to close when you encounter an error
          // just goto the next iteration
          // page.close();
      }
  };
  browser.close();
});
Run Code Online (Sandbox Code Playgroud)


mjs*_*mjs 5

尝试将{args: ['--disable-dev-shm-usage']}启动选项传递给launch()命令-这应该可以提高Puppeteer渲染大型/复杂页面的能力