sodcalculator

10 Tech How-To

Playwright Screenshots on a Low RAM Laptop (India)

Guide 10-12171 · Updated · Step by step

Taking Playwright screenshots on a low-RAM laptop in India can be painfully slow or crash-prone, especially with heavy pages. This guide shows you practical tweaks—from browser flags to screenshot settings—that cut memory usage and get you clean captures without upgrading hardware.

What You’ll Need

  • A laptop running Windows, Linux, or macOS (the steps work on all).
  • Node.js (v16 or later) installed.
  • Playwright installed in your project (npm init playwright@latest).
  • A code editor (VS Code or any text editor).
  • Basic familiarity with the command line.

Step-by-Step

  1. Install Playwright with minimal browsers. Run npm init playwright@latest and when asked which browsers to install, choose only Chromium. Firefox and WebKit consume extra disk and RAM. If you already installed all, remove the unused ones with npx playwright uninstall firefox webkit.
  2. Set Node’s memory limit. Open your terminal and set NODE_OPTIONS=--max-old-space-size=512 (on Windows PowerShell: $env:NODE_OPTIONS="--max-old-space-size=512"). This prevents Node from hogging RAM.
  3. Launch Chromium with memory-saving flags. In your script, pass these args to chromium.launch():
    const browser = await chromium.launch({
      args: [
        '--disable-dev-shm-usage',
        '--disable-gpu',
        '--single-process',
        '--no-sandbox',
        '--disable-extensions',
        '--disable-background-timer-throttling',
        '--disable-backgrounding-occluded-windows',
        '--disable-renderer-backgrounding'
      ]
    });
        

    The --disable-dev-shm-usage is especially helpful on Linux where /dev/shm is small.

  4. Use a viewport that matches your target. Set a smaller viewport (e.g., 1280×720) in newPage({ viewport: { width: 1280, height: 720 } }). Larger viewports increase memory usage.
  5. Wait for the page to be idle, not just loaded. Use await page.waitForLoadState('networkidle') before taking the screenshot. This avoids capturing mid-load and reduces the chance of a crash.
  6. Take a full-page screenshot in chunks. Instead of fullPage: true, capture only the visible viewport. If you need the full page, use page.screenshot({ path: 'shot.png', fullPage: true }) but be aware it uses more memory. For very long pages, consider using a scrolling capture script that stitches images.
  7. Set JPEG quality or use PNG with compression. For non-critical captures, use type: 'jpeg', quality: 70 to reduce file size and memory during encoding. For PNG, use omittedBackground: true if the page has no background.
  8. Close the page and browser after each screenshot. Always await page.close() and await browser.close() in a finally block. This frees memory for the next run.
  9. Run your script with a low-priority process. On Linux/macOS, use nice -n 19 node script.js. On Windows, use start /low node script.js. This prevents the script from starving other apps.
  10. Use a headless browser. Headless mode uses significantly less RAM than headed. Add headless: true in launch options.

Common Mistakes

  • Installing all browsers. Each browser adds ~300MB of disk and RAM when running. Stick to one.
  • Using fullPage: true on huge pages. This can crash a low-RAM laptop. Use viewport screenshots or chunked scrolling.
  • Not closing browser instances. Leaving browsers open leaks memory. Always close in finally.
  • Ignoring the --disable-dev-shm-usage flag on Linux. This is critical for low-RAM systems.
  • Taking screenshots too early. If the page hasn’t finished loading, you’ll get blank or partial screenshots.

Tips

  • Use page.setViewportSize() to adjust after load. This can reduce memory for wide pages.
  • Disable images and fonts. Use page.route('**/*', route => { if (route.request().resourceType() === 'image' || route.request().resourceType() === 'font') route.abort(); else route.continue(); }) to save memory on image-heavy sites.
  • Use a single browser context for multiple screenshots. Reuse the same page and navigate to different URLs instead of creating a new page each time.
  • Set timeout options in goto() to 30s. This prevents hanging on slow networks.
  • Run screenshots in batches. If you have many URLs, process them one by one and restart the script between batches to clear memory.

Troubleshooting

  • Browser crashes with “Out of memory” error. Reduce viewport size, disable GPU, and use --single-process. Also try lowering --max-old-space-size to 256.
  • Screenshot is blank or white. Wait for networkidle and increase the timeout. Also check if the page uses canvas or WebGL—those need GPU, so disable --disable-gpu might cause issues; try removing it.
  • Slow performance. Use headless: true and close all other applications. Also consider using --disable-extensions and --disable-background-timer-throttling.
  • Cannot connect to browser. Ensure no other Playwright process is running. Kill any lingering node processes.
  • File size too large. Use JPEG with quality 50-70, or reduce viewport.

Frequently Asked Questions

Does Playwright work on 2GB RAM?

Yes, but you must use headless mode, disable GPU, and limit viewport. It will be slow but workable for simple pages.

Can I take screenshots on a mobile viewport?

Yes, set the viewport to e.g., 375×812 and use isMobile: true and hasTouch: true in the context options.

Why is my screenshot blurry?

Increase deviceScaleFactor to 2 in viewport options for sharper text, but this doubles memory usage.

How do I take a screenshot of a specific element?

Use await page.locator('selector').screenshot({ path: 'element.png' }). This is more memory-efficient than full-page.

Can I use Playwright on a Raspberry Pi?

Yes, but you may need to install additional dependencies. The low-RAM flags are even more critical there.

What is the best image format for low RAM?

JPEG with quality 60-80 is smaller and uses less memory during encoding than PNG.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →