2

I have come across an issue where I have created a grid that gets built up dynamically by using a document fragment and appending elements to it before appending it to a div. I have noticed that appendChild and removeChild seems to be causing memory leaks. I have created a jsfiddle that demonstrates this. I have made sure that all addons for the browsers were disabled (I didn't have much to begin with). If you open developer toolbar (F12) and profile the memory while clicking run, you will notice that the memory will go up. Every time you click it, the memory goes up. It's as if the garbage collection never happens. If there is an issue with the code, please point it out. I have also tried setting the values to the variables as null after they have been appended and have also tried delete, however they don't make a difference.

https://jsfiddle.net/k75ypb76/6

html:

<div id="grid">

</div>

<button id="btnRun">
  Run
</button>

CSS:

#grid{
  height: 250px;
  width: 500px;
  overflow: scroll;
  border: 1px solid #000000;
}

.row{
  border: 1px solid #ff0000;  
}

.cell{
  display: inline-block;
  width: 40px;
  background-color: #00ff00;
}

JS:

function run(){
    var grid = document.getElementById('grid');

    //create the grid 20 times
    for(var i =0; i < 20; i++){
        var doc = document.createDocumentFragment();
        //clear out the grid
        while (grid.firstChild) {
            grid.removeChild(grid.firstChild);
        }
        //create 300 rows
        for(var x = 0; x < 300; x++){
            var row = document.createElement('div');

            row.className = 'row';
            //create 10 cells per row
            for(var y = 0; y < 10; y++){
                var cell = document.createElement('div');

                cell.className = 'cell';    
                cell.textContent = x + '-' + y;

                row.appendChild(cell);
        }

        doc.appendChild(row);
        }

        grid.appendChild(doc);
    }
}

var btnRun = document.getElementById('btnRun');

btnRun.addEventListener('click', run);

Thanks, hopefully someone can help out with this issue.

Steve Buchok
  • 169
  • 1
  • 10
  • I have tried to delete the cell, row and doc once they have been added to their parent and that doesn't help. I also tried setting their values to null or undefined, and still that doesn't work. I would expect that once the function completes that shortly after GC would come in and cleanup, however this is not the case. I have also noticed that the memory stays as I navigate between pages. – Steve Buchok Dec 18 '15 at 17:46
  • IE version is 11.20.10586.0 Update Versions: 11.0.26 (KB3104002) – Steve Buchok Dec 18 '15 at 17:47

2 Answers2

1

When you removeChild, there is a return for that which isn't removed from memory - browsers other than IE and EDGE just seem to be smart enough to see that you're not using the return value and clean up for you.

For reference: https://github.com/mootools/mootools-core/issues/2225

Colin Reid
  • 11
  • 2
  • Thanks for the response. I saw that post earlier, however that does not fix the issue. Also, your example throws an error (sorry). Still looking. – Steve Buchok Dec 18 '15 at 17:37
  • Yeah, after I posted it, the error came to my attention, so I removed the "solution". But I left the info I learned, because I found it in a few different places and thought it would at least be educational, if not actually helpful. I'm glad you found a solution, but it is frustrating that it can't be fixed by javascript code. I can't imagine how bad it must be to have to tell customers that they have to edit the registry in order for their browser to work. – Colin Reid Dec 21 '15 at 16:37
1

Ok, this was fun. There is a known issue in IE11 on Windows 7, 8, 8.1 with the spellchecker. There is a hotfix that has been released and was in the Dec 8th update that just came out. However, this does not apparently work for Windows 10. The issue is still there. The fix I have been given for now is to edit the registry:

[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_SPELLCHECKING] "iexplore.exe"=dword:00000000

Hope this helps someone else.

Steve Buchok
  • 169
  • 1
  • 10
  • There is apparently a hotfix for Windows 7, 8 and 8.1, not for Windows 10. I have not tried these yet as I am running on Windows 10, however it might help you if you are on one of the others: https://support.microsoft.com/en-us/kb/3119070 – Steve Buchok Dec 21 '15 at 12:01