最近nginx的源码刚好研究到内存池,这儿就看下nginx内存池的相关的东西。

一,为什么要使用内存池

  大多数的解释不外乎提升程序的处理性能及减小内存中的碎片,对于性能优化这点主要体现在:
  (1)系统的malloc/free等内存申请函数涉及到较多的处理,如申请时合适空间的查找,释放时的空间合并。
  (2)默认的内存管理函数还会考虑多线程的应用,加锁操作会增加开销。
  (3)每次申请内存的系统态与用户态的切换也及为的消耗性能。
  对于由于应用的频繁的在堆上分配及释放空间所带来的内存碎片化,其实主流的思想是认为存在的,不过也有人认为
这种考虑其实是多余的,在“内存池到底为我们解决了什么问题”一文中则认为,大量的内存申请与释放仅会造成短暂
的内存碎片化的产生,并不会引起大量内存的长久碎片化,从而导致最后申请大内存时的完全不可用性。文中认为对于
确定的应用均是“有限对象需求”,即在任一程序中申请与释放的对象种类总是有限的,大小也总是有一定重复性的,
这样在碎片产生一段时间后,会因为同样的对象申请而消除内存的临时碎片化。

  不过,综上,内存池有利于提升程序在申请及释放内存时的处理性能这点是确定的。内存池的主要优点有:
  (1)特殊情况的频繁的较小的内存空间的释放与申请不需要考虑复杂的分配释放方法,有较高的性能。
  (2)初始申请时通常申请一块较大的连续空间的内存区域,因此进行管理及内存地址对齐时处理非常方便。
  (3)小块内存的申请通常不用考虑实际的释放操作。

二,内存池的原理及实现方法

内存池的原理基本是内存的提前申请,重复利用。其中主要需要关注的是内存池的初始化,内存分配及内存释放。
内存池的实现方法主要分两种:
一种是固定式,即提前申请的内存空间大小固定,空间划分成固定大小的内存单元以供使用如下图示:

上图引用自http://www.ibm.com/developerworks/cn/linux/l-cn-ppp/index6.html

另一种是动态式,即初始申请固定大小的单元,空间不做明确的大小划分,而是根据需要提供合适大小的空间以供使用。
不过,这两种方式在处理大空间的内存申请时的处理方法通常都是采用系统的内存申请调用。

三,nginx的内存池实现

nginx的内存池设计得非常精妙,它在满足小块内存申请的同时,也处理大块内存的申请请求,同时还允许挂载自己的数据区域
及对应的数据清楚操作。
nginx内存池实现主要是在core/ngx_palloc.{h,c}中,一些支持函数位于os/unix/ngx_alloc.{h,c}中,支持函数主要是对原有的
malloc/free/memalign等函数的封装,对就的函数为:
》ngx_alloc 完成malloc的封装
》ngx_calloc 使用malloc分配空间,同时使用memset完成初始化
》ngx_memalign 会根据系统不同而调用不一样的函数处理,如posix系列使用posix_memalign,windows则不考虑对齐。主要作用
         是申请指定的alignment对齐的起始地址的内存空间。
》ngx_free 完成free的封装

nginx内存池中有两个非常重要的结构,一个是ngx_pool_s,主要是作为整个内存池的头部,管理内存池结点链表,大内存链表,
cleanup链表等,具体结构如下:

  1. //该结构维护整个内存池的头部信息
  2.  
  3. struct ngx_pool_s {
  4. ngx_pool_data_t d; //数据块
  5. size_t max; //数据块大小,即小块内存的最大值
  6. ngx_pool_t *current; //保存当前内存值
  7. ngx_chain_t *chain; //可以挂一个chain结构
  8. ngx_pool_large_t *large; //分配大块内存用,即超过max的内存请求
  9. ngx_pool_cleanup_t *cleanup; //挂载一些内存池释放的时候,同时释放的资源
  10. ngx_log_t *log;
  11. };

