Firefox 强制 gc 在文件上传后清理文件链接减少内存消耗
Firefox force gc to clean file links after file upload reduce memory consumption
我正在开发 javascript 文件上传器。上传完成后,我发现firefox profiler(about:memory)中有很多文件链接。
问题是 - 如何清除此文件链接并强制 GC?
我使用 dataUri (url.createobjecturl) 创建预览,使用 FormData 上传文件。
有内存报告的例子。
651.04 MB (100.0%) -- explicit
├──430.49 MB (66.12%) -- dom
│ ├──428.99 MB (65.89%) -- memory-file-data
│ │ ├──428.93 MB (65.88%) -- large
│ │ │ ├────4.00 MB (00.61%) ── file(length=2111596, sha1=b95ccd8d05cb3e7a4038ec5db1a96d206639b740)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2126739, sha1=15edd5bb2a17675ae3f314538b2ec16f647e75d7)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2138438, sha1=2784adb8078f24f4a144c65ce13643355a4146a8)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2176403, sha1=77958bb26563354cd611eaf7fd1308504fe13bd7)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2179450, sha1=728abac0e79a6c9507d79fd3863f59985e7a48f7)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2190489, sha1=35adc1cb98d2fe48fca1a82965deb7f5fd48474b)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2292088, sha1=e6be87fbd4197f0b64226a842c4495ba5a18d217)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2311952, sha1=85fcdde9920606af6d26af603931d0afc66713fe)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2434576, sha1=f3233a8e88ef9fe5cd1514516795fa41b65056b2)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2595794, sha1=b4b808f3e57a7924e0809009b70b11aa8b9ad608)
│ │ │ ├────2.00 MB (00.31%) ── file(length=1048805, sha1=20423b8519750a48b7b77f40a45e084aaba3e2f3)
│ │ │ ├────2.00 MB (00.31%) ── file(length=1054953, sha1=637b55f0c78022634f11204adbd75c0fae5e92ae)
好的,这对我有用,我用文件清理了数组和对象:arr[i] = null 和 obj.file = null。
上传后我再也不会使用这个对象,arr = null,和 obj = null,不要从内存中删除文件。
不确定强制 GC 收集未使用的对象是否对您的情况有帮助。根据您的内存日志,看起来 file 对象的数量在每次迭代后都在增加。换句话说,问题在于那些对象泄漏,而不是 GC 太慢。
为确保是这种情况,只需检查脚本停止时的内存消耗(例如,上传过程完成后)。如果文件对象的数量最终减少,则意味着 GC 认为这些对象将被删除。如果没有,您的应用程序无论如何都会保留您的对象。
最近我遇到了与 Blob 类似的问题 - 我在循环中创建了对象并添加了一个 Blob 作为其 属性。我在 about:memory 上有一张类似的图片 - file 条目的数量与迭代次数相关。当我将带有 Blob 的 属性 显式设置为 null 时,那些对象从 about:memory 中消失了。
所以我的建议是查看您的代码,找到您将 Blob 作为属性(我怀疑是数组项)保存的所有位置,并在您确定不再需要它们时将它们设置为 null .
希望这对您有所帮助。
我正在开发 javascript 文件上传器。上传完成后,我发现firefox profiler(about:memory)中有很多文件链接。 问题是 - 如何清除此文件链接并强制 GC? 我使用 dataUri (url.createobjecturl) 创建预览,使用 FormData 上传文件。 有内存报告的例子。
651.04 MB (100.0%) -- explicit
├──430.49 MB (66.12%) -- dom
│ ├──428.99 MB (65.89%) -- memory-file-data
│ │ ├──428.93 MB (65.88%) -- large
│ │ │ ├────4.00 MB (00.61%) ── file(length=2111596, sha1=b95ccd8d05cb3e7a4038ec5db1a96d206639b740)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2126739, sha1=15edd5bb2a17675ae3f314538b2ec16f647e75d7)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2138438, sha1=2784adb8078f24f4a144c65ce13643355a4146a8)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2176403, sha1=77958bb26563354cd611eaf7fd1308504fe13bd7)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2179450, sha1=728abac0e79a6c9507d79fd3863f59985e7a48f7)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2190489, sha1=35adc1cb98d2fe48fca1a82965deb7f5fd48474b)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2292088, sha1=e6be87fbd4197f0b64226a842c4495ba5a18d217)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2311952, sha1=85fcdde9920606af6d26af603931d0afc66713fe)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2434576, sha1=f3233a8e88ef9fe5cd1514516795fa41b65056b2)
│ │ │ ├────4.00 MB (00.61%) ── file(length=2595794, sha1=b4b808f3e57a7924e0809009b70b11aa8b9ad608)
│ │ │ ├────2.00 MB (00.31%) ── file(length=1048805, sha1=20423b8519750a48b7b77f40a45e084aaba3e2f3)
│ │ │ ├────2.00 MB (00.31%) ── file(length=1054953, sha1=637b55f0c78022634f11204adbd75c0fae5e92ae)
好的,这对我有用,我用文件清理了数组和对象:arr[i] = null 和 obj.file = null。 上传后我再也不会使用这个对象,arr = null,和 obj = null,不要从内存中删除文件。
不确定强制 GC 收集未使用的对象是否对您的情况有帮助。根据您的内存日志,看起来 file 对象的数量在每次迭代后都在增加。换句话说,问题在于那些对象泄漏,而不是 GC 太慢。
为确保是这种情况,只需检查脚本停止时的内存消耗(例如,上传过程完成后)。如果文件对象的数量最终减少,则意味着 GC 认为这些对象将被删除。如果没有,您的应用程序无论如何都会保留您的对象。
最近我遇到了与 Blob 类似的问题 - 我在循环中创建了对象并添加了一个 Blob 作为其 属性。我在 about:memory 上有一张类似的图片 - file 条目的数量与迭代次数相关。当我将带有 Blob 的 属性 显式设置为 null 时,那些对象从 about:memory 中消失了。
所以我的建议是查看您的代码,找到您将 Blob 作为属性(我怀疑是数组项)保存的所有位置,并在您确定不再需要它们时将它们设置为 null .
希望这对您有所帮助。