当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Firefox force gc to clean file links after file upload reduce memory consumption

问题描述:

I am developing javascript file uploader. And when upload is done, i found that there are a lot of file links in firefox profiler(about:memory).

The question is - How to clean this file links and force GC?

I use dataUri (url.createobjecturl) for creating preview and FormData to upload files.

There is example of memory report.

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)

Ok, that is worked for me, i am cleaned array and object with files : arr[i] = null and obj.file = null.

After upload i am never use again this objects, arr = null, and obj = null, don't remove files from memory.

网友答案:

Not sure that forcing GC to collect the unused object will help in your situation. According to your memory log, it looks like the amount of file objects is increasing after each iterations. In other words, the problem is that those objects leak, rather than GC being too slow.

To make sure that it is the case, just check your memory consumption when your script stopped (e.g. after the upload process complete). If the number of file object eventually reduces, it means that GC considers those objects to be removed. If not, your application holds your objects anyhow.

Recently I had a similar issue with Blobs - I created objects in a loop and added a Blob as its property. I had a similar picture on about:memory - the number of file entries correlated with the number of iterations. When I explicitly set property with a Blob to null, those object disappeared from about:memory.

So my recommendation is to take a look into your code, find all places where you hold Blobs as properties (and, I suspect, array items) and set them to null when you are sure that they are no longer needed.

Hope this helps.

分享给朋友:
您可能感兴趣的文章:
随机阅读: