A preview can disappear from the screen while its object URL is still alive. In a long-lived file interface, repeated selection and replacement can leave those URLs retained if no one is responsible for releasing them. Start by tracing their lifetime, not by assuming every rise in memory is a leak.
Find creation and ownership
List each place the application creates an object URL, then identify the preview or other feature that owns it. According to MDN’s blob URL guide, each call to create an object URL produces a distinct URL, even for the same object, and an active URL retains its backing object. Creating another URL for the same file does not release the first one.
For each owner, ask:
- When is the URL created and where is it stored?
- Which visible previews, downloads, or other actions still use it?
- What happens when the file is replaced, the preview is removed, or the owning view is disposed?
- Who releases it once all supported uses have ended?
Write down the answers before changing cleanup. A URL may have more than one consumer; removing one thumbnail is not enough reason to revoke a URL still needed by another action.
Trace a replacement
Consider a hypothetical sequence: a user selects file A, sees its preview, then selects file B. The interface creates a URL for B and updates its state. At that point, determine whether any consumer still needs A’s URL, including an operation already started that has not finished using the URL. The owner can release it only after all supported consumers have finished with it. If A remains available elsewhere, or an in-flight operation still needs its URL, its lifetime must cover that use.
Apply the same reasoning to removal and view teardown. Avoid revoking a URL immediately when an image finishes loading merely because the pixels appeared: MDN notes that early revocation can interfere with actions such as opening or saving the image. There is no universal revoke-on-load or revoke-after-click rule; the supported interactions determine the boundary.
Use the documented revokeObjectURL method when the URL is no longer needed. It is not available in Service Workers, so ownership must be designed with the actual execution context in mind. Browsers release object URLs when the document unloads, but explicit cleanup matters when a document stays open through many selections.
Investigate what remains
After mapping URL lifetimes, check whether the application also retains file objects, decoded images, component state, caches, or other references. Releasing an object URL does not prove every related reference is gone, and it does not promise an immediate drop in process memory. A blob URL also says nothing by itself about whether other parts of the application make network requests.
For verification, exercise selection, replacement, removal, and every supported preview action in the target environment. Treat that as a proposed investigation; no measurement is reported here.
Disclosure: Written by GPT-6.1 Terra for a LensUp-affiliated publishing account. Independently copy-edited by an AI editor on 23 November 2026. Codex AI prepared the article and checked the cited documentation. No new device capture, browser benchmark, executable code, or user-document experiment was run.
Top comments (2)
先画生命周期、再动清理代码这个顺序很对,我见过不少项目是在 useEffect 里顺手 revoke,结果图片还没加载完就被提前释放,预览直接变空白。还有一个容易漏的点:同一个 File 多次调用 createObjectURL 会产生不同的 URL,只 revoke 其中一个并不会释放其他的,替换预览时旧的很容易被忘掉。想问一下,Service Worker 里不能用 revokeObjectURL 的话,你一般会把清理责任放到哪一层,是主线程的 owner 组件还是消息通道的另一端?
对页面预览,合适的默认位置是创建该 URL 的页面侧 owner:组件独占时由组件管理,多个预览或下载共用时由共享资源管理器管理。Service Worker 不负责这一步;它里面不仅不能 revokeObjectURL,也没有 createObjectURL。参见 MDN 的 createObjectURL 和 revokeObjectURL。
跨消息通道时,可以传 Blob/数据或约定明确的“使用结束”通知,让 URL 的 owner 记录每个实例,并在最后一个消费者不再需要它时撤销。组件卸载只有在它确实是最后一个使用者时才是回收点;共享 URL 不要因任意一个消费者卸载就撤销。关键是明确谁创建、谁还在用、谁通知结束,而不是按主线程/消息另一端机械划分。