Memory leak in Table visualization

33 views
Skip to first unread message

Viktor

unread,
Oct 28, 2009, 10:10:44 AM10/28/09
to Google Visualization API
Hello all,

I use Table visualization and do refresh the table every 5 sec calling
my drawTable function with setTimeout (after I refresh some data in
tabledata):

var table, tablesortcol, tablesortasc, tabledata;
function drawTable() {
if (table == null) {
var div = document.getElementById('vis_div');
table = new google.visualization.Table(div);
tablesortcol = 0;
tablesortasc = true;
google.visualization.events.addListener(table, 'sort',
tableSort);
}
table.draw(tabledata, { alternatingRowStyle: true, sortColumn:
tablesortcol });
}

The problem is, memory usage grows with each call of table.draw as in
IE8 as in Chrome4.

Any solutions?


Thanks,
Viktor

Matt Ball

unread,
Oct 29, 2009, 10:47:07 AM10/29/09
to Google Visualization API
You're creating a closure, which means that every time you call
drawTable(), it will use the same values of the outer variables.
Presumably, table is null at the time that it's bound inside of
drawTable(), which means that you're going to be new-ing every time.
There are at least two decent ways to fix this: either pass your
variables into drawTable as parameters, or initialize your table and
listener outside of drawTable and just call table.draw() whenever you
want to redraw.
Let's avoid the old "memory usage keeps increasing, so it must be a
memory leak!" habit.

Markw65

unread,
Oct 29, 2009, 11:42:48 AM10/29/09
to Google Visualization API
On Oct 29, 7:47 am, Matt Ball <mattjb...@gmail.com> wrote:
> You're creating a closure, which means that every time you call
> drawTable(), it will use the same values of the outer variables.
> Presumably, table is null at the time that it's bound inside of
> drawTable(), which means that you're going to be new-ing every time.

Err... no.

It will use the /current/ values of those variables each time the
function is called. The first time he calls drawTable, table gets set,
and the if clause never gets called again (unless he explicitly sets
table to null elsewhere in his code).

It still might not be a leak. Theres no way to know when the garbage
collector will run. Uncollected objects will count towards a processes
memory usage, even though they are available to be freed. So the real
question is - can you cause the process to run out of memory by
calling drawTable enough times (you didnt say how much memory was
leaked, so it may not be practical).

Mark
Reply all
Reply to author
Forward
0 new messages