[solved] Loading + releasing samples from memory while playing

I’m making a long-form piece with Sonic Pi that’s going to be running for some time. I’m considering using a large selection of samples, but only playing/using 2 or 3 at a time. I’m wondering what the best practice is to prevent problems with samples eating up RAM.

If I load a sample as follows:

set :mysample, "C:/Users/me/Desktop/loop1.wav"

And later change the sample by doing something like:

set :mysample, "C:/Users/me/Desktop/loop2.wav"

Is the original loop1.wav still taking up memory? If I hypothetically did this, for example, in a Sonic Pi session that lasted for days, would I eventually run into trouble?

If so, how can I safely release samples once I’m done with them and free-up the memory?

Hi there @drmindflip

calling set :mysample "C:/Users/me/Desktop/loop1.wav" doesn’t load the sample into memory. It simply stores the string or sentence "C:/Users/me/Desktop/loop1.wav" in the time state. This does take a tiny bit of memory up, but it won’t cause a memory leak - unless you call set with lots and lots and lots of different keys such as :mysample2, :mysample3… etc. Essentially you should never worry about using set lots of times.

However, calling sample "C:/Users/me/Desktop/loop1.wav" will load the sample into memory and then play it. Similarly load_sample "C:/Users/me/Desktop/loop1.wav" will load the sample into memory although it will not play it. Both of these will consume a lot of memory (especially if the sample is long). Calling sample or load_sample with lots of different samples will cause an eventual memory leak and is something to be concerned about if you’re loading many unique samples.

In order to deal with this, you can use the functions sample_free to unload a specific sample from memory (i.e. sample_free "C:/Users/me/Desktop/loop1.wav") or sample_free_all to unload all samples from memory.

Hope that this helps!

1 Like

Ah that’s exactly what I was looking for Sam, thanks a million! The extra clarity on when exactly the sample gets loaded into memory is very helpful too.

So, whenever I’m loading a new sample to replace one, I’ll be sure to use sample_free or sample_free_all to keep things tidy in the background.

Is there a way to view a list of all the samples currently loaded in memory? If not, I guess I can use sample_loaded? after sample_free to make sure it’s gone!

Sorry, there’s not currently a way of listing all the loaded samples - although you shouldn’t need to check whether a sample has been freed with sample_free unless you don’t trust my code! It should definitely free the sample and if it doesn’t, then that’s a bug I’d be very happy to address and fix.

1 Like

Sounds great Sam, thanks for the helpful and detailed advice. And it’s certainly more a case of me not trusting my own code! I’ll just be careful to sample_free dilligently - that’s perfect for what I need.

PS - small related question:

If I have a block of code calling sample "C:/Users/me/Desktop/loop1.wav, and another block of code does the exact same thing simultaneously, will loop1.wav be loaded into RAM twice? Or will subsequent instances of sample "C:/Users/me/Desktop/loop1.wav use the data that has already been loaded?

Hi, loading the same sample twice or more via sample or load_sample doesn’t use any more memory than just loading it once. Sonic Pi is smart enough to cache the loaded sample and re-use it again. So no worries there.

1 Like

That’s awesome Sam, cheers. Makes working with these functions a breeze :slight_smile:

1 Like