I noticed that assigning an ID attribute value to dynamically created DOM objects leads to IE9 memory leak. Has anyone else experienced this and, more importantly, been aware of any problems? It does not leak in other browsers, even IE6 passes!
Leakage code demo:
It simply adds and removes rows from the table continuously and assigns an identifier to each row, which will be used for searching later.
No leak without "row.id = eid;"
<html> <head> <script type="text/javascript"> function addRow(tbl, index) { var row = tbl.insertRow(index); var eid = "loongrowid" + count; row.id = eid; for (var i = 0; i < 9; i++) { row.insertCell(i); } return row; } function removeTableRow(tbl, index) { var row = tbl.rows[index]; tbl.deleteRow( index ); } var count = 1; function fillRow(row){ row.cells[0].innerHTML = '<input type="checkbox"' + ' checked="checked"' + ' />'; for (var i = 1; i < 9; i++) { row.cells[i].innerHTML = count + " c"; } ++count; } var added = false; function dostuff() { var tbl = document.getElementById("tbl"); var i; if (added) { for (i = 0; i < 20; ++i) { removeTableRow(tbl,1); } } else { for (i = 0; i < 20; ++i) { var row = addRow(tbl, i+1); fillRow(row); } } added = !added; setTimeout(dostuff, 1); } </script> </head> <body onload="setTimeout(dostuff, 1)"> <h1 id="count">TESTING</h1> <table id="tbl" style="width:100%;"> <tr> <th>selected</th> <th>date</th> <th>time</th> <th>place</th> <th>device</th> <th>text</th> <th>state</th> <th>status</th> <th>quality</th> </tr> </table> </body> </html>
I noticed that removing all cells from a table row causes a memory leak compression, so I think IE holds onto the row after it is deleted from the table.
I also tried a job that added the created table rows to a Javascript object that would be used as a hash table instead of relying on getElementById (row.id), but also leaked for some reason that I don't see.
var hash = []; // when creating row row.extid = eid; // Note: this by itself causes no leak hash[eid] = row; // when removing row delete hash[row.extid];
source share