You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Script language environments should be released #79
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
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
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.
@hinerm , This will nuke everything started by the script. E.g, this used to work, and it will now throw a MissingPropertyException:
importjavax.swing.*importjava.awt.event.*
counter =0
data = [1, 2, 3]
def frame =newJFrame("Bindings Release Test")
def buttonLabel =newJLabel(" Button: Click after the run has finished ")
def workerLabel =newJLabel(" Worker: waiting 3 s... ")
def button =newJButton("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 +")"
}
} asActionListener)
// Test 2: A thread reading a variable after the script has returnedThread.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 =newJPanel(newjava.awt.GridLayout(0, 1, 4, 4))
panel.add(buttonLabel)
panel.add(workerLabel)
panel.add(button)
frame.add(panel)
frame.pack()
frame.setVisible(true)
returnnull; //
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
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)?
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.
@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?
@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.
@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)
It is very similar to the dialog @tferr just posted.
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.
@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?
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
Activity
Fix #79
tferr commentedon Nov 16, 2026
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
ScriptInstaller: Fix memory leak.
hinerm commentedon Nov 17, 2026
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.
tferr commentedon Nov 17, 2026
@hinerm , This will nuke everything started by the script. E.g, this used to work, and it will now throw a
MissingPropertyException: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
ctrueden commentedon Nov 17, 2026
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)?
hinerm commentedon Nov 17, 2026
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
tries to process
dataas an output and produces an exception. Changing it to:doesn't reproduce the issue so presumably it is an interaction with script inputs/outputs + the engine bindings specifically.
hinerm commentedon Nov 18, 2026
@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?
ctrueden commentedon Nov 18, 2026
@hinerm I would prefer no toggle if possible. What if instead, each script declares in its
#@scriptmetadata 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.tferr commentedon Nov 18, 2026
@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:
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
Varsbutton 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)
hinerm commentedon Nov 18, 2026
@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.
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.
@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.
lol yeah.. by putting this in the script editor options it doesn't appear in search results.
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!
@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
#@scriptsyntax 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?
tferr commentedon Nov 18, 2026
I was thinking the Run menu would be a better fit, but Options makes sense too.
Strange. It should work. This bogus menu entry is picked up on 'fancy' and 'osten':
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