jszip中的javascript内存泄漏

vaqhlq81  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(374)

我在我的小react应用程序中使用jszip。我需要从sharepoint列表中获取附件。附件大小约为3 gb,所以我决定分部分(200 MB)下载。但我的内存消耗量很大(超过3500 mb)。我找不到内存泄漏
源代码。此功能在react组件之外:

var JSZip = require("jszip");
async function testUploadAllAttachments() {    
    const ceilSizebytes = 209715200;
    // actually it count blobs sizes
    let blobCounter = 0;    

    const filterItemsDate = await sp.web.lists.getByTitle("Reports").items.getAll();
    console.log("filterItemsDate: ", filterItemsDate);

    let zip = new JSZip();

    for (const item of filterItemsDate) {        
        let allItemAttachments = await sp.web.lists.getByTitle("Reports").items.getById(item.Id).attachmentFiles();

        let itemFolder = zip.folder(item.Id);
        console.log("itemFolder: ", itemFolder);
        console.log("blobCounter: ", blobCounter);

        for (const attach of allItemAttachments) {
            let urlToFetch = attach.ServerRelativePath.DecodedUrl;

            let blob = await fetch(urlToFetch).then(response => {
                return response.blob();
            });
            // Blob size control
            blobCounter += blob.size;
            if (blobCounter > ceilSizebytes) {

                blobCounter = 0;
                zip.remove(item.Id);                    

                await zip.generateAsync({ type: "blob", 
                compression: "DEFLATE", 
                compressionOptions:{
                    level: 6
                } })
                    .then(function (content) {
                        // saveAs(content, "examplePart.zip");
                        // content = null;
                    })
                zip = null;
                zip = new JSZip();

                // Recreate missing item on removing step
                let itemFolderReset = zip.folder(item.Id);
                for (let i = 0; i < allItemAttachments.length; i++) {
                    let urlToFetchReset = allItemAttachments[i].ServerRelativePath.DecodedUrl;

                    let blobReset = await fetch(urlToFetch).then(response => {
                        return response.blob();
                    });

                    itemFolderReset.file(allItemAttachments[i].FileName, blobReset );
                    blobCounter += blobReset.size;
                }
                continue;
            }
            else {
                itemFolder.file(attach.FileName, blob);

            }

        }

    }
    await zip.generateAsync({ type: "blob" })
        .then(function (content) {
            saveAs(content, "example.zip");
        })    

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题