Page 1 of 1

memory leak during set joint coordinate value

Posted: Mon Feb 03, 2020 3:01 pm
by zannecox
I'm running into a memory leak when scripting in Matlab. Here's code to reproduce:

import org.opensim.modeling.*

[Model_In, path] = uigetfile('.osim');
Model1 = Model(fullfile(path, Model_In));
state =Model1.initSystem();

for i=1:3000
Model1.updCoordinateSet().get('ankle_flexion').setValue(state,0.4);
state =Model1.initSystem();
end

Model1.disownAllComponents();

Either initializing the system or updating the ankle flexion coords leads to ever increasing memory used and it won't be released by matlab. The initialization is much worse than the setting a coordinate value. Suggestions?

Re: memory leak during set joint coordinate value

Posted: Mon Feb 03, 2020 5:08 pm
by tkuchida
There's no need to call initSystem() within the "for" loop. The initSystem() method creates a new underlying computational system, which is not necessary in the "for" loop because setting a coordinate's value does not change the underlying description of the model's dynamics. The "System and State" section of the API Guide provides some additional information on this topic: https://simtk.org/api_docs/opensim/api_ ... ystemstate.

Re: memory leak during set joint coordinate value

Posted: Tue Feb 04, 2020 8:17 am
by aravind
you are probably trying to do:

Code: Select all

[Model_In, path] = uigetfile('.osim');
Model1 = org.opensim.modeling.Model(fullfile(path, Model_In));
state =Model1.initSystem();
ankle_flexion = Model1.getCoordinateSet().get('ankle_flexion')

for i=1:3000
	ankle_flexion.setValue(state,0.4);
	Model1.realizeAcceleration(state) %or some other realize function here
end

Re: memory leak during set joint coordinate value

Posted: Tue Feb 04, 2020 8:53 am
by zannecox
That code was just example code to isolate the problem. I am doing an optimization of wrapping surfaces to match experimental moment arm data. During each call to my objection function, I modify the wrap surface parameters and need to re-initialize the system in order for the changes to take effect (at least according to this thread: viewtopicPhpbb.php?f=91&t=6123&p=30846&start=0&view= )and evaluate the resulting moment arm at a series of joint angles. I isolated my leak down to those two lines.

Re: memory leak during set joint coordinate value

Posted: Tue Feb 04, 2020 9:46 am
by aravind
zannecox wrote:
Tue Feb 04, 2020 8:53 am
That code was just example code to isolate the problem. I am doing an optimization of wrapping surfaces to match experimental moment arm data. During each call to my objection function, I modify the wrap surface parameters and need to re-initialize the system in order for the changes to take effect (at least according to this thread: viewtopicPhpbb.php?f=91&t=6123&p=30846&start=0&view= )and evaluate the resulting moment arm at a series of joint angles. I isolated my leak down to those two lines.
something like what James posted in that thread, I still don't get a memory leak.

Code: Select all

modelPath = '.\legacy\models\1 joint\simple_1link.osim';%model path
geometryPath='C:\OpenSim 4.0\Geometry';
ModelVisualizer.addDirToGeometrySearchPaths(geometryPath);

myModel = org.opensim.modeling.Model(modelPath)
state =myModel.initSystem();

for i=1:3000
	wrap = myModel.getBodySet().get(0).getWrapObjectSet().get(0)
	prop = wrap.getPropertyByName('translation')
	x_translation = org.opensim.modeling.PropertyHelper.getValueVec3(prop,1)
	org.opensim.modeling.PropertyHelper.setValueVec3(x_translation + 1 ,prop, 1)
	state =myModel.initSystem();
end

Re: memory leak during set joint coordinate value

Posted: Tue Feb 04, 2020 10:48 am
by zannecox
Hmmm, weird. If I run the same code you did, at first the memory used by matlab stays pretty constant, but after a bit (that takes a few minutes to run 3000 iterations on my slow computer) - the memory usage starts to climb. And no matter what I do from matlab - I can't clear it unless I restart the program. If you don't get the same behavior, it makes me wonder if it has something do to with the model I'm loading. I'll try another and see if I get the same results.

Re: memory leak during set joint coordinate value

Posted: Tue Feb 04, 2020 10:56 am
by zannecox
Loading arm26.osim from the example models, I don't get the same behavior. Memory usage stays constant. Must be something about my model. Maybe that it uses the Schutte muscle model? I tend to blame everything weird on that. :) Thanks for your quick replies. I'll dig around some more to figure out