0

I am using XMLHttpRequest to download the file of size 750mb as arraybuffer. The problem is that after downloading the file, the memory is not released to the os even after deleting the XMLHttpRequest object. My sample code is

<!DOCTYPE html>
<html>
<body>

<div id="demo">
<button type="button" onclick="loadXMLDoc()">load the file</button>
<button type="button" onclick="clear()">clear</button>
</div>

<script>
var xhttp = null;
function loadXMLDoc() {
    if(xhttp == null)
        xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      console.log(xhttp.response);
      delete xhttp;
      xhttp = null;

    }
  };
    xhttp.open("GET", "http://192.168.1.104/gltf/bmw/buffer.bin", true); //buffer.bin is 750 mb

    xhttp.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
    xhttp.responseType = 'arraybuffer';
    xhttp.send();
}

function clear()
{
    delete xhttp;
    xhttp = null;


}

</script>

</body>
</html>

1) is this the right way to delete the XMLHttpRequest object? 2) How to make the XMLHttpRequest to release the memory back to the os? enter image description here

Thanks

Srinivas
  • 351
  • 5
  • 14

2 Answers2

0

Not exactly. delete <variable name> even looks confusing(and I'm surprised it works without throwing an error). <variable> = null is correct approach in general. But in your case it should not make a difference.

JS implements own memory management. Garbage collector will free memory once related variable is not used by anyone else.

The thing is Garabge Collector is not running continuously. Rather it's called from time to time. Try clicking "Collect Garbage" button in Developer Tools. This will call GC immediately. Developer Tools - initiate garbage collecting

This is not a solution rather a way to investigate if memory can be freed or it cannot because of referencing to your data from somewhere.

skyboyer
  • 22,209
  • 7
  • 57
  • 64
  • Thanks for the reply. Clicking "Collect Garbage" button in Developer Tools did not solve the problem.Still the memory is not released. if i use xhttp.responseType = 'blob'; then there is no memory spike. – Srinivas Aug 16 '18 at 06:55
  • I am not sure about this but put the code of xmlhttprequest in a closure function and try again. Secondly, checkout answer here i.e. versioning the call: https://stackoverflow.com/questions/3308538/how-to-clear-the-cache-data-when-using-ajax – Dhiren Aug 16 '18 at 06:56
  • @Srinivas, I tried to find out if there is something special about 'arraybuffer' but did not find anything about that :( – skyboyer Aug 16 '18 at 07:02
0

I found that the issue is with the "console.log()".If i comment out the console.log line then the memory is released back to the os.

Srinivas
  • 351
  • 5
  • 14