Why use enif_alloc instead of malloc

Why use

void * enif_alloc (size_t size) as opposed to

void * malloc (size_t size); when trying to allocate memory from Erlang C NIF?

The link does not indicate much about the reasons.

http://www.erlang.org/doc/man/erl_nif.html#enif_alloc

I've seen NIF examples when malloc was used and I never see enif_alloc. What does he do differently? Why is it better to use?

+3


source to share


1 answer


enif_alloc uses erlang internal memory allocators, which means that if memory is already available in the VM's internal cache, it can use that memory instead of a system call to get memory. In some cases this can lead to faster memory allocation, you will have to measure your code to figure out if any value matters. In general, I would recommend using enif_alloc.



If I remember correctly, using enif_alloc also enables memory usage when issuing the erlang: memory command.

+8


source







All Articles