另一重要的结构为ngx_pool_data_s,这个是用来连接具体的内存池结点的,具体如下:

  1. //该结构用来维护内存池的数据块,供用户分配之用
  2. typedef struct {
  3. u_char *last; //当前内存分配结束位置,即下一段可分配内存的起始位置
  4. u_char *end; //内存池结束位置
  5. ngx_pool_t *next; //链接到下一个内存池
  6. ngx_uint_t failed;//统计该内存池不能满足分配请求的次数
  7. } ngx_pool_data_t;

还有另两个结构ngx_pool_large_t,ngx_pool_cleanup_t,如下示:

  1. //大内存结构
  2. struct ngx_pool_large_s {
  3. ngx_pool_large_t *next; //下一个大块内存
  4. void *alloc;//nginx分配的大块内存空间
  5. };
  6.  
  7. struct ngx_pool_cleanup_s {
  8. ngx_pool_cleanup_pt handler; //数据清理的函数句柄
  9. void *data; //要清理的数据
  10. ngx_pool_cleanup_t *next; //连接至下一个
  11. };

然后我们具体看一下nginx内存池的组成结构,如下图示:

上面的图中,current指针是指向的首结点,在具体的运行过程中是会根据failed值进行调整的。还有就是
ngx_pool_cleanup_s与ngx_pool_large_s的结构空间均来自内存池结点。

然后看nginx相关的操作:
1.创建内存池

内存池的创建是在ngx_create_pool函数中完成的,实现如下:

  1. //创建内存池
  2. ngx_pool_t *
  3. ngx_create_pool(size_t size, ngx_log_t *log)
  4. {
  5. ngx_pool_t *p;
  6. //ngx_memalign实际上会依据os不用,分情况处理,在os不支持memalign情况的分配时,选择直接分配内存
  7. p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); // 分配内存函数,uinx,windows分开走
  8. if (p == NULL) {
  9. return NULL;
  10. }
  11.  
  12. p->d.last = (u_char *) p + sizeof(ngx_pool_t); //初始指向 ngx_pool_t 结构体后面
  13. p->d.end = (u_char *) p + size; //整个结构的结尾后面
  14. p->d.next = NULL;
  15. p->d.failed = ;
  16.  
  17. size = size - sizeof(ngx_pool_t);
  18. //实际上pool数据区的大小与系统页的大小有关的
  19. p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL;
  20. //最大不超过 NGX_MAX_ALLOC_FROM_POOL,也就是getpagesize()-1 大小
  21. p->current = p;
  22. p->chain = NULL;
  23. p->large = NULL;
  24. p->cleanup = NULL;
  25. p->log = log;
  26.  
  27. return p;
  28. }

2.销毁内存池

内存池的销毁位于ngx_destroy_pool(ngx_pool_t *pool)中,此函数会清理所有的内存池结点,同时清理large链表的内存
并且对于注册的cleanup链表的清理操作也会进行。具体实现如下:

  1. void
  2. ngx_destroy_pool(ngx_pool_t *pool)
  3. {
  4. ngx_pool_t *p, *n;
  5. ngx_pool_large_t *l;
  6. ngx_pool_cleanup_t *c;
  7. //会先调用cleanup函数进行清理操作,不过这儿是对自己指向的数据进行清理
  8. for (c = pool->cleanup; c; c = c->next) {
  9. if (c->handler) {
  10. ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, ,
  11. "run cleanup: %p", c);
  12. c->handler(c->data);
  13. }
  14. }
  15. //这儿是作大数据块的清除
  16. for (l = pool->large; l; l = l->next) {
  17.  
  18. ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, , "free: %p", l->alloc);
  19.  
  20. if (l->alloc) {
  21. ngx_free(l->alloc);
  22. }
  23. }
  24.  
  25. #if (NGX_DEBUG)
  26.  
  27. /*
  28. * we could allocate the pool->log from this pool
  29. * so we cannot use this log while free()ing the pool
  30. */
  31.  
  32. for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
  33. ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, ,
  34. "free: %p, unused: %uz", p, p->d.end - p->d.last);
  35.  
  36. if (n == NULL) {
  37. break;
  38. }
  39. }
  40.  
  41. #endif
  42.  
  43. for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
  44. ngx_free(p);
  45.  
  46. if (n == NULL) {
  47. break;
  48. }
  49. }
  50. }

