2
votes

I run a c++ program that uses jemalloc as memory allocator which pre-divides big chunks into small chunks of pre-defined sizes (i.e. 1, 2, 4, 8, ... bytes)

Even though I ask 110 bytes of memory allocation, it returns a memory with 128 bytes capacity.

In my program, I track the amount of dynamically allocated memory (with highly diverse size) and limit the memory allocation of threads to avoid OutOfMemory crash.

However, due to the discrepancy between the size requested and the actual size granted, I cannot exactly count the amounts of dynamically allocated bytes.

Is there any 'jemalloc' API that receives a request size as an input and provides an actual allocation size as an output?

Thanks

1

1 Answers

3
votes

As per the documentation, you can use malloc_usable_size() and passing your allocated pointer.

The malloc_usable_size function returns the usable size of the allocation pointed to by ptr. The return value may be larger than the size that was requested during allocation. The malloc_usable_size function is not a mechanism for in-place realloc; rather it is provided solely as a tool for introspection purposes. Any discrepancy between the requested allocation size and the size reported by malloc_usable_size should not be depended on, since such behavior is entirely implementation-dependent.

More info :