There is a lot of memory learning on the Internet. The purpose of this learning is to convert memory usage into your own things and use them elsewhere. The general idea is to turn the memory into zero.
Reference other article address: http://blog.csdn.net/v_july_v/article/details/7040425
Draw a picture here:
The structure of the memory pool is relatively simple. Note the following:
1) The memory pool must be created before use, as shown in figure
Pool = ngx_create_pool (1024, null );
2) When the memory pool is used, the memory is allocated through P = ngx_palloc (pool, 512.
3) The size of the first memory block is slightly different from that of the subsequent memory block. Taking a memory pool of 1024 as an example, the size of the Data zone of the first memory block is 1024-sizeof (ngx_pool_s) = 984. The size of the second and subsequent memory blocks is 1024-sizeof (ngx_pool_data_t) = 1008, as shown in.
4) for the field failed, the source code contains the following code:
For (P = current; P-> D. Next; P = p-> D. Next ){
If (p-> D. Failed ++> 4 ){
Current = p-> D. Next;
}
}
I have never understood it before. Many people on the Internet say it is a group of experience values. Later I wrote some test code and kept allocating the memory pool. I suddenly figured it out, also understand that pool-> current does not necessarily point to the current memory block. understanding:
The memory pool is actually a chain structure. If the memory is always allocated from the beginning, once there are too many blocks in the memory pool, the efficiency will be affected. Therefore, a current pointer is added, when the allocation policy reaches a certain level, the current will be moved back, and the subsequent allocation will no longer use the memory before the current (even if the previous memory block is available), so as to change the space for time, make the efficiency as high as possible.
5) Once ngx_palloc is called for memory pool allocation, P points to the allocated memory. At this time, the last variable in the current memory pool has pointed to the end of the current memory. Pay special attention to this.
The test code is as follows:
void dump_pool(ngx_pool_t* pool){ while (pool) { printf("pool = 0x%x\n", pool); printf(" .d\n"); printf(" .last = 0x%x\n", pool->d.last); printf(" .end = 0x%x\n", pool->d.end); printf(" .next = 0x%x\n", pool->d.next); printf(" .failed = %d\n", pool->d.failed); printf(" .max = %d\n", pool->max); printf(" .current = 0x%x\n", pool->current); printf(" .chain = 0x%x\n", pool->chain); printf(" .large = 0x%x\n", pool->large); printf(" .cleanup = 0x%x\n", pool->cleanup); printf(" .log = 0x%x\n", pool->log); printf("available pool memory = %d\n\n", pool->d.end - pool->d.last); pool = pool->d.next; }}int test_main(){ ngx_pool_t *pool; char *p = NULL; int i = 0; printf("sizeof(ngx_pool_t) = %d\n", sizeof(ngx_pool_t));//ngx_pool_data_t printf("sizeof(ngx_pool_data_t) = %d\n", sizeof(ngx_pool_data_t)); printf("--------------------------------\n"); printf("create a new pool:\n"); printf("--------------------------------\n"); pool = ngx_create_pool(1024, NULL); dump_pool(pool); for (i = 0; i < 10; i++) { printf("--------------------------------\n"); printf("alloc block %d from the pool:\n", i+1); printf("--------------------------------\n"); p = ngx_palloc(pool, 512); dump_pool(pool); } ngx_destroy_pool(pool); return 0;}