mastodon-sc / mastodon

Mastodon – a large-scale tracking and track-editing framework for large, multi-view images.
BSD 2-Clause "Simplified" License
66 stars 20 forks source link

Mastodon Memory Leak #225

Open maarzt opened 1 year ago

maarzt commented 1 year ago

Problem description

I noticed that mastodon objects like WindowManger, ProjectManager, AppModel etc. don't get garbage collected after the project is closed.

This can be a problem for users. Fiji might run out of memory when opening a large number of Mastodon projects, one after the other. The Eclipse Memory Analyzer is a handy tool to inspect such problems. The org.mastodon.spatial.SpatioTemporalIndexImpRebuilderThread seems to stay in memory and keeps a reference to the ModelGraph.

TODOs

Expected Behavior

All Mastodon related objects (WindowManager, ProjectManager, AppModel, ModelGraph, etc. ) get garbage collected after a Mastodon project is closed.

tinevez commented 1 year ago

Argh the horror.

maarzt commented 1 year ago

The following few lines of code are enough to trigger an OutOfMemoryException:

package org.mastodon.mamut.tomancak;

import org.junit.Test;
import org.mastodon.mamut.model.Model;

public class MastodonMemoryTest
{
    @Test
    public void test() {
        for ( int i = 0; i < 100; i++ )
        {
            new Model();
        }
    }
}

The problem seems to be related to the UndoRedoStack. See stack trace:

java.lang.OutOfMemoryError: Java heap space

    at org.mastodon.undo.ByteArrayUndoRedoStack.<init>(ByteArrayUndoRedoStack.java:122)
    at org.mastodon.undo.GraphUndoRedoStack.<init>(GraphUndoRedoStack.java:90)
    at org.mastodon.undo.GraphUndoRecorder.<init>(GraphUndoRecorder.java:98)
    at org.mastodon.mamut.model.Model.<init>(Model.java:178)
    at org.mastodon.mamut.model.Model.<init>(Model.java:122)
    at org.mastodon.mamut.tomancak.MastodonMemoryTest.test(MastodonMemoryTest.java:12)
stefanhahmann commented 1 year ago

@maarzt Could you ask for review/approval/merge of the related PR https://github.com/bigdataviewer/bigdataviewer-core/pull/163 so that the temporarily introduced PainterThread of this PR does not live longer than necessary?

tpietzsch commented 1 year ago

I'll review it today

tpietzsch commented 1 year ago

merged and released with bigdataviewer-core-10.4.7

tinevez commented 1 year ago

Could we depend on bigdataviewer-core-10.4.7 in mastodon and use the code from there? Or do we have specifics in Mastodon?

maarzt commented 1 year ago

This issue if fixed with the following PRs being merged and mastodon-graph-1.0.0-beta-24 and bigdataviewer-core-10.4.7 being released:

tinevez commented 10 months ago

Hello @maarzt Really thank you the tests you build sweat talent and skills.

Unfortunately I tested the org.mastodon.mamut.GarbageCollectionTest and it still fails, even with