Re: why its not consuming memory

Discussion in 'C Programming' started by p_cricket_guy@yahoo.co.in, May 24, 2007.

  1. Guest

    On May 23, 11:26 pm, Ravi <> wrote:
    > I created a program just to amuse me. I consumes a lot of memory and
    > never gives it up:
    >
    > #incluse <stdio.h>
    > #include <stdlib.h>
    > int main(void) {
    > void *a;
    > while(1)
    > a = malloc(99999);
    >
    > }
    >
    > but the output of vmstat (on GNU/Linux) is a very constant as:
    >
    > procs -----------memory---------- ---swap-- -----io---- -system-- ----
    > cpu----
    > r b swpd free buff cache si so bi bo in cs us
    > sy id wa
    > 1 0 0 157292 51668 201972 0 0 97 23 379 408 10
    > 8 80 2


    < sinpped >

    This is more of a Linux question. Folks in a Linux newsgroup would
    provide a better answer.

    <OT>
    Try modifying your program to malloc memory of size equal to Virtual
    Memory page size on your architecture. Also, write to at least one
    character into the allocated memory in each iteration. You might
    see what you expect.

    /* Disclaimer: Use with caution - */

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

    #define PAGE_SIZE 4000 /* Depends on your arch */
    int main(void) {
    char *a;
    size_t total = 0;
    while(1) {
    a = malloc(PAGE_SIZE);
    if (a == NULL) {
    printf("No more memory after %d bytes \n", total);
    return EXIT_SUCCESS; /* Hope your OS frees all
    the memory allocated ! */
    }
    total += PAGE_SIZE;
    a[PAGE_SIZE/2] = 1;
    }
    return EXIT_SUCCESS;
    }

    OS's with virtual memory may actually allocate physical memory
    when the allocated memory is accessed.

    </OT>
     
    , May 24, 2007
    #1
    1. Advertising

  2. <> schrieb im Newsbeitrag
    news:...
    > On May 23, 11:26 pm, Ravi <> wrote:
    >> I created a program just to amuse me. I consumes a lot of memory and
    >> never gives it up:

    > < sinpped >
    >
    > This is more of a Linux question. Folks in a Linux newsgroup would
    > provide a better answer.
    >
    > <OT>
    > Try modifying your program to malloc memory of size equal to Virtual
    > Memory page size on your architecture. Also, write to at least one
    > character into the allocated memory in each iteration. You might
    > see what you expect.
    >
    > /* Disclaimer: Use with caution - */
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > #define PAGE_SIZE 4000 /* Depends on your arch */

    Indeed arch dependant, but almost certailiny not 4000, more likely a power
    of 2 like 4096

    Bye, Jojo
     
    Joachim Schmitz, May 24, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,072
    Smokey Grindel
    Dec 2, 2006
  2. Ravi

    why its not consuming memory

    Ravi, May 23, 2007, in forum: C Programming
    Replies:
    7
    Views:
    335
    Eric Sosman
    May 25, 2007
  3. thunk
    Replies:
    1
    Views:
    330
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    508
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    643
    thunk
    Apr 3, 2010
Loading...

Share This Page