Skip to content

Script language environments should be released #79

Open

Description

@hinerm

Running a simple script in the Fiji script editor:

#@ DatasetIOService dios
#@ String(value="Select file", visibility="MESSAGE") dragNdrop;
#@ File inputFile
#@ UIService ui

testImg = dios.open(inputFile.getPath())

ui.show(testImg)

with a large dataset, memory usage showed persistent growth over time (clicking on status bar didn't free memory). Even after the script editor and image are closed.

Either the engine or the bindings need to be released

Image

Activity

  1. added a commit that references this issue on Nov 16, 2026
    6ab27d5
  2. tferr commented on Nov 16, 2026

    @tferr
    Collaborator

    I am convinced this is fixed in #81 TextEditor has module and scriptInfo fields. These were never cleared after a normal run. Now they are unless the REPL mode is active. Closing it for now. Let's re-open it if the issue persists

  3. added a commit that references this issue on Nov 16, 2026
  4. hinerm commented on Nov 17, 2026

    @hinerm
    MemberAuthor

    The memory leak persists at least in the groovy script language. Using the above script and an 8GB image resulted in memory remaining claimed after closing the script editor.

    The path to GC root goes to the script engine itself so the fix has to go through there.

  5. reopened this on Nov 17, 2026
  6. tferr commented on Nov 17, 2026

    @tferr
    Collaborator

    @hinerm , This will nuke everything started by the script. E.g, this used to work, and it will now throw a MissingPropertyException:

    import javax.swing.*
    import java.awt.event.*
    
    counter = 0
    data = [1, 2, 3]
    
    def frame = new JFrame("Bindings Release Test")
    def buttonLabel = new JLabel(" Button: Click after the run has finished ")
    def workerLabel = new JLabel(" Worker: waiting 3 s... ")
    def button = new JButton("Read script variable")
    
    // Test 1: a listener that outlives the run
    button.addActionListener({ e ->
        try {
            counter++
            buttonLabel.text = "Button: OK (counter=" + counter + ")"
        } catch (Throwable t) {
            buttonLabel.text = "Button: FAILED (" + t.class.simpleName + ")"
        }
    } as ActionListener)
    
    // Test 2: A thread reading a variable after the script has returned
    Thread.start("bindings-worker") {
        Thread.sleep(3000)
        def msg
        try {
            msg = "Worker: OK (data=" + data + ")"
        } catch (Throwable t) {
            msg = "Worker: FAILED (" + t.class.simpleName + ")"
        }
        SwingUtilities.invokeLater { workerLabel.text = msg }
    }
    
    def panel = new JPanel(new java.awt.GridLayout(0, 1, 4, 4))
    panel.add(buttonLabel)
    panel.add(workerLabel)
    panel.add(button)
    frame.add(panel)
    frame.pack()
    frame.setVisible(true)
    
    return null; // 

    Some of the own Fiji example scripts demo how to attach listeners to an image, so at least those will brake. But maybe, it is not a frequent use case?

    Otherwise this would require some sort of preference checkbox in the Run menu that switches the clearing off entirely!? when on: everything works at the risk of memory problems. When off: Fixes #79 for everyone but more interactive scripts with callbacks or threads will break

  7. ctrueden commented on Nov 17, 2026

    @ctrueden
    Member

    I don't understand the example script in the issue report. Are you saying memory is not freed even after the shown image is closed again, due to persistent bindings? Can we make an MCVE that doesn't interact with SCIFIO's on-demand loading (like maybe it just allocates some big array)?

  8. reopened this on Nov 17, 2026
  9. hinerm commented on Nov 17, 2026

    @hinerm
    MemberAuthor

    I reverted the binding release commit and reopened the issue for now.

    @ctrueden correct. it's not an issue with SCIFIO; I included the heap dump to show that there is a clear path from the script bindings that prevents garbage collection of the image.

    I can't reproduce the issue with large array allocation because

    double[] data = new double[1_000_000_000]
    

    tries to process data as an output and produces an exception. Changing it to:

    double[] data = new double[1_000_000_000]
    
    String g = "hello"
    

    doesn't reproduce the issue so presumably it is an interaction with script inputs/outputs + the engine bindings specifically.

  10. hinerm commented on Nov 18, 2026

    @hinerm
    MemberAuthor

    @hinerm , This will nuke everything started by the script. E.g, this used to work, and it will now throw a MissingPropertyException:

    @tferr this is a fair point. I'm considering changing the binding release to a setting in Options and moving the release ONLY to when the script editor is closed (not after each run). What do think about a notice when closing the script editor asking if you want to dispose of the script environment, with a yes/no option and "remember my result" toggle?

  11. ctrueden commented on Nov 18, 2026

    @ctrueden
    Member

    @hinerm I would prefer no toggle if possible. What if instead, each script declares in its #@script metadata whether its bindings should persist somewhere more durable? Defaulting to false (so we keep the bindings nuke), but setting to true continues to allow the sorts of use cases @tferr is highlighting? Or it could be the responsibility of such scripts to make their data more durable by adding it to the object service, or making it a field of the gui it's tied to? Several ways forward here.

  12. tferr commented on Nov 18, 2026

    @tferr
    Collaborator

    @hinerm, I actually think the toggle would be fine, but I understand @ctrueden's point.

    The toggle could also be added now, and removed on upcoming releases if a more robust solution is found!? Just in case you guys decide to go with the toggle, here are some thoughts on how I see it:

     Run
     +--------------------------------+
     | (...)                          |
     | [ ] Auto-save Before Compiling |
     | Script Variables on Close   >  +--------------------------+
     |--------------------------------| ( ) Ask                  |
                                      | ( ) Release              |
                                      | (o) Keep                 |
                                      +--------------------------+
    
    +--------------------------------------------------------------+
    |  Release Script Variables?                                   |
    |                                                              |
    |  (?)  Scripts run in this session may still hold variables   |
    |       (images, results, etc.) in memory.                     |
    |                                                              |
    |       Release them now? Windows and listeners created by     |
    |       those scripts may stop working.                        |
    |                                                              |
    |       [ ] Remember my choice                                 |
    |                                                              |
    |              [ Release ]   [ Keep ]   [ Cancel ]             |
    +--------------------------------------------------------------+
    

    Do you have a way to know which runs have been properly cleared/collected? If yes, then the prompt could be skipped in those cases. Also, the script editor now opens markdown/json/yaml/etc files just fine, and it would be awkward to be prompted about this when nothing has run.

    A Vars button of sorts could also be added to the status bar as shortcut to the menu options with an extra 'Release Now” option that would free the variables without closing the editor. I would probably appreciate that if I were in a long session hitting a memory problem.

    FYI: The command palette also searches for tooltips so e.g., A menu item with a description of "Avoids memory leaks" is picked up when typing 'mem'

    (also, sorry for not profiling your original snippet, I had noticed that the module/scriptInfo fields were not cleared, and immediately thought that was the issue)

  13. hinerm commented on Nov 18, 2026

    @hinerm
    MemberAuthor

    @ctrueden @tferr I pushed a branch with my current proposal: https://github.com/scijava/script-editor/tree/binding-memory-leak

    It is very similar to the dialog @tferr just posted.

    image

    This adds a three-way setting in the script editor's Options to release bindings on exit. Default behavior is when closing the script editor you get asked if you want to clear the editor's variables.

    image

    @tferr's button counting script above is a great tester. As long as the script editor is open, the frame/button will continue to work.. If you close the editor and choose to clear them it will break, or continue working if you keep them.

    A menu item with a description of "Avoids memory leaks" is picked up when typing 'mem'

    lol yeah.. by putting this in the script editor options it doesn't appear in search results.

    also, sorry for not profiling your original snippet

    No worries at all. I appreciate bringing attention to this issue so that we resolve it, and that you pushed back when my original change was short-sighted!

    I would prefer no toggle if possible. What if instead, each script declares in its #@script metadata whether its bindings should persist somewhere more durable?

    @ctrueden My concern with defaulting to "nuke" and not informing the user is that this will change behavior without clearly giving the user a path to the fix. Putting a setting like this in #@script syntax is reasonable and I think could be added as an override for editor settings, but I think learning new syntax is a barrier to adoption.

    I can appreciate that adding more dialogs/options is annoying but don't feel too bad about a potential one-time question at close. How strong is your objection to the current implementation?

  14. self-assigned this
    on Nov 18, 2026
  15. tferr commented on Nov 18, 2026

    @tferr
    Collaborator

    I was thinking the Run menu would be a better fit, but Options makes sense too.

    by putting this in the script editor options it doesn't appear in search results.

    Strange. It should work. This bogus menu entry is picked up on 'fancy' and 'osten':

    final JRadioButtonMenuItem item = new JRadioButtonMenuItem("my fancy item");
    item.setToolTipText("ostentatious item");
    

    There is one catch: Internally the command palette uses a map using the items' labels as keys. So if two entries are added with the same text, only the last is considered. That should probably be fixed!? Let me know if it is not working, and I can have a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions