Resources are not Released when the Graph is Cleared

Troubleshooting

Summary

This article describes the possible reasons when resources like nodes or edges are not released after the graph is cleared.

Description

Memory leak in the library

Problem

Earlier versions of yFiles FLEX did not release the node and edge instances after the graph was cleared by IGraph's clear() method. This was due to a bug in the rendering mechanism. This bug is fixed in yFiles FLEX 1.3.2.1 versions and yFiles FLEX 1.4 and higher.

Solution

Update to the latest yFiles FLEX version (1.4 and higher). If you need to keep yFiles FLEX 1.3, update to version 1.3.2.1.

References to the nodes are kept by IMappers

Problem

Objects (like nodes or edges) which represent keys in IMapper's default implementation, DictionaryMapper won't be collected by the garbage collection, even if this is the only reference to their instance. They will be collected if the DictionaryMapper is created having "weak" key references. Unfortunately, this is not the default.

Solution

Create IMappers always as DictionaryMappers with "weak" key references:

// passing "true" as first parameter in the constructor
// will create a mapper with weak key references
var map:IMapper = new DictionaryMapper(true);

// when adding a new mapper to the registry
// add explicitly a mapper with weak keys
graph.mapperRegistry.addMapper("MapperName", new DictionaryMapper(true));

Using the undo mechanism

The undo engine is not enabled by default. If you didn't enable it explicitly, you might skip this section.

Problem

The undo engine will keep references to once created nodes forever (thus, a "clear" will be undoable) or until you explicitly clear the undo engine. Thus, the undo engine will keep the graph object instances alive.

Solution

The undo engine can be cleared:

var engine:UndoEngine = graph.lookup(UndoEngine) as UndoEngine;
if (engine != null) {
  engine.clear();
}

Keeping references in other lists

Inspect your code whether you are keeping references to graph objects after clearing the graph. If you are using a profiling tool, look for references from outside the yFiles FLEX library.

Other problems

Due to an implementation as singleton, some style renderers can keep instances alive, even if they are removed from the graph. Those instances will be set free when the renderer is invoked the next time.

Profiling pitfalls

The Flex Builder Profiler lists all instances in memory, even those which are not references anymore. Those objects will be kept in memory until a garbage collection is started.

Always start a garbage collection before inspecting the live objects or getting a memory snapshot to be sure to get a list which consists only of instances which are alive.

Related article

The article Resources are not Released when the GraphCanvasComponent is Removed from the View describes how to free resources after removing the GraphCanvasComponent.

Categories this article belongs to:
yFiles FLEX > Other
yFiles FLEX > Displaying and Editing Graphs > Graph Structure
Applies to:
yFiles FLEX: 1.3, 1.4, 1.5, 1.6, 1.7, 1.8
Keywords:
resources - memory leak - profilier - profiling - garbage collection