Resources are not Released when the Graph is Cleared
TroubleshootingSummary
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
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 theGraphCanvasComponent.