ArmA 2: Custom Memory Allocator

This page is about Arma 2 functionality. For similar functionality in Arma 3, see Arma 3: Custom Memory Allocator.
Since Arma 2 Operation Arrowhead build 85869 (1.60 beta) it is possible to provide custom memory allocators for the game.

The memory allocator is a very important component, which significantly affects both performance an stability of the game.

The purpose of this customization is to allow the allocator to be developed independently on the application,

allowing both Bohemia Interactive and community to fix bugs and improve performance without having to modify the core game files.

## Default

Default allocator used by engine until build 1.60.87645 is based on Intel TBB 3 (see details about **tbb3malloc\_bi** below)

Default allocator used by engine after build 1.60.87645 is based on Intel TBB 4 (see details about **tbb4malloc\_bi** below)

## Specifying a custom allocator

The allocator is a dll placed in a directory named "dll" located next to the game executable. Allocator search order is:

* tbb3malloc\_bi - based on Intel TBB 3, distributed under GPL v2 + RE (source code), based on tbb30\_20110427oss
* tbb4malloc\_bi - based on Intel TBB 4, distributed under GPL v2 + RE (source code) based on tbb43\_20141204oss
* jemalloc\_bi - based on JEMalloc, distributed under BSD-derived license (source code)
* tcmalloc\_bi - based on TCMalloc, distributed under New BSD license (source code) based on revision 100
* nedmalloc\_bi - based on NedMalloc, distributed under Boost Software License (source code)
* customMalloc\_bi - not provided, feel free to plug-in your own

If no allocator dll is found, functions \_aligned\_malloc/ \_aligned\_free (using Windows Heap functions) are used as a fallback

*note: Windows 7 allocator seems to be quite good, and it may therefore make sense for some users to delete all custom allocators on Windows 7 or newer).*

You can select an allocator by via **commandline** below or *deleting other allocators* from the \dll\ folder.

### Commandline parameter

You can specify a particular allocator from a command line, like:

* **-malloc=tbb3malloc\_bi**
* **-malloc=tbb4malloc\_bi**
* **-malloc=jemalloc\_bi**
* **-malloc=tcmalloc\_bi**
* **-malloc=nedmalloc\_bi**

or

* **-malloc=mybestmalloc\_bi**
* **-malloc=system** *can be used to force using Windows allocator even when allocator dlls are present*

*(dll directory and extension are appended automatically, the allocator must not be located in other directory and its name must not contain any dots before the .dll extension)*

### Dedicated server

You can specify allocator for Windows dedicated server the same way as for client binary, but expected performance gain is minimal, because of low concurrency of the dedicated server code compared to a client.

Linux server uses allocator provided by operating system. *There are NO plans to allow its customization*.

## DLL Interface

The dll interface is as follows:

```
extern "C" {
 \_\_declspec(dllexport) size\_t \_\_stdcall MemTotalCommitted(); // \_MemTotalCommitted@0
 \_\_declspec(dllexport) size\_t \_\_stdcall MemTotalReserved(); // \_MemTotalReserved@0
 \_\_declspec(dllexport) size\_t \_\_stdcall MemFlushCache(size\_t size); // \_MemFlushCache@4
 \_\_declspec(dllexport) void \_\_stdcall MemFlushCacheAll(); // \_MemFlushCacheAll@0
 \_\_declspec(dllexport) size\_t \_\_stdcall MemSize(void \*mem); // \_MemSize@4
 \_\_declspec(dllexport) void \*\_\_stdcall MemAlloc(size\_t size); // \_MemAlloc@4
 \_\_declspec(dllexport) void \_\_stdcall MemFree(void \*mem); // \_MemFree@4
};

```

Note: besides of the interface above, if the allocator is performing any per-thread caching, it will typically want to perform a cleanup of per-thread data on DLL\_THREAD\_DETACH event sent to DllMain function.

### MemTotalCommitted()

Total memory committed by the allocator (should correspond to VirtualAlloc with MEM\_COMMIT)

### MemTotalReserved()

Total memory reserved by the allocator (should correspond to VirtualAlloc with MEM\_RESERVE)

### MemFlushCache(size\_t size)

Try to flush at least "size" bytes of memory from caches and working areas, return how much memory was flushed. Called by game when memory needs to be trimmed to reduce virtual memory use.

### MemFlushCacheAll()

Flush all memory held in caches and working areas. Called by game when memory needs to be trimmed to reduce virtual memory use.

### MemSize(void \*mem)

Return allocated size of given memory block.

### MemAlloc(size\_t size)

Allocate at least size bytes of memory, return the allocated memory. If the size is 16 B or more, the memory must be 16 B -aligned, so that it is usable to hold SSE data.