Deal with memory leak in gojs diagram


#1

Hi.
I’m struggling with memory leaks in gojs diagram. I have made the diagram and its node/link templates out of the loop. The diagram uses same HTML tag. Within the loop i just make my desired nodes and links in object forms and then assign them to diagram.model.nodeDataArray. In DevTools of chrome in memory profiling timeline I clearly observe remained vertical blueline in any interval.

var nodes = [];
function loop()
{
$.ajax({
url:‘someurl’,
type:‘POST’,
success:function(data){
$.each(data,function(ind,value){
nodes.push({‘key’:value.key,‘text’:value.text});
});
diagram.model.clear();
diagram.model.nodeDataArray = [];
diagram.clear();
diagram.model.nodeDataArray = nodes;
nodes = null;
},
error:function(error){}
});
setTimeOut(function(){loop();},60000);
}


#2

The only obvious reason for memory retention is that the UndoManager has been enabled. Is that the case?

It’s odd that you are setting Model.nodeDataArray twice. But that shouldn’t matter with respect to memory usage.

I assume you are wanting to load a new diagram upon each successful response. What is normal and easiest is to create a new Model and then assign Diagram.model. Nothing else is needed. Most of the samples demonstrate this.


#3

I just set animationManager.Enable to true. In any interval i want to update all elements of the diagram. Of course there are others items in any node object and i just wrote the key and text above.
I’m afraid maybe nodes variable doesn’t completely remove from memory.


#4

There would be a reference to the same array from diagram.model.nodeDataArray, so the nodes variable is just another reference to that same Array.

So, if you have not enabled the UndoManager and you have changed your code not to use the nodes variable and to just replace the Diagram.model each time, do you still have a memory problem?


#5

Sorry. I’m out of the time of working day. Tomorrow i will test your suggestion.
Thank you


#6

If data is just the Array of node objects, your code could just be:

success: function(data) {
  diagram.model = new go.GraphLinksModel(data);
}

But I don’t know if you are using a GraphLinksModel or a TreeModel or a plain Model, so you would need to use the desired class.