3.分配内存

从内存池中分配内存涉及到几个函数,如下:

  1. void *ngx_palloc(ngx_pool_t *pool, size_t size); //palloc取得的内存是对齐的
  2. void *ngx_pnalloc(ngx_pool_t *pool, size_t size); //pnalloc取得的内存是不对齐的
  3. void *ngx_pcalloc(ngx_pool_t *pool, size_t size); //pcalloc直接调用palloc分配好内存,然后进行一次0初始化操作
  4. void *ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment); //在分配size大小的内存,并按照alignment对齐,然后挂到large字段下
  5. static void *ngx_palloc_block(ngx_pool_t *pool, size_t size); //申请新的内存池结点
  6. static void *ngx_palloc_large(ngx_pool_t *pool, size_t size); //申请大的内存块

下面仅对部分函数进行源码的分析:
首先来看ngx_palloc()函数,其源码为:

  1. //有内存对齐的空间申请
  2. void *
  3. ngx_palloc(ngx_pool_t *pool, size_t size)
  4. {
  5. u_char *m;
  6. ngx_pool_t *p;
  7.  
  8. if (size <= pool->max) {
  9. //从current遍历到链表末尾,不找前面原因其实是因为failed的控制机制,保证前面的节点
  10. //基本处于満的状态。仅剩余部分小块区域。
  11. p = pool->current;
  12.  
  13. do {
  14. m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); // 对齐内存指针,加快存取速度
  15.  
  16. if ((size_t) (p->d.end - m) >= size) {
  17. p->d.last = m + size;
  18.  
  19. return m;
  20. }
  21.  
  22. p = p->d.next;
  23.  
  24. } while (p);
  25. //遍历结束也不能找到合适的可以满足申请要求的结点则新建结点
  26. return ngx_palloc_block(pool, size);
  27. }
  28. //申请大内存时的处理
  29. return ngx_palloc_large(pool, size);
  30. }

其中涉及到两个函数,分别为ngx_palloc_block,ngx_palloc_large先来看ngx_palloc_block,其源码如下:

  1. //申请新的内存池块
  2. static void *
  3. ngx_palloc_block(ngx_pool_t *pool, size_t size)
  4. {
  5. u_char *m;
  6. size_t psize;
  7. ngx_pool_t *p, *new, *current;
  8.  
  9. psize = (size_t) (pool->d.end - (u_char *) pool);
  10.  
  11. m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log);
  12. if (m == NULL) {
  13. return NULL;
  14. }
  15.  
  16. new = (ngx_pool_t *) m;
  17.  
  18. new->d.end = m + psize;
  19. new->d.next = NULL;
  20. new->d.failed = ;
  21. //这儿有个细节,新的节点可以用ngx_pool_t指针表示,但具体的数据存储则是ngx_pool_data_t.
  22. m += sizeof(ngx_pool_data_t);
  23. m = ngx_align_ptr(m, NGX_ALIGNMENT);
  24. new->d.last = m + size;
  25. //这儿是调整current指针,每一次空间申请失败都会导致current至内存池链表结尾的
  26. //结点的failed次数加1,这样在连续分配时,当前current其后的几个结点,其实也差不多
  27. //处于饱和状态,然后这时将current一次调至失败次数较小的结点是合理的,不过判断跳转时机
  28. //是依据经验值的。
  29. current = pool->current;
  30.  
  31. for (p = current; p->d.next; p = p->d.next) {
  32. if (p->d.failed++ > ) {
  33. current = p->d.next;
  34. }
  35. }
  36.  
  37. p->d.next = new;
  38.  
  39. pool->current = current ? current : new;
  40.  
  41. return m;
  42. }

