DEV Community

Cover image for Decoding a 50 MP JPEG vs a PNG in sharp: peak memory, measured
Sahadat Husain
Sahadat Husain

Posted on

Decoding a 50 MP JPEG vs a PNG in sharp: peak memory, measured

A 50 megapixel image costs about 150 MB of raw pixels in sharp, and the process peaked at 264 MB for a JPEG and 253 MB for a PNG when I decoded it in full. A resize to 1920 pixels is cheaper: 134 MB for the 3.5 MB JPEG, 184 MB for the 65.5 MB PNG. Reading the size with metadata() added nothing over an idle process (72 MB).

Plan your memory per pixel, not per file byte. Here is the script, the output, and what it means for how many images a worker can hold at once.

How much memory does a 50 megapixel image need?

The raw pixel count sets the floor. 8000 × 6250 is 50,000,000 pixels. At 3 bytes per pixel (RGB) that is 150,000,000 bytes, and sharp returned exactly that from raw().toBuffer().

I built three test images from our camera photo (4964 × 2782, apps/web/public/images/index/image-compressor-sample-camera-photo.jpeg), upscaled to 8000 × 6250:

// gen.mjs
import sharp from 'sharp';
const src = 'image-compressor-sample-camera-photo.jpeg';
const base = await sharp(src).resize(8000, 6250, { fit: 'cover' }).toBuffer();
const noise = await sharp({ create: { width: 8000, height: 6250, channels: 3,
  background: { r: 128, g: 128, b: 128 }, noise: { type: 'gaussian', mean: 128, sigma: 6 } } })
  .png({ compressionLevel: 0 }).toBuffer();
const mixed = await sharp(base).composite([{ input: noise, blend: 'overlay' }]).toBuffer();
await sharp(mixed).jpeg({ quality: 85 }).toFile('j50.jpg');
await sharp(mixed).png({ compressionLevel: 6 }).toFile('p50.png');
Enter fullscreen mode Exit fullscreen mode
j50.jpg 8000 6250 50000000 3548643
p50.png 8000 6250 50000000 65506064
Enter fullscreen mode Exit fullscreen mode

The same pixels are 3,548,643 bytes as a JPEG and 65,506,064 bytes as a PNG. The noise makes the PNG large; a real screenshot would be far smaller, and I show a plainer PNG below.

Does the file format change peak memory?

I ran each operation in a fresh Node process and read the peak resident set size from /usr/bin/time -l. Each cell is the median of 5 runs.

// mem.mjs  (node mem.mjs <file> <op>)
import sharp from 'sharp';
import { readFile } from 'node:fs/promises';
const [file, op] = process.argv.slice(2);
if (op === 'metadata') await sharp(file).metadata();
if (op === 'resize') await sharp(file).resize(1920).webp({ quality: 80 }).toBuffer();
if (op === 'resize-buffer') await sharp(await readFile(file)).resize(1920).webp({ quality: 80 }).toBuffer();
if (op === 'raw') await sharp(file).raw().toBuffer();
Enter fullscreen mode Exit fullscreen mode
Operation JPEG 3.5 MB PNG 65.5 MB
idle Node with sharp loaded 72 MB 72 MB
metadata() 72 MB, 1.2 ms 72 MB, 1.0 ms
resize(1920).webp() from a path 134 MB, 307.5 ms 184 MB, 871.6 ms
same, input read into a Buffer first 134 MB, 290.1 ms 256 MB, 743.7 ms
raw().toBuffer() (full decode) 264 MB, 122.1 ms 253 MB, 490.8 ms

Peak memory for the JPEG and the PNG under resize, full decode and metadata

Three things stand out.

  • A full decode costs the same for both formats. The output buffer is 150 MB either way, and the process peaks 192 MB (JPEG) and 181 MB (PNG) above idle. The format does not change what you pay once you ask for every pixel.
  • A resize is cheaper than a full decode. For the JPEG it peaked at 134 MB, half of the 264 MB. This fits libvips decoding the JPEG at a reduced size, but I did not confirm that directly.
  • Passing a Buffer instead of a path costs the file size on top. The PNG went from 184 MB to 256 MB, a gap of 72 MB, close to its 65.5 MB size. The 3.5 MB JPEG did not move.

Note: a small file is not a small decode. Our plain upscaled PNG (no noise, 69,569,834 bytes) peaked at 186 MB for a resize and 258 MB for a full decode, the same band as the noisy one. File size predicts almost nothing. Pixels do.

What does this mean for a worker?

Take the 264 MB full decode and a 2 GB container. Subtract roughly 72 MB for the runtime and you can hold about seven such decodes at once, before any output or encoder memory. The resize path at 134 to 184 MB fits ten or more. Those are my arithmetic on my numbers, not a limit I measured on a small server.

So cap two things. Cap the pixel count (50,000,000 here) and cap how many images are in flight. The sharp constructor docs give limitInputPixels a default of 268,402,689, which at 3 bytes per pixel would be 805 MB for one raw buffer. That is too loose for a small worker. For the pixel-cap behaviour in detail, see my earlier piece on limitInputPixels and MAX_IMAGE_PIXELS. Reading the size first is cheap, as the table shows: 1.2 ms and no extra memory.

What should you do?

  • Set limitInputPixels to what your product needs. A 50 MP cap is 150 MB of raw pixels.
  • Read metadata() first and reject early. It added no memory in my runs.
  • Pass a file path when you can. A Buffer adds the whole file to the heap.
  • Budget a full decode at about 3 bytes per pixel plus the runtime, not the file size.
  • Bound parallel jobs. Memory grows with each one in flight.

Our own engine works under these limits, and the 400 MP JPEG case shows what happens past them.

How I measured

  • Machine: Apple M1 Pro, 8 cores, 16 GB, macOS, 2026-10-18.
  • Versions: Node 24.22.0, sharp 0.35.4, libvips 8.18.6.
  • Peak memory: maximum resident set size from /usr/bin/time -l, median of 5 fresh Node processes per cell. Times are the median of the same 5 runs, measured inside the process.
  • Raw output in measured.json next to this article.

What I did not test

One photo's pixels, and one platform. Linux, other allocators and other sharp versions will give different numbers. I did not test AVIF, WebP or HEIC input, or 16-bit images.


I build Blinkimg, an image compressor and converter with a developer API. I wrote this article with help from an AI assistant (Claude), which ran the code on my machine; every number in it comes from those runs.

Top comments (0)