我是否总是需要显式调用URL.revokeObjectURL()? [英] Do I always need to call URL.revokeObjectURL() explicitly?

查看:144
本文介绍了我是否总是需要显式调用URL.revokeObjectURL()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Blob来下载文件,问题是即使在下载文件后,我也希望保留对象URL,而无需在代码库中进行重大更改.

I'm using blob to download files, Problem is I want to keep Object URL even after downloading the file, without making major changes in code base.

因此,一种选择是不致电URL.revokeObjectURL();

So one of the option is not to call URL.revokeObjectURL();

依靠浏览器的垃圾收集器来避免任何内存泄漏是否安全?

Is it safe to depend on browser's garbage collector to avoid any memory leak?

我是否总是需要显式调用URL.revokeObjectURL();?

Do I always need to call URL.revokeObjectURL(); explicitly ?

推荐答案

依靠浏览器的垃圾收集器来避免任何内存泄漏是否安全?

Is it safe to depend on browser's garbage collector to avoid any memory leak?

createObjectURL大约要做的一件事情是,将创建的Blob URL中的ID映射到与当前全局对象相关联的隐藏Map中的实际Blob.从GC的角度来看,这意味着Blob 一直可以访问,直到全局本身变为可收集的为止 -通常在您离开页面或关闭标签页时-或吊销映射时(假设这是最后一个参考).

What createObjectURL approximately¹ does is to create a mapping of the ID in the created blob URL to the actual blob in a hidden Map associated with the current global object. Which means from the perspective of the GC the Blob remains reachable until either the global itself becomes collectible - commonly when you navigate away from the page or close a tab - or when the mapping is revoked (assuming that was the last reference).

因此,这取决于您将其视为泄漏的原因.它不会在浏览器的生命周期内保持活动状态,而只会在当前页面的生命周期内保持活动状态.如果您希望其寿命可能短于当前页面,则需要撤消.

So it depends on what you consider as a leak. It won't remain alive for the lifetime of the browser, only for the lifetime of the current page. You need to revoke if you want its lifetime to be potentially shorter than the current page.

¹并非完全如此,但是在推理可及性时,它可以很好地描述行为.

¹ That's not exactly how it's implemented, but it describes the behavior reasonably well when reasoning about reachability.

这篇关于我是否总是需要显式调用URL.revokeObjectURL()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