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
Repeated calls to LSL.StreamInlet .info().channel_count() leaks memory #9
while (true) {
float[] sample = newfloat[inlet.info().channel_count()];
inlet.pull_sample(sample);
}
Whereas this works properly
float[] sample = newfloat[inlet.info().channel_count()];
while (true) {
inlet.pull_sample(sample);
}
I my experiment, I was collecting data at about 1 kHz. After an hour using the first snippet, my Java process was using about 20 GB. This obviously has an easy work around, but it does indicate that there is memory leaking somewhere (inside the native code?).
I'm not very good with Java, but the difference between the two snippets is that you're calling new to create new memory inside the loop vs outside. I don't know if you need to do manual cleanup here or if Java is expected to do garbage cleanup when you exit scope.
This doesn't seem like an LSL issue to me... unless you got the first snippet from a provided example?
If you found the pattern in the first snippet (new inside a while loop) in any example code or liblsl documentation then please open a documentation-focused issue.
The new float[], whether inside or outside the loop isn't a problem, The garbage collector can handle the extra allocations without problem. Allocating in the loop isn't great form. I've tested the following and it will show the memory leak as well.
while (true) {
inlet.info().channel_count();
}
Looking deeper into the code, the StreamInfo needs to be destroyed in order to avoid the leak.
while (true) {
varinfo = inlet.info();
info.channel_count();
info.destroy();
}
A better idiom here would be to have StreamInfo implement AutoCloseable. That way the IDE can let you know that you are not closing the resource.
As the examples are written, they all leak memory by not destroying the StreamInfo. Albeit, they leak very little. A super quick look over the codebase shows there are Pointer objects in many places, all of which could represent a memory leak if they are not destroyed.
Ah, I understand better now, thanks.
Well, I am not a Java developer and I doubt @tstenner is using liblsl on Android anymore. So I think this can only be fixed with a community PR.
Activity
cboulay commentedon Jun 20, 2024
I'm not very good with Java, but the difference between the two snippets is that you're calling
newto create new memory inside the loop vs outside. I don't know if you need to do manual cleanup here or if Java is expected to do garbage cleanup when you exit scope.This doesn't seem like an LSL issue to me... unless you got the first snippet from a provided example?
cboulay commentedon Jun 20, 2024
If you found the pattern in the first snippet (
newinside a while loop) in any example code or liblsl documentation then please open a documentation-focused issue.boulder-on commentedon Jun 21, 2024
The new float[], whether inside or outside the loop isn't a problem, The garbage collector can handle the extra allocations without problem. Allocating in the loop isn't great form. I've tested the following and it will show the memory leak as well.
Looking deeper into the code, the StreamInfo needs to be destroyed in order to avoid the leak.
A better idiom here would be to have StreamInfo implement AutoCloseable. That way the IDE can let you know that you are not closing the resource.
As the examples are written, they all leak memory by not destroying the StreamInfo. Albeit, they leak very little. A super quick look over the codebase shows there are Pointer objects in many places, all of which could represent a memory leak if they are not destroyed.
cboulay commentedon Jun 21, 2024
Ah, I understand better now, thanks.
Well, I am not a Java developer and I doubt @tstenner is using liblsl on Android anymore. So I think this can only be fixed with a community PR.