TableTools memory leak when tables are deleted without refreshing

TableTools memory leak when tables are deleted without refreshing

pandaEaterpandaEater Posts: 6Questions: 0Answers: 0
edited April 2013 in General
I have a web app that destroys and creates a DataTable with a button click. To remove the DataTable I'm using the jQuery .html() method which guarantees that it [quote] removes other constructs such as data and event handlers from child elements before replacing those elements with the new content. [/quote] I confirmed there was no memory leak using this method without TableTools using a combination of Resource Monitor on windows and the Chrome heap snapshot debugging capability.

When the TableTools plugin is being used, it stores references to aoData in, from what I’ve seen, multiple locations. So when I clear the current DataTable and create a new one the aoData array from the old table is still referenced by TableTools which creates a memory leak. At first glance, it looked like it was just this instance array keeping the reference to old tables in TableTools.js:
[code]
/**
* Store of all instances that have been created of TableTools, so one can look up other (when there is need of a master)
* @property _aInstances
* @type Array
* @default []
* @private
*/
TableTools._aInstances = [];
[/code]
So my first attempt at fixing the problem was splicing the _aInstance array to remove the first element when I delete the current table:
[code]
TableTools._aInstances.splice(0, 1);
$('#tableContainer').html(‘’);
[/code]
Unfortunately the aoData array is still being referenced somewhere after doing this so the leak persists.

Is there a built-in method to clear the TableTools object to prevent it from referencing removed DataTables data? I’ve tried a few other things including using fnClearTable with no luck.

I appreciate any help!

Replies

  • pandaEaterpandaEater Posts: 6Questions: 0Answers: 0
    Any ideas on this one?
  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin
    The destroy hook in TableTools should be removing the instance from the instances array. It looks like it isn't : -/
    https://github.com/DataTables/TableTools/blob/master/media/js/TableTools.js#L676

    I'll need to spend a bit of time and look at this. I'll try to make some time soon, but I'm trying to put all of the development time I can into 1.10 at the moment.

    Allan
  • pandaEaterpandaEater Posts: 6Questions: 0Answers: 0
    I did some investigating this afternoon and found the two objects keeping references to the removed DataTables:
    [code]
    /* In TableTools.js */
    TableTools._aInstances = [];

    /* In ZeroClipboard.js */
    clients: {}
    [/code]

    I’m using this code as an easy workaround since I only have one DataTable at a time on my page so blowing the ZeroClipboard_TableTools.clients object away each time is fine.
    [code]
    TableTools._aInstances.shift();
    delete ZeroClipboard_TableTools.clients;
    ZeroClipboard_TableTools.clients = {};
    [/code]

    When I get some time, I’ll look into a more elegant fix to this.
  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin
    Thanks for posting your investigations - really brilliant in fact :-)

    Allan
This discussion has been closed.