0

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?

Basj
  • 41,386
  • 99
  • 383
  • 673
  • Have you tried importing gc (garbage collection) and manually relessing the memory when you want? – reticentroot Mar 19 '15 at 23:41
  • @hrand How to use it in such a context and how to tell `gc` which one to keep, which one to not keep, when to erase them? – Basj Mar 19 '15 at 23:44
  • Perhaps you should take a look here to get an idea https://docs.python.org/2/c-api/memory.html – reticentroot Mar 19 '15 at 23:47
  • I would simply let the operating system handle this. It should cache the files as long as there is enough memory. – BlackJack Mar 20 '15 at 16:50

0 Answers0