dict memory leak

McNamara's icon

Is there a way to make Max clear unused data it has stored in memory?

I have a patch that is tracking events by logging them in dicts. As the patch runs for several hours the RAM Max is using grows significantly.

I figured clearing the dicts would make the RAM usage drop, but it doesn't change.

I have read elsewhere on the forums that this is a product of how Max tracks characters and data upon creation regardless of whether they have been cleared via messages to a dict object.

Are there lower level controls of Max available to tell Max to clear a dict - or any other unused data - on a low-level or delete and any associated data from memory and start fresh; equivalent to restarting Max (without having to actually restart Max).

My current solution is quitting Max and starting it up again which isn't ideal.

Source Audio's icon

to my knowledge, it is not possible to clear symbol table cache
without restarting Max.
one can try to reduce memory usage by avoiding auto-created symbols
like forming strings with time stamps etc.
or for huge amount of data use different strategies, like writing to external files, using audio buffer to store floats or ???

McNamara's icon

do you know if writing to dicts in fact adds to the symbol table cache or could another part of my patch be the primary contributor to the RAM bloat?

I have another part of my patch that is saving symbols to a "value" object - maybe that's moreso where the issue lies? I am going to test a new patch that doesn't write symbols to a value object and see if that makes a significant difference

Source Audio's icon

I think it does not matter where symbols get stored.
you can run test using uzi to simulate events logging.
longer the symbols - more RAM gets used,
only unique symbols add to the table.

11OLSEN's icon

Try ";max size" in a message box and see the symbol count in the console. If it keeps increasing = not good for long term running Max app. Try reusing symbols and don't cast numbers to symbols in your logging.

McNamara's icon

thanks everyone

I tried ;max size and indeed my symbol count is slowly growing. This isn't a deal-breaker of a problem but it is for a permanent installation and I'd estimate the RAM will get bloated after about 14 hours of usage of the patch (if people are engaging with the system). Just would be nice to not have to restart Max.

I don't think I can store only numbers in a dict as JSON expects strings, not numbers for object names.

Is there anything as robust as a JSON format but doesn't use symbols? (coll and table are too simple)

11OLSEN's icon

Yes, the key names need to be symbols in dicts. But why not reuse the names. You either rework your logging or whatever is creating new symbols to make it (re)use a limited number of symbols or restart Max regularly maybe every 24 hours (Max app can check system clock and there's a message to restart itself)

You won't find an exception, any symbol entering Max land will be stored in the global symbol table until app quits.