0

0

Nginx内存管理

php中文网

php中文网

发布时间:2016-07-30 13:31:32

|

1417人浏览过

|

来源于php中文网

原创

1.源代码位置

头文件:http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_palloc.h

源文件:http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_palloc.c

2.数据结构定义

先来学习一下nginx内存池的几个主要数据结构:

    ngx_pool_data_t(内存池数据块结构)

<span>   1:</span><span>typedef</span> <span>struct</span> {<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   2:</span>     u_char               *last;        <pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   3:</span>     u_char               *end;<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   4:</span>     ngx_pool_t           *next;<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   5:</span>     ngx_uint_t            failed;<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   6:</span> } ngx_pool_data_t;
  • last:是一个unsigned char 类型的指针,保存的是/当前内存池分配到末位地址,即下一次分配从此处开始。
  • end:内存池结束位置;
  • next:内存池里面有很多块内存,这些内存块就是通过该指针连成链表的,next指向下一块内存。
  • failed:内存池分配失败次数。

ngx_pool_s(内存池头部结构)

<span>   1:</span><span>struct</span> ngx_pool_s {<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   2:</span>     ngx_pool_data_t       d;<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   3:</span>     size_t                max;<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   4:</span>     ngx_pool_t           *current;<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   5:</span>     ngx_chain_t          *chain;<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   6:</span>     ngx_pool_large_t     *large;<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   7:</span>     ngx_pool_cleanup_t   *cleanup;<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   8:</span>     ngx_log_t            *log;<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   9:</span> };
  • d:内存池的数据块;
  • max:内存池数据块的最大值;
  • current:指向当前内存池;
  • chain:该指针挂接一个ngx_chain_t结构;
  • large:大块内存链表,即分配空间超过max的情况使用;
  • cleanup:释放内存池的callback
  • log:日志信息

由ngx_pool_data_t和ngx_pool_t组成的nginx内存池结构如下图所示:

Nginx内存管理

3.相关函数介绍

在分析内存池方法前,需要对几个主要的内存相关函数作一下介绍:

ngx_alloc:(只是对malloc进行了简单的封装)

<span>   1:</span><span>void</span> *<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   2:</span> ngx_alloc(size_t size, ngx_log_t *log)<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   3:</span> {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   4:</span>     <span>void</span>  *p;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   5:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   6:</span>     p = malloc(size);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   7:</span>     <span>if</span> (p == NULL) {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   8:</span>         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   9:</span>                       <span>"malloc(%uz) failed"</span>, size);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  10:</span>     }<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  11:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  12:</span>     ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0, <span>"malloc: %p:%uz"</span>, p, size);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  13:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  14:</span>     <span>return</span> p;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  15:</span> }

ngx_calloc:(调用malloc并初始化为0)

<span>   1:</span><span>void</span> *<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   2:</span> ngx_calloc(size_t size, ngx_log_t *log)<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   3:</span> {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   4:</span>     <span>void</span>  *p;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   5:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   6:</span>     p = ngx_alloc(size, log);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   7:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   8:</span>     <span>if</span> (p) {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   9:</span>         ngx_memzero(p, size);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  10:</span>     }<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  11:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  12:</span>     <span>return</span> p;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  13:</span> }

ngx_memzero:

<span>   1:</span> #define ngx_memzero(buf, n)       (<span>void</span>) memset(buf, 0, n)

ngx_free :

<span>   1:</span> #define ngx_free          free

ngx_memalign:

<span>   1:</span><span>void</span> *<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   2:</span> ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   3:</span> {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   4:</span>     <span>void</span>  *p;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   5:</span>     <span>int</span>    err;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   6:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   7:</span>     err = posix_memalign(&p, alignment, size);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   8:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   9:</span>     <span>if</span> (err) {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  10:</span>         ngx_log_error(NGX_LOG_EMERG, log, err,<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  11:</span>                       <span>"posix_memalign(%uz, %uz) failed"</span>, alignment, size);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  12:</span>         p = NULL;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  13:</span>     }<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  14:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  15:</span>     ngx_log_debug3(NGX_LOG_DEBUG_ALLOC, log, 0,<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  16:</span>                    <span>"posix_memalign: %p:%uz @%uz"</span>, p, size, alignment);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  17:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  18:</span>     <span>return</span> p;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  19:</span> }