然后是ngx_palloc_large,其源码如下:

  1. //控制大块内存的申请
  2. static void *
  3. ngx_palloc_large(ngx_pool_t *pool, size_t size)
  4. {
  5. void *p;
  6. ngx_uint_t n;
  7. ngx_pool_large_t *large;
  8. //ngx_alloc仅是对alloc的简单封装
  9. p = ngx_alloc(size, pool->log);
  10. if (p == NULL) {
  11. return NULL;
  12. }
  13.  
  14. n = ;
  15.  
  16. for (large = pool->large; large; large = large->next) {
  17. if (large->alloc == NULL) {
  18. large->alloc = p;
  19. return p;
  20. }
  21.  
  22. if (n++ > ) {
  23. break;
  24. }
  25. }
  26.  
  27. large = ngx_palloc(pool, sizeof(ngx_pool_large_t));
  28. if (large == NULL) {
  29. ngx_free(p);
  30. return NULL;
  31. }
  32.  
  33. large->alloc = p;
  34. large->next = pool->large;
  35. pool->large = large;
  36.  
  37. return p;
  38. }

4.其余的函数

内存池中还一些其它支持函数,这里不细说了:

  1. ngx_pool_cleanup_t *ngx_pool_cleanup_add(ngx_pool_t *p, size_t size);
  2. void ngx_pool_run_cleanup_file(ngx_pool_t *p, ngx_fd_t fd);
  3. void ngx_pool_cleanup_file(void *data);
  4. void ngx_pool_delete_file(void *data);

四,下面是一全例子

这个例子主要是演示下nginx内存池的使用,代码如下:

  1. /*
  2. * author:doop-ymc
  3. * date:2013-11-11
  4. * version:1.0
  5. */
  6.  
  7. #include <stdio.h>
  8. #include "ngx_config.h"
  9. #include "ngx_conf_file.h"
  10. #include "nginx.h"
  11. #include "ngx_core.h"
  12. #include "ngx_string.h"
  13. #include "ngx_palloc.h"
  14.  
  15. #define MY_POOL_SIZE 5000
  16.  
  17. volatile ngx_cycle_t *ngx_cycle;
  18.  
  19. void
  20. ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
  21. const char *fmt, ...)
  22. {
  23.  
  24. }
  25.  
  26. void
  27. echo_pool(ngx_pool_t* pool)
  28. {
  29. int n_index;
  30. ngx_pool_t *p_pool;
  31. ngx_pool_large_t *p_pool_large;
  32.  
  33. n_index = ;
  34. p_pool = pool;
  35. p_pool_large = pool->large;
  36.  
  37. printf("------------------------------\n");
  38. printf("pool begin at: 0x%x\n", pool);
  39.  
  40. do{
  41. printf("->d :0x%x\n", p_pool);
  42. printf(" last = 0x%x\n", p_pool->d.last);
  43. printf(" end = 0x%x\n", p_pool->d.end);
  44. printf(" next = 0x%x\n", p_pool->d.next);
  45. printf(" failed = %d\n", p_pool->d.failed);
  46. p_pool = p_pool->d.next;
  47. }while(p_pool);
  48. printf("->max :%d\n", pool->max);
  49. printf("->current :0x%x\n", pool->current);
  50. printf("->chain :0x%x\n", pool->chain);
  51.  
  52. if(NULL == p_pool_large){
  53. printf("->large :0x%x\n", p_pool_large);
  54. }else{
  55. do{
  56. printf("->large :0x%x\n", p_pool_large);
  57. printf(" next = 0x%x\n", p_pool_large->next);
  58. printf(" alloc = 0x%x\n", p_pool_large->alloc);
  59. p_pool_large = p_pool_large->next;
  60. }while(p_pool_large);
  61. }
  62.  
  63. printf("->cleanup :0x%x\n", pool->cleanup);
  64. printf("->log :0x%x\n\n\n", pool->log);
  65.  
  66. }
  67.  
  68. int main()
  69. {
  70. ngx_pool_t *my_pool;
  71.  
  72. /*create pool size:5000*/
  73. my_pool = ngx_create_pool(MY_POOL_SIZE, NULL);
  74. if(NULL == my_pool){
  75. printf("create nginx pool error,size %d\n.", MY_POOL_SIZE);
  76. return ;
  77. }
  78. printf("+++++++++++CREATE NEW POOL++++++++++++\n");
  79. echo_pool(my_pool);
  80.  
  81. printf("+++++++++++ALLOC 2500+++++++++++++++++\n");
  82. ngx_palloc(my_pool, );
  83. echo_pool(my_pool);
  84.  
  85. printf("+++++++++++ALLOC 2500+++++++++++++++++\n");
  86. ngx_palloc(my_pool, );
  87. echo_pool(my_pool);
  88.  
  89. printf("+++++++++++ALLOC LARGE 5000+++++++++++\n");
  90. ngx_palloc(my_pool, );
  91. echo_pool(my_pool);
  92.  
  93. printf("+++++++++++ALLOC LARGE 5000+++++++++++\n");
  94. ngx_palloc(my_pool, );
  95. echo_pool(my_pool);
  96.  
  97. ngx_destroy_pool(my_pool);
  98. return ;
  99.  
  100. }

