Find the answer to your Linux question:
Results 1 to 2 of 2
Hello All, I am having a tricky issue here. I wanted to limit the resident memory usage of processes and to test it out I wrote a program that allocates ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2012
    Posts
    1

    Resident memory is growing huge as I do memset in a for loop


    Hello All,

    I am having a tricky issue here.

    I wanted to limit the resident memory usage of processes and to test it out I wrote a program that allocates memory and write some data using memset.

    It was found that If I do not use memset, the program runs OK, resident memory usage of the program is within lower limits, and virtual memory grows up as expected. But If I use memset, the resident memory is growing till a point where the whole of memory is used up and eventually resulting in a crash.

    Compile and run the following program and observe top for the memory usage of the program

    The simple program is as follows:


    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>

    int main()
    {
    char *memTest;
    int i;
    for(i =0; i<100; i++)
    {
    memTest = calloc(5000000000,sizeof(memTest));
    // If I comment the following line resident memory is not growing
    memset(memTest,9,5000000000);
    printf("\nItteration: %d",i);
    sleep(1);
    }
    return 0;
    }


    The question that is really bothering me here is that why kernel is not freeing up resident memory when it is not in use? Some page out operation that was due is not happening.

    The OS used is SUSE 11.2, but guess this is a Linux kernel issue and not OS specific.

    Any help is appreciated.

    \\ Regards,

    ArunSL

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,601
    You keep allocating data, but you never free it. Try this instead:
    Code:
    int main()
    {
        char *memTest;
        int i;
        for(i =0; i<100; i++) 
        {
            memTest = calloc(5000000000,sizeof(memTest));
           // If I comment the following line resident memory is not growing
           memset(memTest,9,5000000000);
           printf("\nItteration: %d",i);
           free(memTest);
           sleep(1);
        }
        return 0;
    }
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •