DEV Community

KomoPDF
KomoPDF

Posted on

PDF previews keep growing in memory: when to revoke blob URLs

A PDF preview can work perfectly on the first upload and still consume more memory each time a user replaces the file. One common cause is creating a new blob URL for every preview and never releasing the old one. The opposite shortcut, revoking the URL immediately after assigning it to an iframe, can leave the viewer trying to load a URL that no longer resolves.

For a browser PDF editor, the useful boundary is the lifetime of the preview: keep its URL while the viewer or download link needs it, then release it when that preview is replaced or closed.

What a blob URL holds

URL.createObjectURL(file) returns a blob: URL that refers to a File or Blob. It is a browser-managed reference, not a network address for the original file. Each call creates a distinct URL, even for the same object. An active object URL can keep its underlying object reachable, so repeatedly creating URLs can retain resources longer than intended. The browser releases object URLs when the document unloads, but a long-lived editing tab should clean up as it goes.

Revoke a URL with URL.revokeObjectURL(url) once all consumers have finished with it. This releases the URL's reference; it does not promise that all memory disappears immediately. A PDF viewer, JavaScript variable, canvas or other component may still hold its own data.

A preview with an explicit close action

The following minimal example assumes an <iframe id="preview"> and a file input. Only this function owns the iframe's object URL. Call closePreview() when the user closes the preview or the component is removed.

const frame = document.querySelector('#preview');
let currentUrl = null;

function showPdf(file) {
  const nextUrl = URL.createObjectURL(file);
  const previousUrl = currentUrl;
  currentUrl = nextUrl;
  frame.src = nextUrl;

  if (previousUrl) URL.revokeObjectURL(previousUrl);
}

function closePreview() {
  frame.removeAttribute('src');
  if (currentUrl) URL.revokeObjectURL(currentUrl);
  currentUrl = null;
}
Enter fullscreen mode Exit fullscreen mode

This revokes the previous URL after the iframe is pointed at the replacement. It intentionally keeps the current URL alive beyond the iframe's load event: an embedded viewer may need the document again for actions such as print, download or reload. If another part of the interface uses the same URL, give it a separate URL or coordinate ownership before revoking it. Check replacement and close behavior in each browser your application supports; embedded PDF viewers differ.

For a React or other component-based interface, the same rule belongs in the lifecycle cleanup. Avoid creating an object URL during render: a render can run more than once, and an unused render still creates a URL. Create it in an effect or event handler, and revoke the exact URL created by that operation in its cleanup.

Large files have more than one memory cost

Revoking a blob URL fixes one source of retention. It does not release a PDF parser's page cache or a canvas still attached to the page. A single RGBA canvas needs approximately width × height × 4 bytes for its pixel buffer before other allocations. For example, a 2,000 by 3,000 pixel canvas needs about 24,000,000 bytes, or 22.9 MiB, for that buffer alone. Higher device pixel ratios increase both dimensions and therefore the pixel count.

If a preview renders every page at full resolution, reducing the number of live canvases may save more memory than changing the file URL. Render visible pages first, release off-screen render resources according to the viewer library's API, and cancel obsolete render tasks when a user switches files. Keep a separate copy of the original file only when the workflow needs one.

Check the failure cases

Use two test PDFs of different sizes and switch between them repeatedly. Confirm that the final preview is the expected file, that closing it removes access to its old URL, and that printing or downloading still works while the preview remains open. Include a rapid sequence of file selections: asynchronous parsing can finish out of order, so the application should ignore results from an older selection after a newer one has started.

Inspect browser memory across several open, replace and close cycles. Garbage collection and the built-in viewer can make readings noisy; look for a sustained trend across repeated cycles rather than expecting an immediate drop after each revoke. Repeat with a long document to separate retained file references from rendered page buffers.

For the broader editing workflow, KomoPDF provides browser-based PDF editing and page organization. Its local editing workflow keeps files on the device; its optional KolmoPDF translation connection uploads the current PDF when translation is requested. For a related document compatibility issue, see why filled PDF forms can open blank in another viewer.

References: MDN: createObjectURL, MDN: revokeObjectURL, and MDN: blob URLs.

Top comments (0)