这里alignment主要是针对部分unix平台需要动态的对齐,对POSIX 1003.1d提供的posix_memalign( )进行封装,在大多数情况下,编译器和C库透明地帮你处理对齐问题。nginx中通过宏NGX_HAVE_POSIX_MEMALIGN来控制;调用posix_memalign( )成功时会返回size字节的动态内存,并且这块内存的地址是alignment的倍数。参数alignment必须是2的幂,还是void指针的大小的倍数。返回的内存块的地址放在了memptr里面,函数返回值是0.
 

4.内存池基本操作

  • 内存池对外的主要方法有:
创建内存池 ngx_pool_t *  ngx_create_pool(size_t size, ngx_log_t *log);
销毁内存池 void ngx_destroy_pool(ngx_pool_t *pool);
重置内存池 void ngx_reset_pool(ngx_pool_t *pool);
内存申请(对齐) void *  ngx_palloc(ngx_pool_t *pool, size_t size);
内存申请(不对齐) void *  ngx_pnalloc(ngx_pool_t *pool, size_t size);
内存清除 ngx_int_t  ngx_pfree(ngx_pool_t *pool, void *p);

4.1 创建内存池ngx_create_pool

ngx_create_pool用于创建一个内存池,我们创建时,传入我们的需要的初始大小:

如此AI写作
如此AI写作

AI驱动的内容营销平台,提供一站式的AI智能写作、管理和分发数字化工具。

下载

