why its not consuming memory

Discussion in 'C Programming' started by Ravi, May 23, 2007.

  1. Ravi

    Ravi Guest

    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
    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 157276 51676 201972 0 0 97 23 379 407 10
    9 80 2
    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 157260 51684 201972 0 0 96 23 379 407 10
    9 79 2
    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 157276 51692 201972 0 0 96 23 379 407 10
    9 79 2
    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 51700 201972 0 0 96 23 379 406 10
    9 79 2
    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 157276 51708 201972 0 0 96 23 379 406 10
    9 79 2
    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 51716 201972 0 0 96 23 379 405 10
    9 79 2

    showinf free memory almost constant.
    Ravi, May 23, 2007
    #1
    1. Advertising

  2. Ravi <> wrote:

    > #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:


    Bet $100 that malloc() is returning NULL.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, May 23, 2007
    #2
    1. Advertising

  3. "Ravi" <> wrote in message
    news:...
    > 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:

    ....
    > showinf free memory almost constant.


    Linux happens to use a "lazy" allocation mechanism; memory isn't given to
    the program until it's actually used. Since your program never uses the
    memory, it never affects the "free memory" stats. "free" memory on Linux
    means unused memory, not unallocated memory.

    (In very rare circumstances, this has an evil side effect that malloc() can
    succeed but then using the memory later causes a segfault because the system
    doesn't actually have any remaining memory to give at that point. Lesson:
    don't malloc() memory until you're ready to use it.)

    S

    --
    Stephen Sprunk "Those people who think they know everything
    CCIE #3723 are a great annoyance to those of us who do."
    K5SSS --Isaac Asimov


    --
    Posted via a free Usenet account from http://www.teranews.com
    Stephen Sprunk, May 23, 2007
    #3
  4. Ravi

    Army1987 Guest

    "Ravi" <> ha scritto nel messaggio
    news:...
    >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);
    > }


    The last time I did something like that (but with a reasonable size
    i.e. 200) I crashed a server without even being root. And nobody
    knew where the server was physically located, so we had to ask a
    professor to go reboot it. Luckily, other users weren't doing
    anything more serious than surfing the web or writing simple
    homework assignment programs.

    The standard says:
    7.20.3.3 The malloc function

    Synopsis

    1 #include <stdlib.h>

    void *malloc(size_t size);

    Description

    2 The malloc function allocates space for an object whose size is specified
    by size and

    whose value is indeterminate.

    Returns

    3 The malloc function returns either a null pointer or a pointer to the
    allocated space.

    It doesn't even use the verbs "succeed" or "fail".

    Getting back to your question, see http://c-faq.com, question 7.14.
    Army1987, May 24, 2007
    #4
  5. Ravi

    JimS Guest

    On 23 May 2007 11:26:59 -0700, 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);
    >}


    >showinf free memory almost constant.


    Is is possible for a compiler to know that malloc has no side effects
    and to optimize this program to

    int main(void) {for (;;);}

    under the as-if rule?

    Jim
    JimS, May 25, 2007
    #5
  6. In article <>,
    JimS <> wrote:
    >On 23 May 2007 11:26:59 -0700, 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);
    >>}


    >Is is possible for a compiler to know that malloc has no side effects
    >and to optimize this program to


    >int main(void) {for (;;);}


    >under the as-if rule?


    Urrr, no -- because malloc *does* have side effects!
    --
    "It is important to remember that when it comes to law, computers
    never make copies, only human beings make copies. Computers are given
    commands, not permission. Only people can be given permission."
    -- Brad Templeton
    Walter Roberson, May 25, 2007
    #6
  7. -cnrc.gc.ca (Walter Roberson) writes:
    > In article <>,
    > JimS <> wrote:
    >>On 23 May 2007 11:26:59 -0700, 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);
    >>>}

    >
    >>Is is possible for a compiler to know that malloc has no side effects
    >>and to optimize this program to

    >
    >>int main(void) {for (;;);}

    >
    >>under the as-if rule?

    >
    > Urrr, no -- because malloc *does* have side effects!


    It doesn't have any well-defined visible side effects. Since the
    program never examines the value of "a" (please pick better variable
    names, even for exapmles), I *think* the compiler can legally optimize
    away the call to malloc().

    The program would be more meaningful if it checked whether malloc()
    returns a null pointer or not.

    In practice, on some systems, the behavior could be considerably
    different if the program attempts to access the allocated memory, due
    to "lazy allocation". It can be argued (and I do argue) that lazy
    allocation is non-conforming, but it is a common implementation
    choice.

    (In lazy allocation, malloc() allocates a chunk of address space, but
    the actual memory isn't actually allocated until the program attempts
    to reference it. Unfortunately, if this delayed allocation fails, it
    doesn't cleanly return an error indication as malloc() itself should
    have done; it causes the process, or perhaps even another process, to
    be killed.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 25, 2007
    #7
  8. Ravi

    Eric Sosman Guest

    JimS wrote On 05/24/07 20:02,:
    > On 23 May 2007 11:26:59 -0700, 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);
    >>}

    >
    >
    >>showinf free memory almost constant.

    >
    >
    > Is is possible for a compiler to know that malloc has no side effects
    > and to optimize this program to
    >
    > int main(void) {for (;;);}
    >
    > under the as-if rule?


    malloc() has no side-effects? I didn't know that ...

    --
    Eric Sosman, May 25, 2007
    #8
    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:
    1,762
    Smokey Grindel
    Dec 2, 2006
  2. Re: why its not consuming memory

    , May 24, 2007, in forum: C Programming
    Replies:
    1
    Views:
    326
    Joachim Schmitz
    May 24, 2007
  3. thunk
    Replies:
    1
    Views:
    292
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    456
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    600
    thunk
    Apr 3, 2010
Loading...

Share This Page