FMOD Engine User Manual 2.03

5. White Papers | Memory Management

Memory Management and Conservation

This white paper gives some pointers on how to use and save memory in the FMOD Engine by describing things that may not be so obvious upon first looking at the API.

Using a fixed size memory pool.

To make FMOD stay inside a fixed size memory pool, and not do any external allocs, you can use the Memory_Initialize function.
i.e.

result = FMOD::Memory_Initialize(malloc(4*1024*1024), 4*1024*1024, 0,0,0);  // allocate 4mb and pass it to the FMOD Engine to use.
ERRCHECK(result);

Note! that this uses malloc. On Xbox 360 and Xbox you must use a different operating system alloc such as XPhysicalAlloc otherwise FMOD may not behave correctly. See "Platform specific issues" tutorials for more information on this.

Note! that this function allows you to specify your own callbacks for alloc and free. In this case the memory pool pointer and length must be NULL. The 2 features are mutually exclusive.

Lowering sound instance overhead.

The FMOD_LOWMEM flag is used for users wanting to shave some memory usage off of the sound class. This flag removes memory allocation for certain features, such as the 'name' field, which aren't used often in games. If Sound::getName is called when this flag is set, it returns "(null)".

Using compressed samples.

The FMOD Engine can play ADPCM, AT9, MP2/MP3, Opus, and XMA data compressed, without needing to decompress it to PCM first. This can save a large amount of memory, at the cost of requiring more CPU time when the sound is played.

To enable this, use the FMOD_CREATECOMPRESSEDSAMPLE flag. When using formats other than the ones specified above or platforms that do not support those formats, this flag is ignored.

On platforms that support hardware decoding, using this flag results in the platform hardware decoder decompressing the data without affecting the main CPU. For information about what platforms support hardware decoding and which encoding formats they support, see the Platform Details chapter.

Note! Using FMOD_CREATECOMPRESSEDSAMPLE incurs a 'one off' memory overhead cost, as it allocates the pool of codecs required to play the encoding format of the sample data. For information on how to control this pool, see the following section.

Controlling memory usage with settings.

System::setSoftwareFormat maxinputchannels is default to 6 to allow up to 6 channel wav files to be played through FMOD's software engine. Setting this to a lower number will save memory across the board. If the highest channel count in a sound you are going to use is stereo, then set this to 2.
For sounds created with FMOD_CREATECOMPRESSEDSAMPLE, System::setAdvancedSettings allows the user to reduce the number of simultaneous XMA/ADPCM or MPEG sounds played at once, to save memory. The defaults are specified in the documentation for this function. Lowering them will reduce memory. Note the pool of codecs for each codec type is only allocated when the first sound of that type is loaded. Reducing XMA to 0 when XMA is never used will not save any memory.
For streams, setting System::setStreamBufferSize will control the memory usage for the stream buffer used by FMOD for each stream. Lowering the size in this function will reduce memory, but may also lead to stuttering streams. This is purely based on the type of media the FMOD streamer is reading from (ie CDROM is slower than harddisk), so it is to be experimented with based on this.
Reducing the number of Channels used will reduce memory. System::init and System::setSoftwareChannels give control over maximum number of virtual voices and real voices used. You will need to make sure you specify enough voices though to avoid Channel stealing.

Tracking FMOD memory usage.

Using Memory_GetStats is a good way to track FMOD memory usage, and also find the highest amount of memory allocated at any time, so you can adjust the fix memory pool size for the next time.