haproxy Memory pool Overview

Memory pools are classified by type , Each type of memory pool has a name , Use a linked list to record free memory blocks , Each memory block is equal in size , And in accordance with the 16 Byte alignment .

haporxy use pool_head Structure record memory pool

struct pool_head {
void **free_list; /* Free list */
struct list list; /* Double linked list , Link each type of memory pool */
unsigned int used; /* How many memory blocks are used */
unsigned int allocated; /* How many memory blocks are allocated */
unsigned int limit; /* Memory block upper limit */
unsigned int minavail; /* Keep at least a few , Not all of them will be recycled */
unsigned int size; /* Memory block size */
unsigned int flags; /* Can you share , Different types , But the same size , Can you share a pool_head */
unsigned int users; /* The memory pool has several users */
char name[12]; /* Memory pool name */

During program execution , The resulting memory pool , Most likely by size , Arrange in the following way :

The creation of memory pool

haproxy When creating a memory pool , Check the memory pool first , Is there a memory pool of the same size as required , Yes, and the memory pool can be shared , take pool_head.users++. If there is no , Create a new memory pool .

struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags)
struct pool_head *pool;
struct pool_head *entry;
struct list *start;
unsigned int align; // according to 16 Byte alignment
align = 16;
size = (size + align - 1) & -align;
//pools Global variable , The head node of the memory pool
start = &pools;
pool = NULL; list_for_each_entry(entry, &pools, list) {
if (entry->size == size) {
if (flags & entry->flags & MEM_F_SHARED) {// Equal size and shareable
pool = entry;
else if (entry->size > size) { // Memory pools are sorted by size , new pool_head, In place
start = &entry->list;
// Create a new memory pool
if (!pool) {
pool = CALLOC(1, sizeof(*pool));
if (!pool)
return NULL;
if (name)
strlcpy2(pool->name, name, sizeof(pool->name));
pool->size = size;
pool->flags = flags;
LIST_ADDQ(start, &pool->list);
return pool;

Memory application

create_pool Just apply for the type of memory pool , There is no specific allocation of memory , The task of allocating memory is done by pool_refill_alloc To complete

void *pool_refill_alloc(struct pool_head *pool)
void *ret; // If there is an upper limit on the number of memory blocks that can be applied for , And it has reached the upper limit , No more applications
if (pool->limit && (pool->allocated >= pool->limit))
return NULL;
ret = MALLOC(pool->size);
// If the application fails ,pool_gc2() Garbage collection , And then apply again , If you fail again, give up
if (!ret) {
ret = MALLOC(pool->size);
if (!ret)
return NULL;
return ret;

among ,pool_gc2() Garbage collection function , Will traverse all memory pools , And free up free memory blocks ( leave minavail The number of ).

User requests memory , It's not a direct call pool_refill_alloc, But by calling pool_alloc2, If free_list There's no free memory in , Call pool_refill_alloc Apply for memory . If you have free memory , Then use the first memory ,free_list Point to the next piece .

#define pool_alloc2(pool) \
({ \
void *__p; \
if ((__p = pool->free_list) == NULL) \
__p = pool_refill_alloc(pool); \
else { \
pool->free_list = *(void **)pool->free_list; \
pool->used++; \
} \
__p; \

Memory free

If the user of the memory block , After applying for a memory block , Don't actively free memory blocks , So after destroying the memory pool , Memory blocks will not return to memory . therefore , Must pay attention to , To actively free memory blocks .

The release of memory blocks is simple , Just put the memory block directly on the first node of the free list .

#define pool_free2(pool, ptr) \
({ \
*(void **)ptr = (void *)pool->free_list; \
pool->free_list = (void *)ptr; \
pool->used--; \
pool_gc2_ifneed(pool); \

Destroy the memory pool

The destruction of the memory pool is simple , Free all free memory blocks , Then release the memory pool . If there is still memory in use (pool->used != 0), Stop releasing

void *pool_destroy2(struct pool_head *pool)
if (pool) {
if (pool->used)
return pool;
if (!pool->users) {
return NULL;

among , pool_flush2 Function will directly free up all free memory

void pool_flush2(struct pool_head *pool)
void *temp, *next;
if (!pool)
return; next = pool->free_list;
while (next) {
temp = next;
next = *(void **)temp;
pool->free_list = next;

haproxy- Code reading - More articles on memory management

  1. Python Memory management mechanism -《 The source code parsing 》

    Python Memory management mechanism Python Memory management layered architecture /* An object allocator for Python. Here is an introduction to the layer ...

  2. oc Memory management summary ( One )

    ** memory management problem 1. What is? ios memory management ? When objects are no longer used , Clear it out of memory immediately 2. Why use memory management ? 1. Strict memory management , Can be our application has a great improvement in performance 2. If suddenly ...

  3. ios memory management 2- Memory management between objects

    Same as before , Create a new command line based project , Building a new Student And a class Book class Write the following code : Student.h // // Student.h // memory management 2- Memory management between objects // // ...

  4. iOS ARC Compiler rules and memory management rules

    iOS In development , Automatic reference counting is already a standard memory management solution . Except for some old projects or libraries, no one uses manual memory management . ARC It's no doubt a way to save developers from the tedious / Release the reference object from the logic . But it's not all right , ...

  5. 《 The future of code 》 Reading notes : Memory management and GC That thing

    One . Memory is limited In recent years , Our computer memory has several GB, Maybe your computer is 4G, His computer is 8G, Company server memory is 32G perhaps 64G. however , No matter how large the memory capacity is , It's not infinite . actually , As memory capacity increases , Software ...

  6. effective OC2.0 52 Reading notes ( 5、 ... and memory management )

    The fifth chapter : memory management 29 Understanding reference counting 30 With ARC Simplified reference counting summary :ARC Standardizing memory management rules through naming conventions . Other programming languages rarely look like OC This emphasizes naming .ARC By setting the global data structure ( The specific content of this data structure depends on ...

  7. iOS Memory management for performance optimization :Analyze、Leaks、Allocations The use and case code of

    Recently I took on a small task , And the company's iOS Let's share instruments Specific use of , So there's this blog ... Performance optimization is a big topic , This is mainly about memory leaks . One . Some related concepts A lot of people should know this better ...

  8. Objective-C Memory management and advanced environment programming Read and share

    Common debugging methods API uintptr_t objc_rootRetainCount(id obj) _objc_autoreleasePoolPrint();// View the objects in the auto release pool LLVM cl ...

  9. linux memory management --slab And its code parsing

    Linux The kernel uses data from Solaris One way , But this method has been used in embedded system for a long time , It allocates memory as an object according to its size , go by the name of slab Cache . The goal of memory management is to provide a way , It's true ...

Random recommendation

  1. Redis Time delay analysis and response

    Redis Time delay analysis and response Redis The event loop is handled in one thread , As a single threaded program , It is important to ensure that the delay of event processing is short , such , The subsequent tasks in the event loop will not block : When redis When the data volume reaches a certain level ( such as ...

  2. export Excel Gridview

    /// <summary>    /// Define export Excel Function of     /// </summary>    /// <param name="FileType ...

  3. Excel How to find and list all links in ( External data links )?

    stay Excel in , Sometimes you need to create external links to refer to the cell contents of other workbooks , But it's a bit difficult to find all the links and list them in a single workbook . Now I'll show you some quick ways , Not only can it help you find out ...

  4. H5 Front end interview questions and answers (2)

    I've been thinking about job hopping recently , But there are not many interviews , I'm a little upset . Sort out the mood , There's a lot of competition in the job hopping season , Try to be yourself ... 21. Please design a plan , Used to ensure that js Load full . <!doctype html> < ...

  5. JavaScript_01 brief introduction , Basic grammar , Operator

    JavaScript( No JScript and scriptease) 1.js It is divided into internal reference and external reference , Whether it's internal or external references , Can be placed in html( Except in the label ) Any position of , But the location of the definition affects the order of execution ...

  6. use ps Add atmosphere to the photo -- Lens halo

    1. Looking for a picture 2. Create a new layer and fill it in black 3. Choose a filter --- Rendering --- Lens halo 4. Select layer mode --- Filter color . ​ edit : Qian Feng UI Design

  7. hdu-4180-exgcd

    RealPhobia Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. mybatis Study ( Two )---- Do... On the watch CRUD operation

    One . Use MyBatis Execute on table CRUD operation —— be based on XML The implementation of the userMapper.xml The mapping file is as follows : <?xml version="1.0" encoding=&quo ...

  9. mysql 5.7.10 Start multi instance notes

    1. Copy profile cp /etc/my.cnf /etc/my3308.cnf 2. Modify the configuration file 3. Create directory , And give authority 4. Initialize database ---> There's a mistake 2018-01-03T0 ...

  10. Minimum cut D. Behind the Wall Samara University ACM ICPC 2016-2017 Quarterfinal Qualification Contest

    Topic link :http://codeforces.com/gym/101149/problem/D The main idea of the topic : The fort is under attack . The fortress is n*m Matrix , The matrix was flat at first , And then that number means building a wall on the current level ...