Resources are not Released when the GraphCanvasComponent is Removed from the View

Questions & Answers

Summary

This article describes how to completely release the GraphCanvasComponent after removing it from the application.

Description

Memory leak in the Library

Problem

In yFiles FLEX version 1.6.0.1 and below an internal class of the GraphEditorInputMode keeps a static reference to the input mode. Thus the GraphCanvasComponent and references to its GraphCanvasComponent are not released for Garbage Collection.

This will only happen if a GraphEditorInputMode is associated with the GraphCanvasComponent.

Solution

If you don't use handles (for resizing and moving port and bends) it is possible to use a modified GraphEditorInputMode:

public class NoHandleGraphEditorInputMode extends GraphEditorInputMode
{
  override public function NoHandleGraphEditorInputMode(graph:IGraph=null,
                                                     selectionModel:ISelectionModel=null) {
    super(graph, selectionModel);
  }

  protected override function onHandleInputModeChanged(oldHandleInputMode:HandleInputMode,
                                                  newHandleInputMode:HandleInputMode):void {
    // nothing!
  }
}
}

Not all references are removed

Problem

Especially when the GraphCanvasComponent has been created in the MXML part there are a number of references which keep the component alive.

Solution

To subject the component to garbage collection it is necessary that all references are freed.

var group:Group = graphCanvas.parent as Group;
if (group != null) {
  group.removeElement(graphCanvas);
} else if (graphCanvas.parent != null) {
  graphCanvas.parent.removeChild(graphCanvas);
}
// The GraphEditorInputMode holds a reference to the GraphCanvasComponent, 
// so to free the GraphCanvasComponent it is necessary to uninstall and free the GEIM, too.    
graphCanvas.inputMode = null;
// if the GraphEditorInputMode is held in a field (or an MXML id), this has to be set to null
geim = null;
// Remove the GCC from all fields
graphCanvas = null;
In Flex 3, an input mode which is defined in MXML is kept in several references. If you are using Flex 3 and want to remove the GraphCanvasComponent, it is recommended to add the input mode in ActionScript.

Related article

The article Resources are not Released when the Graph is Cleared describes possible reasons why references to nodes and edges are kept after clearing a graph.

Categories this article belongs to:
yFiles FLEX > Other
Applies to:
yFiles FLEX: 1.4, 1.5, 1.6, 1.7, 1.8
Keywords:
resources - memory leak - profilier - profiling - garbage collection