Deal with memory leak in gojs diagram

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);
}

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.

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.

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?

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

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.

Thank you Walter. Sorry for my late reply.
Anyway, I decided to assign new values of node properties in a way that within the first iterationI assign whole node to both nodeDataArray and linkDataArray and in successive iterations used the setDataProperty of model to change each node’s property. It seems there is no leak by this approach. Thank you for your suggestions.

var firstLoop = true;
while(true){
if(firstLoop == true)
{
myDiagram.model.nodeDataArray = myNodeArray;
myDiagram.mode.linkDataArray = myLinkArray;
firstLoop = false
}
else
{
for(i=0; i< elemSize;i++){ myDiagram.model.setDataProperty(myDiagram.model.nodeDataArray[i],‘someProperty’,newNodeValue[i]);
myDiagram.model.setDataProperty(myDiagram.model.linkDataArray[i],‘someProperty’,newLinkValue[i]);
}
}
}