Javascript - File saved to disk is stuck in Chrome's memory

北战南征 提交于 2019-12-10 17:52:49

问题


I have this code:

function saveFile(str, part) {
  var textFileAsBlob = new Blob([str], {type:"text/plain"});
  var fileNameToSaveAs = "Parsed audio - part "+part;

  var downloadLink = document.createElement("a");
  downloadLink.download = fileNameToSaveAs;
  downloadLink.innerHTML = "Download File";

  if (window.URL != null)
  {
      // Chrome allows the link to be clicked
      // without actually adding it to the DOM.
      downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
  }
  downloadLink.click(); 
}

It works okay, except this one problem with Chrome: The blob's "footprint" or whatever is kept in Chrome's main process' memory. When download window is opened, the whole blob (250MB in my case!) is copied into main process' memory. That's kinda bad since if I save multiple files, I eventually fill memory up to 750MB, and at that point chrome stops downloading files with "Not found" error. Pic: http://i.stack.imgur.com/j5PUn.jpg

Am I doing some stupid mistake or is this Chrome's fault? Can I clean Chrome's memory to get rid of this problem?


回答1:


As my comment seemed to be the answer you were looking for, I've put it as an actual answer


You're not releasing the blob URL after the click, which means the GC can't get rid of the blob

// after the click
URL.revokeObjectURL(downloadLink.href);


来源:https://stackoverflow.com/questions/29878506/javascript-file-saved-to-disk-is-stuck-in-chromes-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!