Introduction
The current version of redis supports tcmalloc, jemalloc, Apple's malloc, or redis's own zmalloc. Zmalloc manages the memory allocation, which is thread-safe, and the allocated memory must be sizeof (long.
Source code
Zmalloc. h zmalloc. c
Description of macro variables:
- Prefix_size: the size of the allocated memory is recorded before the actually allocated memory. It is similar to the memory data structure of malloc in the actual C, but C may be different depending on the specific environment.
- Have_malloc_size: Indicates whether other malloc functions are used to implement the malloc. Related functions can be used to directly obtain the allocated memory size, if not, the actual size + prefix_size is allocated based on zmalloc.
Others
In addition to the standard zamlloc, zcalloc, zrealloc, and zfree functions, zmalloc also provides the zstrdup function for dumping memory data to strings, obtain the zmalloc_used_memory function that has used the total memory and the zmalloc_set_oom_handler that sets the memory overflow handler. It also provides the actual material memory size used by the program to obtain RSS in the system.
For specific implementation, you can read the detailed source code. It is easy to read the source code based on the above.