Core: trim malloc memory after configuration unload.

When using dynamic configurations, multiple malloc arens could be created
because of using threads.  Also, a worker process may persist through
multiple dynamic cycles which increases memory fragmentation.  Both these
factors contribute to malloc not being able to effectively free its memory
to the kernel, which increases overall system memory consumption despite
only a small amount of memory being allocated from malloc().

To enfoce memory release, malloc_trim() is now called after cycle unload.
This commit is contained in:
Roman Arutyunyan 2025-06-13 17:25:20 +04:00
parent 9d9b3785c1
commit dd22d6ed11
2 changed files with 14 additions and 0 deletions

View file

@ -833,6 +833,16 @@ ngx_feature_test="void *p; p = memalign(4096, 4096);
. auto/feature
ngx_feature="malloc_trim()"
ngx_feature_name="NGX_HAVE_MALLOC_TRIM"
ngx_feature_run=no
ngx_feature_incs="#include <malloc.h>"
ngx_feature_path=
ngx_feature_libs=
ngx_feature_test="malloc_trim(0)"
. auto/feature
ngx_feature="mmap(MAP_ANON|MAP_SHARED)"
ngx_feature_name="NGX_HAVE_MAP_ANON"
ngx_feature_run=yes

View file

@ -864,6 +864,10 @@ ngx_dynamic_conf_unload(ngx_dynamic_conf_ctx_t *ctx)
ctx->cycle, ctx->id);
ngx_destroy_pool(ctx->pool);
#if (NGX_HAVE_MALLOC_TRIM)
malloc_trim(0);
#endif
}