Memory leak when accessing dict

woyteg's icon

woyteg

6月 03 2024 | 12:42 午後

Hi,

long time max user, experienced python user and novice in java script here.

I seem to experience some kind of memory leak when repeatedly accessing a dict through js. The attached patch tries to replicate my issue with minimal code. In reality i am building some sort of sequencer and dont plan to query the js object every millisecond but this seems to replicate/isolate the issue nicely.

Hope some of you find my mistake obvious and its not an actual bug.

Thanks in advance!

Here is the script:

outlets = 1
var dict = new Dict('testing');
keys = dict.getkeys();
function bang(){
	keys = dict.getkeys();
	foo = dict.get(keys[0]);
	outlet(0,foo);
	return
}

Contents of the dict:

{
	"test" : [ 1, 2, 3, 4, 5, 6, 7, 8 ],
	"test2" : [ 9, 8, 7, 6, 5, 4 ]
}

Max Patch
Copy patch and select New From Clipboard in Max.

JBG's icon

JBG

6月 03 2024 | 6:58 午後

I'm not sure that it's a bug, but I don't think it's specifically related to dictionaries but rather to js in general, in particular with overdrive on.

Javascript is... slow. And when you call a js function faster than it is executed, the consecutive calls will be scheduled to be executed afterwards.

Here's an even simpler example:

function bang() {
	outlet(0, 0);
}
Max Patch
Copy patch and select New From Clipboard in Max.

Running this for a few seconds will lead to Max using up a a few GBs of RAM from scheduling loads of js calls, but once you stop it, the RAM usage will gradually decrease again, as it handles the queued calls without any new ones being queued.

(This is on both an M2 and an Intel MacBook Pro on Max 8.6.2, but as far as I know this issue has been around for at least a couple of years)