开发者

Dynamic table memory usage

I use a dynamic table:

<html>
<body>
<button id="button">Build table</button>
<div id="container">
<script type="text/javascript">
window.onload=function(){
 var table = null;

 var row = "<tr><td>111111111111111111111111111111111111111111111111111111</td>" +
           "<td>222222222222222222222222222222222222222222222222222222</td>" 开发者_C百科+
     "<td>333333333333333333333333333333333333333333333333333333</td></tr>";
 var data = null;
 for (var i = 0; i < 2000; i++){
  data += row;
 }

 var obj = document.getElementById("button");
 obj.onclick=function buildTable(){
  document.getElementById("container").innerHTML = "<div><table><tbody>" + data + "</tbody></table></div>";
 };
};
</script>
</body>
</html>

Using chromes task manager, each time new data is loaded the memory usage increases considerably and doesn't go down, so after some time the app consumes a lot of memory and requires the browser to be closed. Is there any change in the code I can use to solve this or is it a browser side problem?


On my computer it works as expected, memory use build up, until the browser decide to run the garbage collector, at which point memory use go down to a base level.

Are you sure that memory consumption actually does reach a critical level? If so I'd characterize it as a bug in Chrome, you shouldn't be able to make a memory leak just by feeding it malformed HTML.


Man, you are trying to add 2000 rows for each click! Thats a pritty large table and tables are quite memory consuming. Actually you have 2000 rows from the beginning, so clicking the button once will make i 4000. I would recommend recommend using some kind of pagination, limitied the max numbers of rows on in the document at once. Not more than 2000. What about removing the first 2000 before adding 2000 new ones?

Maybe your page will be a bit faster if you redesign your code to use only nested divs instead of <table><tr>...</tr><table> ... but still... 4000+ rows ... its a lot... not very userfriendly... scrolling... etc... What are you trying to make excatly ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