0

I have memory leakage when using Ajax, where Firefox memory usage increases after each ajax call.

I use jQuery 1.10.2.

Are there any workarounds/fixes for this bug?

Edit: I use $.post not $.ajax.

Any $.post function is causing the leak:

$(".Button1").on("click", function(){
    $.post(document.location, "data1=1", function(data) {
        $("#mainDiv").html(data);
    });
});

This is an example code, every time I click on the button memory usage will increase.

Edit2: I have tested this on Chrome too and same problem, so the problem is with jQuery.

I have tried this workaround: http://bugs.jquery.com/ticket/10824

But the problem still exists.

This is more noticable when I run the Ajax function every 10 seconds using setInterval, where memory insanely increases.

DeepBlue
  • 684
  • 7
  • 23
  • 1
    Your question lacks sufficient information to diagnose the problem. Please post the relevant parts of your code and elaborate on the data being leaked. – Frédéric Hamidi Jan 27 '14 at 11:31
  • Sounds like this should be a bugreport to Firefox, not a StackOverflow question. – Venemo Jan 27 '14 at 11:43
  • @Frederic see example code – DeepBlue Jan 27 '14 at 11:45
  • @ Venemo I have tested this on Chrome too and same problem – DeepBlue Jan 27 '14 at 12:07
  • The problem may be caused if you are attaching data to the generated HTML by the AJAX response, then each time you run the AJAX request the **DOM** elements previously in `#mainDiv` are destroyed, but the associated data could remain untouched. See this stackoverflow: **[jQuery memory leak with DOM removal](https://stackoverflow.com/questions/1462649/jquery-memory-leak-with-dom-removal)** – jherax Jun 16 '15 at 17:45

2 Answers2

1

I have found the cause of the problem, it is html() function, if I use innerHtml the problem is solved.

I think I found a solution on stopping memory leak with html() function:

I save mainDiv object into a variable, then only pass this variable to the $.post:

var main = $("mainDiv");
$(".Button1").on("click", function(){
    $.post(document.location, "data1=1", function(data) {
        main.html(data);
   });
});

And memory leak is reduced to minimum (there is still some slight memory leak but I do not think it is from the code).

DeepBlue
  • 684
  • 7
  • 23
  • Keeping in **cache** the object, could provide an small improvement. The real problem may be caused when the **HTML** is replaced inside the `#mainDiv`, also if you are attaching data to the generated HTML, then each time you run the AJAX request the **DOM** elements previously in `#mainDiv` are destroyed, but the associated data could remain untouched. See this stackoverflow: **[jQuery memory leak with DOM removal](https://stackoverflow.com/questions/1462649/jquery-memory-leak-with-dom-removal)** – jherax Jun 16 '15 at 17:49
0

I think you are loading whole the document in a div, and on the next click again, and so on, so you are not loading the page but the page in a div. What happens if you load data in the document?

Thrash Bean
  • 658
  • 4
  • 7