INOUE Seiichiro <inoue <at> ariel-networks.com> writes:

> 
> Hi, 
> 
> > Checking my module with a stress test (1440 reqs/min) I'm observing , 
> > with top, that SIZE and RSS of httpd processes are growing.
> 
> I recommend you to check apr_allocator_max_free_set().
> 
> The following sample code shows the effect.
> Without calling apr_allocator_max_free_set(), you can find the memory usage 
is growing rapidly.
> 
> ///////////////// sample code starts
> /* memory pool leak test.
>  *  <at> remark No error checks */
> #include <stdio.h>
> #include <apr_general.h>
> 
> int main(int argc, char **argv)
> {
>     apr_pool_t *mp;
>     int i;
>       
>     apr_initialize();
>     apr_pool_create(&mp, NULL);
> 
>     /* XXX Without this setting, memory usage is growing more than expected 
*/
> #define MY_POOL_MAX_FREE_SIZE 32
>     {
>         apr_allocator_t *pa = apr_pool_allocator_get(mp);
>         if (pa) {
>             apr_allocator_max_free_set(pa, MY_POOL_MAX_FREE_SIZE);
>         }
>     }
> 
> #define BASE_ALLOC_SIZE    (8*1024)
>     i = 0;
>     while (1) {
>         apr_palloc(mp, BASE_ALLOC_SIZE + i);
>         i++;
>         if (i % 10000 == 0) {
>             puts("press enter key (please check memory usage)");
>             getchar();
>         }
>         apr_pool_clear(mp);
>     }
>     apr_terminate();
>     return 0;
> }
> ///////////////// sample code ends
> 
> - INOUE Seiichiro <inoue <at> ariel-networks.com>
> 
> 
there would be memory leak, it seems that there is no-effect by calling 
apr_allocator_max_free_set.

#define SIZE (10*1024*1024)

apr_pool_t *p;
apr_initialize();
atexit(apr_terminate);

apr_allocator_t *my_allocator_ptr = NULL;
        apr_pool_t *system_pool = NULL;
        if( apr_allocator_create(&my_allocator_ptr) == APR_SUCCESS ){
                printf("created my_allocator_ptr success\n");
                apr_allocator_max_free_set(my_allocator_ptr, SIZE);
                printf("set max free size for my_allocator_ptr to %d\n", SIZE);
        }
        apr_pool_create(&system_pool, NULL);
        apr_pool_create_ex(&p, system_pool, (apr_abortfunc_t)
failed_to_allocate, my_allocator_ptr);
        apr_allocator_owner_set(my_allocator_ptr, p);

        apr_allocator_t *pa = apr_pool_allocator_get(p);
        printf("pa %x my_allocator_ptr %x\n\n", pa, my_allocator_ptr);

        int chunk_size;
        void *ptr = NULL;
        chunk_size = 1024*1024;
        ptr = apr_pcalloc(p, chunk_size);
        for(int i = 1; ptr != NULL; i++){
                ptr = apr_pcalloc(p, chunk_size);
                if( ptr != NULL ){
                        printf("pass %3.3d: allocated %d bytes\n", i, 
chunk_size);
                }
                else{
                        printf("pass %3.3d: ***failed to allocate %d bytes\n", 
i, chunk_size);
                }
        }



Reply via email to