try to maka a program run out of memory

Discussion in 'C++' started by Tony Johansson, Apr 4, 2005.

  1. Hello experts!!

    I have written a small program that I hope will run out of memory(just for
    testing) because i have deliberately removed the delete of the allocated
    memory but it will not run out of memory.
    So my question is can you see why not this program will run out of memory.
    It consist of three files these are start.cpp,test.h and slask.h

    // Here is the file containing the main funtion called start.cpp
    #include "test.h"
    main()
    {
    Test testobj;
    for (long i =0; i<999999999; i++)
    testobj.foo();
    }

    //Here is file test.h containing the class definition of class Test
    #include "slask.h"
    class Test
    {
    public:
    void foo()
    { s = new Slask(); }
    private:
    Slask* s;
    };

    //Here is the last file named slask.h containg the class definition of class
    Slask.
    class Slask
    {
    public:
    private:
    char namn1[1000];
    char namn2[1000];
    };

    Many thanks
    //Tony
     
    Tony Johansson, Apr 4, 2005
    #1
    1. Advertising

  2. Tony Johansson

    marbac Guest

    Tony Johansson wrote:

    > I have written a small program that I hope will run out of memory(just for
    > testing) because i have deliberately removed the delete of the allocated
    > memory but it will not run out of memory.
    > So my question is can you see why not this program will run out of memory.
    > It consist of three files these are start.cpp,test.h and slask.h
    >


    Hi,

    i can only talk about the linux behaviour of this program:

    it runs out of memory if you have no swap-memory. I ran the program and
    my harddisk got very busy, that means that the system uses the swap-area
    on the harddisk to allocate new items. I didn`t look what happens if the
    system is out of swap ... could take hours

    regards marbac
     
    marbac, Apr 4, 2005
    #2
    1. Advertising

  3. Tony Johansson schrieb:
    > Hello experts!!
    >
    > I have written a small program that I hope will run out of memory(just for
    > testing) because i have deliberately removed the delete of the allocated
    > memory but it will not run out of memory.
    > So my question is can you see why not this program will run out of memory.
    > It consist of three files these are start.cpp,test.h and slask.h


    One possibility might be your impatience ;-)
    I added a try...catch around your loop in main and got a std::bad_alloc
    exception as expected...it took this box (Athlon XP 2400+, 256MB, WinXP)
    some 15 minutes to get there though. Meanwhile I could see the C++
    library's (mingw) exponential allocator at work: at first the program
    almost immediately hogged 256MB, then 512MB, 1GB where it became really
    slow due to swapping and new allocations were again decreasing in size
    (by a factor of 0.5) which makes sense when you take into account that
    there is an absolute maximum of 2GB of per process memory on this
    platform, which is exactly what the program had allocated when the
    exception was thrown.
    If your platform doesn't have such a "low" limit, it could take a lot
    longer run out of memory. To speed things up, you could deactivate any
    swap...another one would be to drastically increase Slask's size or
    simply alloc blocks of a GB or so if you want to force an OOM condition.

    Another possibility is that you're using an old compiler that doesn't
    throw std::bad_alloc but returns a null pointer when the allocation
    fails. Did you check that? Given that your compiler accepted the below
    error (implicit int is not part of Standard C++), maybe it has other
    non-conforming behaviour too...

    > // Here is the file containing the main funtion called start.cpp
    > #include "test.h"
    > main()

    int main()

    > {
    > Test testobj;
    > for (long i =0; i<999999999; i++)
    > testobj.foo();
    > }


    Cheers,
    Malte
     
    Malte Starostik, Apr 4, 2005
    #3
    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. bienwell
    Replies:
    4
    Views:
    3,886
    bienwell
    May 27, 2005
  2. John Salerno
    Replies:
    20
    Views:
    871
    John Salerno
    Aug 11, 2006
  3. Fabio Z Tessitore

    who is simpler? try/except/else or try/except

    Fabio Z Tessitore, Aug 12, 2007, in forum: Python
    Replies:
    5
    Views:
    389
  4. Replies:
    34
    Views:
    921
    Alf P. Steinbach
    Dec 2, 2007
  5. Replies:
    35
    Views:
    963
    Alf P. Steinbach
    Dec 2, 2007
Loading...

Share This Page