Makefile文件:

  1. CC = gcc
  2. CFLAGS += -W -Wall -g
  3.  
  4. NGX_ROOT_PATH = /home/doop-ymc/nginx/nginx-1.0.
  5.  
  6. TARGETS = pool_t
  7. TARGETS_C_FILE = $(TARGETS).c
  8.  
  9. all: $(TARGETS)
  10.  
  11. .PHONY:clean
  12.  
  13. clean:
  14. rm -f $(TARGETS) *.o
  15.  
  16. INCLUDE_PATH = -I. \
  17. -I$(NGX_ROOT_PATH)/src/core \
  18. -I$(NGX_ROOT_PATH)/src/event \
  19. -I$(NGX_ROOT_PATH)/src/event/modules \
  20. -I$(NGX_ROOT_PATH)/src/os/unix \
  21. -I$(NGX_ROOT_PATH)/objs \
  22.  
  23. CORE_DEPS = $(NGX_ROOT_PATH)/src/core/nginx.h \
  24. $(NGX_ROOT_PATH)/src/core/ngx_config.h \
  25. $(NGX_ROOT_PATH)/src/core/ngx_core.h \
  26. $(NGX_ROOT_PATH)/src/core/ngx_log.h \
  27. $(NGX_ROOT_PATH)/src/core/ngx_palloc.h \
  28. $(NGX_ROOT_PATH)/src/core/ngx_array.h \
  29. $(NGX_ROOT_PATH)/src/core/ngx_list.h \
  30. $(NGX_ROOT_PATH)/src/core/ngx_hash.h \
  31. $(NGX_ROOT_PATH)/src/core/ngx_buf.h \
  32. $(NGX_ROOT_PATH)/src/core/ngx_queue.h \
  33. $(NGX_ROOT_PATH)/src/core/ngx_string.h \
  34. $(NGX_ROOT_PATH)/src/core/ngx_parse.h \
  35. $(NGX_ROOT_PATH)/src/core/ngx_inet.h \
  36. $(NGX_ROOT_PATH)/src/core/ngx_file.h \
  37. $(NGX_ROOT_PATH)/src/core/ngx_crc.h \
  38. $(NGX_ROOT_PATH)/src/core/ngx_crc32.h \
  39. $(NGX_ROOT_PATH)/src/core/ngx_murmurhash.h \
  40. $(NGX_ROOT_PATH)/src/core/ngx_md5.h \
  41. $(NGX_ROOT_PATH)/src/core/ngx_sha1.h \
  42. $(NGX_ROOT_PATH)/src/core/ngx_rbtree.h \
  43. $(NGX_ROOT_PATH)/src/core/ngx_radix_tree.h \
  44. $(NGX_ROOT_PATH)/src/core/ngx_slab.h \
  45. $(NGX_ROOT_PATH)/src/core/ngx_times.h \
  46. $(NGX_ROOT_PATH)/src/core/ngx_shmtx.h \
  47. $(NGX_ROOT_PATH)/src/core/ngx_connection.h \
  48. $(NGX_ROOT_PATH)/src/core/ngx_cycle.h \
  49. $(NGX_ROOT_PATH)/src/core/ngx_conf_file.h \
  50. $(NGX_ROOT_PATH)/src/core/ngx_resolver.h \
  51. $(NGX_ROOT_PATH)/src/core/ngx_open_file_cache.h \
  52. $(NGX_ROOT_PATH)/src/core/ngx_crypt.h \
  53. $(NGX_ROOT_PATH)/src/event/ngx_event.h \
  54. $(NGX_ROOT_PATH)/src/event/ngx_event_timer.h \
  55. $(NGX_ROOT_PATH)/src/event/ngx_event_posted.h \
  56. $(NGX_ROOT_PATH)/src/event/ngx_event_busy_lock.h \
  57. $(NGX_ROOT_PATH)/src/event/ngx_event_connect.h \
  58. $(NGX_ROOT_PATH)/src/event/ngx_event_pipe.h \
  59. $(NGX_ROOT_PATH)/src/os/unix/ngx_time.h \
  60. $(NGX_ROOT_PATH)/src/os/unix/ngx_errno.h \
  61. $(NGX_ROOT_PATH)/src/os/unix/ngx_alloc.h \
  62. $(NGX_ROOT_PATH)/src/os/unix/ngx_files.h \
  63. $(NGX_ROOT_PATH)/src/os/unix/ngx_channel.h \
  64. $(NGX_ROOT_PATH)/src/os/unix/ngx_shmem.h \
  65. $(NGX_ROOT_PATH)/src/os/unix/ngx_process.h \
  66. $(NGX_ROOT_PATH)/src/os/unix/ngx_setproctitle.h \
  67. $(NGX_ROOT_PATH)/src/os/unix/ngx_atomic.h \
  68. $(NGX_ROOT_PATH)/src/os/unix/ngx_gcc_atomic_x86.h \
  69. $(NGX_ROOT_PATH)/src/os/unix/ngx_thread.h \
  70. $(NGX_ROOT_PATH)/src/os/unix/ngx_socket.h \
  71. $(NGX_ROOT_PATH)/src/os/unix/ngx_os.h \
  72. $(NGX_ROOT_PATH)/src/os/unix/ngx_user.h \
  73. $(NGX_ROOT_PATH)/src/os/unix/ngx_process_cycle.h \
  74. $(NGX_ROOT_PATH)/src/os/unix/ngx_linux_config.h \
  75. $(NGX_ROOT_PATH)/src/os/unix/ngx_linux.h \
  76. $(NGX_ROOT_PATH)/src/core/ngx_regex.h \
  77. $(NGX_ROOT_PATH)/objs/ngx_auto_config.h
  78.  
  79. NGX_PALLOC = $(NGX_ROOT_PATH)/objs/src/core/ngx_palloc.o
  80. NGX_STRING = $(NGX_ROOT_PATH)/objs/src/core/ngx_string.o
  81. NGX_ALLOC = $(NGX_ROOT_PATH)/objs/src/os/unix/ngx_alloc.o
  82.  
  83. $(TARGETS): $(TARGETS_C_FILE) $(NGX_PALLOC) $(NGX_STRING) $(NGX_ALLOC)
  84. $(CC) $(CFLAGS) $(INCLUDE_PATH) $^ -o $@
  85.  
  86. $(NGX_PALLOC):$(CORE_DEPS) \
  87. $(NGX_ROOT_PATH)/src/core/ngx_palloc.c
  88. $(CC) -c -g -o0 $(INCLUDE_PATH) -o $(NGX_PALLOC) $(NGX_ROOT_PATH)/src/core/ngx_palloc.c
  89.  
  90. $(NGX_ALLOC):$(CORE_DEPS) \
  91. $(NGX_ROOT_PATH)/src/os/unix/ngx_alloc.c
  92. $(CC) -c -g -o0 $(INCLUDE_PATH) -o $(NGX_ALLOC) $(NGX_ROOT_PATH)/src/os/unix/ngx_alloc.c
  93.  
  94. $(NGX_STRING):$(CORE_DEPS) \
  95. $(NGX_ROOT_PATH)/src/core/ngx_string.c
  96. $(CC) -c -g -o0 $(INCLUDE_PATH) -o $(NGX_STRING) $(NGX_ROOT_PATH)/src/core/ngx_string.c

