When pressing A, I'm loading ~ 400 MB of sound data in memory (example: loading Piano samples).
When pressing B, anothing preset is loaded with another 400 MB (example: Violin preset). Idem when pressing C (let's say Trumpet preset), etc.
Please note that only one instrument is played at a certain time, so basically when C is loaded, we don't need A, B, D, E, etc. anymore.
But I would like to keep A, B, C, ..., Z in memory as long as there is enough free memory, so that if I switch quickly from C to A, the data is still in memory... and so I don't need to wait for data to be loaded.
But because memory is not infinite, I would like that if we are close to full memory, the oldest things are erased. Like a garbage collector but that empties bin only if very few free memory is available.
Should I handle this myself, like this :
presets_in_memory = []
def button_press_callback(key):
if key == A:
presets_in_memory.append(Preset(1))
if key == B:
presets_in_memory.append(Preset(2))
...
while (FreeMemory() < 100 000 000):
del presets_in_memory[0] # empties the oldest objects while free mem < 100MB
# so that the garbage collector
# will free their memory
or let another system / module do this for me?