<span>   1:</span> ngx_pool_t *<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   2:</span> ngx_create_pool(size_t size, ngx_log_t *log)<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   3:</span> {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   4:</span>     ngx_pool_t  *p;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   5:</span>     <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   6:</span>     <span>//以16(NGX_POOL_ALIGNMENT)字节对齐分配size内存</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   7:</span>     p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   8:</span>     <span>if</span> (p == NULL) {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   9:</span>         <span>return</span> NULL;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  10:</span>     }<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  11:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  12:</span>     <span>//初始状态:last指向ngx_pool_t结构体之后数据取起始位置</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  13:</span>     p->d.last = (u_char *) p + <span>sizeof</span>(ngx_pool_t);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  14:</span>     <span>//end指向分配的整个size大小的内存的末尾</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  15:</span>     p->d.end = (u_char *) p + size;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  16:</span>     <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  17:</span>     p->d.next = NULL;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  18:</span>     p->d.failed = 0;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  19:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  20:</span>     size = size - <span>sizeof</span>(ngx_pool_t);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  21:</span>     <span>//#define NGX_MAX_ALLOC_FROM_POOL  (ngx_pagesize - 1),内存池最大不超过4095,x86中页的大小为4K</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  22:</span>     p->max = (size <span>  23:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  24:</span>     p->current = p;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  25:</span>     p->chain = NULL;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  26:</span>     p->large = NULL;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  27:</span>     p->cleanup = NULL;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  28:</span>     p->log = log;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  29:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  30:</span>     <span>return</span> p;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  31:</span> }

nginx对内存的管理分为大内存与小内存,当某一个申请的内存大于某一个值时,就需要从大内存中分配空间,否则从小内存中分配空间。

nginx中的内存池是在创建的时候就设定好了大小,在以后分配小块内存的时候,如果内存不够,则是重新创建一块内存串到内存池中,而不是将原有的内存池进行扩张。当要分配大块内存是,则是在内存池外面再分配空间进行管理的,称为大块内存池。

 

4.2 内存申请 ngx_palloc

<span>   1:</span><span>void</span> *<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   2:</span> ngx_palloc(ngx_pool_t *pool, size_t size)<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   3:</span> {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   4:</span>     u_char      *m;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   5:</span>     ngx_pool_t  *p;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   6:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   7:</span>     <span>//如果申请的内存大小小于内存池的max值</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   8:</span>     <span>if</span> (size max) {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   9:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  10:</span>         p = pool->current;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  11:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  12:</span>         <span>do</span> {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  13:</span>             <span>//对内存地址进行对齐处理</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  14:</span>             m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  15:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  16:</span>             <span>//如果当前内存块够分配内存,则直接分配</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  17:</span>             <span>if</span> ((size_t) (p->d.end - m) >= size) <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  18:</span>             {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  19:</span>                 p->d.last = m + size;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  20:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  21:</span>                 <span>return</span> m;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  22:</span>             }<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  23:</span>             <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  24:</span>             <span>//如果当前内存块有效容量不够分配,则移动到下一个内存块进行分配</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  25:</span>             p = p->d.next;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  26:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  27:</span>         } <span>while</span> (p);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  28:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  29:</span>         <span>//当前所有内存块都没有空闲了,开辟一块新的内存,如下2详细解释</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  30:</span>         <span>return</span> ngx_palloc_block(pool, size);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  31:</span>     }<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  32:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  33:</span>     <span>//分配大块内存</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  34:</span>     <span>return</span> ngx_palloc_large(pool, size);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  35:</span> }

需要说明的几点:

1、ngx_align_ptr,这是一个用来内存地址取整的宏,非常精巧,一句话就搞定了。作用不言而喻,取整可以降低CPU读取内存的次数,提高性能。因为这里并没有真正意义调用malloc等函数申请内存,而是移动指针标记而已,所以内存对齐的活,C编译器帮不了你了,得自己动手。

<span>   1:</span> #define ngx_align_ptr(p, a)                                                   \<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   2:</span>      (u_char *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1))

2、开辟一个新的内存块 ngx_palloc_block(ngx_pool_t *pool, size_t size)

这个函数是用来分配新的内存块,为pool内存池开辟一个新的内存块,并申请使用size大小的内存;

<span>   1:</span><span>static</span> <span>void</span> *<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   2:</span> ngx_palloc_block(ngx_pool_t *pool, size_t size)<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   3:</span> {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   4:</span>     u_char      *m;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   5:</span>     size_t       psize;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   6:</span>     ngx_pool_t  *p, *<span>new</span>;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   7:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   8:</span>     <span>//计算内存池第一个内存块的大小</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   9:</span>     psize = (size_t) (pool->d.end - (u_char *) pool);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  10:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  11:</span>     <span>//分配和第一个内存块同样大小的内存块</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  12:</span>     m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  13:</span>     <span>if</span> (m == NULL) {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  14:</span>         <span>return</span> NULL;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  15:</span>     }<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  16:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  17:</span>     <span>new</span> = (ngx_pool_t *) m;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  18:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  19:</span>     <span>//设置新内存块的end</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  20:</span>     <span>new</span>->d.end = m + psize;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  21:</span>     <span>new</span>->d.next = NULL;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  22:</span>     <span>new</span>->d.failed = 0;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  23:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  24:</span>     <span>//将指针m移动到d后面的一个位置,作为起始位置</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  25:</span>     m += <span>sizeof</span>(ngx_pool_data_t);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  26:</span>     <span>//对m指针按4字节对齐处理</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  27:</span>     m = ngx_align_ptr(m, NGX_ALIGNMENT);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  28:</span>     <span>//设置新内存块的last,即申请使用size大小的内存</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  29:</span>     <span>new</span>->d.last = m + size;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  30:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  31:</span>     <span>//这里的循环用来找最后一个链表节点,这里failed用来控制循环的长度,如果分配失败次数达到5次,就忽略,不需要每次都从头找起</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  32:</span>     <span>for</span> (p = pool->current; p->d.next; p = p->d.next) {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  33:</span>         <span>if</span> (p->d.failed++ > 4) {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  34:</span>             pool->current = p->d.next;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  35:</span>         }<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  36:</span>     }<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  37:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  38:</span>     p->d.next = <span>new</span>;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  39:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  40:</span>     <span>return</span> m;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  41:</span> }

3、分配大块内存 ngx_palloc_large(ngx_pool_t *pool, size_t size)

在ngx_palloc中首先会判断申请的内存大小是否超过内存块的最大限值,如果超过,则直接调用ngx_palloc_large,进入大内存块的分配流程;

<span>   1:</span><span>static</span> <span>void</span> *<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   2:</span> ngx_palloc_large(ngx_pool_t *pool, size_t size)<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   3:</span> {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   4:</span>     <span>void</span>              *p;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   5:</span>     ngx_uint_t         n;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   6:</span>     ngx_pool_large_t  *large;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   7:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   8:</span>     <span>// 直接在系统堆中分配一块大小为size的空间</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   9:</span>     p = ngx_alloc(size, pool->log);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  10:</span>     <span>if</span> (p == NULL) {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  11:</span>         <span>return</span> NULL;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  12:</span>     }<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  13:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  14:</span>     n = 0;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  15:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  16:</span>     <span>// 查找到一个空的large区,如果有,则将刚才分配的空间交由它管理  </span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  17:</span>     <span>for</span> (large = pool->large; large; large = large->next) {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  18:</span>         <span>if</span> (large->alloc == NULL) {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  19:</span>             large->alloc = p;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  20:</span>             <span>return</span> p;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  21:</span>         }<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  22:</span>         <span>//为了提高效率, 如果在三次内没有找到空的large结构体,则创建一个</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  23:</span>         <span>if</span> (n++ > 3) {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  24:</span>             <span>break</span>;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  25:</span>         }<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  26:</span>     }<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  27:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  28:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  29:</span>     large = ngx_palloc(pool, <span>sizeof</span>(ngx_pool_large_t));<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  30:</span>     <span>if</span> (large == NULL) {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  31:</span>         ngx_free(p);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  32:</span>         <span>return</span> NULL;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  33:</span>     }<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  34:</span>     <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  35:</span>     <span>//将large链接到内存池</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  36:</span>     large->alloc = p;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  37:</span>     large->next = pool->large;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  38:</span>     pool->large = large;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  39:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  40:</span>     <span>return</span> p;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  41:</span> }

整个内存池分配如下图:

Nginx内存管理

  • 4.3 内存池重置 ngx_reset_pool

<span>   1:</span><span>void</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   2:</span> ngx_reset_pool(ngx_pool_t *pool)<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   3:</span> {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   4:</span>     ngx_pool_t        *p;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   5:</span>     ngx_pool_large_t  *l;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   6:</span>     <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   7:</span>     <span>//释放大块内存</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   8:</span>     <span>for</span> (l = pool->large; l; l = l->next) {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   9:</span>         <span>if</span> (l->alloc) {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  10:</span>             ngx_free(l->alloc);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  11:</span>         }<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  12:</span>     }<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  13:</span>     <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  14:</span>     <span>// 重置所有小块内存区</span><pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  15:</span>     <span>for</span> (p = pool; p; p = p->d.next) {<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  16:</span>         p->d.last = (u_char *) p + <span>sizeof</span>(ngx_pool_t);<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  17:</span>         p->d.failed = 0;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  18:</span>     }<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  19:</span>  <pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  20:</span>     pool->current = pool;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  21:</span>     pool->chain = NULL;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  22:</span>     pool->large = NULL;<pre class="brush:php;toolbar:false;" courier new width:870px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  23:</span> }

4.4 内存池释放 ngx_pfree

<span>   1:</span> ngx_int_t<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   2:</span> ngx_pfree(ngx_pool_t *pool, <span>void</span> *p)<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   3:</span> {<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   4:</span>     ngx_pool_large_t  *l;<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   5:</span>  <pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   6:</span>     <span>//只检查是否是大内存块,如果是大内存块则释放</span><pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   7:</span>     <span>for</span> (l = pool->large; l; l = l->next) {<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   8:</span>         <span>if</span> (p == l->alloc) {<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>   9:</span>             ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  10:</span>                            <span>"free: %p"</span>, l->alloc);<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  11:</span>             ngx_free(l->alloc);<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  12:</span>             l->alloc = NULL;<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  13:</span>  <pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  14:</span>             <span>return</span> NGX_OK;<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  15:</span>         }<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  16:</span>     }<pre class="brush:php;toolbar:false;" courier new width:950px padding:0px direction:ltr margin:0em line-height:12pt background-color:rgb><span>  17:</span>  <pre class="brush:php;toolbar:false;" courier new width:950px padding:>

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法

本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。

705

2026.02.13

微博网页版主页入口与登录指南_官方网页端快速访问方法
微博网页版主页入口与登录指南_官方网页端快速访问方法

本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。

233

2026.02.13

Flutter跨平台开发与状态管理实战
Flutter跨平台开发与状态管理实战

本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。

117

2026.02.13

TypeScript工程化开发与Vite构建优化实践
TypeScript工程化开发与Vite构建优化实践

本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。

22

2026.02.13

Redis高可用架构与分布式缓存实战
Redis高可用架构与分布式缓存实战

本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。

61

2026.02.13

c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

30

2026.02.12

雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法
雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法

本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。

15

2026.02.12

豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法
豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法

本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。

669

2026.02.12

PostgreSQL性能优化与索引调优实战
PostgreSQL性能优化与索引调优实战

本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。

58

2026.02.12

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号