运行结果:

  1. [root@localhost pool]# ./pool_t
  2. +++++++++++CREATE NEW POOL++++++++++++
  3. ------------------------------
  4. pool begin at: 0x8f33020
  5. ->d :0x8f33020
  6. last = 0x8f33048
  7. end = 0x8f343a8
  8. next = 0x0
  9. failed =
  10. ->max :
  11. ->current :0x8f33020
  12. ->chain :0x0
  13. ->large :0x0
  14. ->cleanup :0x0
  15. ->log :0x0
  16.  
  17. +++++++++++ALLOC +++++++++++++++++
  18. ------------------------------
  19. pool begin at: 0x8f33020
  20. ->d :0x8f33020
  21. last = 0x8f33a0c
  22. end = 0x8f343a8
  23. next = 0x0
  24. failed =
  25. ->max :
  26. ->current :0x8f33020
  27. ->chain :0x0
  28. ->large :0x0
  29. ->cleanup :0x0
  30. ->log :0x0
  31.  
  32. +++++++++++ALLOC +++++++++++++++++
  33. ------------------------------
  34. pool begin at: 0x8f33020
  35. ->d :0x8f33020
  36. last = 0x8f33a0c
  37. end = 0x8f343a8
  38. next = 0x8f343c0
  39. failed =
  40. ->d :0x8f343c0
  41. last = 0x8f34d94
  42. end = 0x8f35748
  43. next = 0x0
  44. failed =
  45. ->max :
  46. ->current :0x8f33020
  47. ->chain :0x0
  48. ->large :0x0
  49. ->cleanup :0x0
  50. ->log :0x0
  51.  
  52. +++++++++++ALLOC LARGE +++++++++++
  53. ------------------------------
  54. pool begin at: 0x8f33020
  55. ->d :0x8f33020
  56. last = 0x8f33a14
  57. end = 0x8f343a8
  58. next = 0x8f343c0
  59. failed =
  60. ->d :0x8f343c0
  61. last = 0x8f34d94
  62. end = 0x8f35748
  63. next = 0x0
  64. failed =
  65. ->max :
  66. ->current :0x8f33020
  67. ->chain :0x0
  68. ->large :0x8f33a0c
  69. next = 0x0
  70. alloc = 0x8f35750
  71. ->cleanup :0x0
  72. ->log :0x0
  73.  
  74. +++++++++++ALLOC LARGE +++++++++++
  75. ------------------------------
  76. pool begin at: 0x8f33020
  77. ->d :0x8f33020
  78. last = 0x8f33a1c
  79. end = 0x8f343a8
  80. next = 0x8f343c0
  81. failed =
  82. ->d :0x8f343c0
  83. last = 0x8f34d94
  84. end = 0x8f35748
  85. next = 0x0
  86. failed =
  87. ->max :
  88. ->current :0x8f33020
  89. ->chain :0x0
  90. ->large :0x8f33a14
  91. next = 0x8f33a0c
  92. alloc = 0x8f36ae0
  93. ->large :0x8f33a0c
  94. next = 0x0
  95. alloc = 0x8f35750
  96. ->cleanup :0x0
  97. ->log :0x0

总结:从上面的例子初步能看出一些nginx pool使用的轮廓,不过这儿没有涉及到failed的处理。

五,内存池的释放

这部分在nginx中主要是利用其自己web server的特性来完成的;web server总是不停的接受连接及
请求,nginx中有不同等级的内存池,有进程级的,连接级的及请求级的,这样内存总会在对应的进程,
连接,或者请求终止时进行内存池的销毁。

转载请注明出处:http://www.cnblogs.com/doop-ymc/p/3418514.html

nginx 内存池分析的更多相关文章

  1. NGINX 内存池有感

    写在前面 写NGINX系列的随笔,一来总结学到的东西,二来记录下疑惑的地方,在接下来的学习过程中去解决疑惑. 也希望同样对NGINX感兴趣的朋友能够解答我的疑惑,或者共同探讨研究. 整个NGINX系列 ...

  2. nginx——内存池篇

    nginx--内存池篇 一.内存池概述 内存池是在真正使用内存之前,预先申请分配一定数量的.大小相等(一般情况下)的内存块留作备用.当有新的内存需求时,就从内存池中分出一部分内存块,若内存块不够再继续 ...

  3. 初识nginx——内存池篇

    初识nginx——内存池篇 为了自身使用的方便,Nginx封装了很多有用的数据结构,比如ngx_str_t ,ngx_array_t, ngx_pool_t 等等,对于内存池,nginx设计的十分精炼 ...

  4. swoole之memoryGlobal内存池分析

    内存池的作用: 直接使用系统调用malloc会有如下弊端: 频繁分配内存时会产生大量内存碎片 频繁分配内存增加系统调用开销 容易造成内存泄漏 内存池是预先申请一定数量的,大小相等的内存块作为预备使用: ...

  5. nginx 内存池

    参考 https://www.cnblogs.com/xiekeli/archive/2012/10/17/2727432.html?tdsourcetag=s_pctim_aiomsg 源码版本 n ...

  6. nginx内存池

    一.设计原则 (1)降低内存碎片 (2)降低向操作系统申请内存的次数 (3)减少各个模块的开发效率 二.源代码结构 struct ngx_pool_s {     ngx_pool_data_t    ...

  7. nginx源码分析—内存池结构ngx_pool_t及内存管理

    Content 0. 序 1. 内存池结构 1.1 ngx_pool_t结构 1.2 其他相关结构 1.3 ngx_pool_t的逻辑结构 2. 内存池操作 2.1 创建内存池 2.2 销毁内存池 2 ...

  8. nginx源代码分析之内存池实现原理

    建议看本文档时结合nginx源代码. 1.1   什么是内存池?为什么要引入内存池? 内存池实质上是接替OS进行内存管理.应用程序申请内存时不再与OS打交道.而是从内存池中申请内存或者释放内存到内存池 ...

  9. nginx源码学习----内存池

    最近在进行监控平台的设计,之前一直觉得C/C++中最棘手的部分是内存的管理上,远不止new/delete.malloc/free这么简单.随着代码量的递增,程序结构复杂度的提高.各种内存方面的问题悄然 ...

随机推荐

  1. JSP内置对象--pageContent,request,response,session,application,config,out,page,exception

  2. ./encrypt: error while loading shared libraries: libcrypto.so.10:

    ./encrypt: error while loading shared libraries: libcrypto.so.10:

  3. 在MyEclipse中运行tomcat报错 严重: Error starting static Resources

    严重: Error starting static Resourcesjava.lang.IllegalArgumentException: Document base E:\apache-tomca ...

  4. String s = new String("aa") 创建了几个对象?

    1 最近几个同学面试的时候出现了这样一个问题 刚听到这个题目的时候的确是不知所措: 经过网上的查找和自己的理解来解释一下这个题目的答案 答案是: 为什么呢??? 1 实现我们都知道创建实例有两种方法 ...

  5. Dominating Patterns

    Dominating Patterns Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu Descr ...

  6. VS2013使用技巧汇总

    1. Peek View 在不新建TAB的情况下快速查看.编辑一个函数的代码. 以前要看一个函数的实现,需要在使用的地方点击F12跳转到该函数,实际上这是很浪费时间的.VS2013Peek View便 ...

  7. Struts2值栈详解

    1. 关于值栈: 1). helloWorld 时, ${productName} 读取 productName 值, 实际上该属性并不在 request 等域对象中, 而是从值栈中获取的.  2). ...

  8. webservice(soap)接口的加密,SHA-1实现

    import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security. ...

  9. Java中常见的5种WEB服务器介绍

    这篇文章主要介绍了Java中常见的5种WEB服务器介绍,它们分别是Tomcat.Resin.JBoss.WebSphere.WebLogic,需要的朋友可以参考下 Web服务器是运行及发布Web应用的 ...

  10. sockaddr_u详解

    struct sockaddr { unsigned short sa_family;     /* address family, AF_xxx */ char sa_data[14];       ...