Memory problems (possibly very easy question)

Discussion in 'C++' started by hamishd, Nov 28, 2006.

  1. hamishd

    hamishd Guest

    Hello,

    In my application I create some large vectors to hold data. For
    example:

    std::vector<DataItemClass*> MyData;
    DataItemClass * DataItem;

    for(i=0;i<SomeLargeLimit;i++){
    DataItem = new DataltemClass;
    MyData.push_back(DataItem);
    }


    So while my application is running, I can watch in the "Windows Task
    Manager" under "Processes" my application's mem usage increase as I
    allocate more memory. Eg, say to 150,000KB.

    How do I release this memory?

    I go:
    for(i=0;i<MyData.size();i++)
    delete(MyData);
    MyData.clear();

    But my mem usage does not reduce... so when i run the process a few
    times the usage gets really really large, and windows gives me virtual
    memory usage warnings (or similar).

    When I run the application in debug mode, the same thing occurs, and i
    can confirm the "delete" operations are being executed. MSVC++ 6.0 does
    not show any memory leaks, so why is my memory usage not clearing?
     
    hamishd, Nov 28, 2006
    #1
    1. Advertising

  2. hamishd

    Gavin Deane Guest

    hamishd wrote:
    > Hello,
    >
    > In my application I create some large vectors to hold data. For
    > example:
    >
    > std::vector<DataItemClass*> MyData;
    > DataItemClass * DataItem;
    >
    > for(i=0;i<SomeLargeLimit;i++){
    > DataItem = new DataltemClass;
    > MyData.push_back(DataItem);
    > }
    >
    >
    > So while my application is running, I can watch in the "Windows Task
    > Manager" under "Processes" my application's mem usage increase as I
    > allocate more memory. Eg, say to 150,000KB.
    >
    > How do I release this memory?
    >
    > I go:
    > for(i=0;i<MyData.size();i++)
    > delete(MyData);
    > MyData.clear();


    What does MyData.capacity() return here. clear erases the elements from
    the vector, so after this code MyData.size() will return zero. However,
    the memory held by the vector is not necessarily released.

    If capacity does not return zero, the vector is keeping hold of its
    storage space for future use, as it is allowed to do. The following
    trick may help instead of calling clear (but make sure you keep your
    loop that deletes all your DataItemClass objects).

    std::vector<DataItemClass*>().swap(MyData);

    See http://www.gotw.ca/gotw/054.htm

    Gavin Deane
     
    Gavin Deane, Nov 28, 2006
    #2
    1. Advertising

  3. >
    > std::vector<DataItemClass*> MyData;
    > DataItemClass * DataItem;
    >
    > for(i=0;i<SomeLargeLimit;i++){
    > DataItem = new DataltemClass;
    > MyData.push_back(DataItem);
    > }
    > for(i=0;i<MyData.size();i++)
    > delete(MyData);
    > MyData.clear();


    delete without () is enough.
    It might be that your OS is not requiring the memory, so it's not
    freeing it. this might speed up re-allocations later. Try to open some
    other app that really uses a lot of ram and see if it gets freed then.
     
    Gernot Frisch, Nov 28, 2006
    #3
  4. hamishd

    peter koch Guest

    hamishd skrev:
    > Hello,
    >
    > In my application I create some large vectors to hold data. For
    > example:
    >
    > std::vector<DataItemClass*> MyData;
    > DataItemClass * DataItem;
    >
    > for(i=0;i<SomeLargeLimit;i++){
    > DataItem = new DataltemClass;
    > MyData.push_back(DataItem);
    > }
    >
    >
    > So while my application is running, I can watch in the "Windows Task
    > Manager" under "Processes" my application's mem usage increase as I
    > allocate more memory. Eg, say to 150,000KB.
    >
    > How do I release this memory?
    >
    > I go:
    > for(i=0;i<MyData.size();i++)
    > delete(MyData);
    > MyData.clear();
    >
    > But my mem usage does not reduce... so when i run the process a few
    > times the usage gets really really large, and windows gives me virtual
    > memory usage warnings (or similar).


    This means there is a problem somewhere.

    >
    > When I run the application in debug mode, the same thing occurs, and i
    > can confirm the "delete" operations are being executed. MSVC++ 6.0 does
    > not show any memory leaks, so why is my memory usage not clearing?


    Because you made a fault elsewhere. Can you provide a complete program
    to demonstrate your fault? Do that - e.g. providing a dummy
    DataItemClass and see if the problem reproduces. If it does, post
    again. My suspicion is that the culprit is the DataItemClass
    destructor.
    By the way: VC++ 6.0 is REALLY old. You ought to replace it with
    something more modern and more C++.

    /Peter
     
    peter koch, Nov 28, 2006
    #4
  5. hamishd

    Eric Hill Guest

    > But my mem usage does not reduce... so when i run the process a few
    > times the usage gets really really large, and windows gives me virtual
    > memory usage warnings (or similar).


    If you minimize the application, does the memory usage drop? If so, have
    a look at this article:

    http://support.microsoft.com/kb/293215

    Eric
     
    Eric Hill, Nov 28, 2006
    #5
  6. Interesting. On my machine:

    404MB
    TC tc;
    for( long i= 0; i < 2000000; ++i )
    tc.push_back( new CTest );
    540MB
    for( ITC it= tc.begin( ); it != tc.end( ); ++it )
    delete *it;
    413MB
    {
    TC temp;
    temp.swap( tc );
    }
    404MB

    Best, Dan.


    hamishd wrote:

    > Hello,
    >
    > In my application I create some large vectors to hold data. For
    > example:
    >
    > std::vector<DataItemClass*> MyData;
    > DataItemClass * DataItem;
    >
    > for(i=0;i<SomeLargeLimit;i++){
    > DataItem = new DataltemClass;
    > MyData.push_back(DataItem);
    > }
    >
    >
    > So while my application is running, I can watch in the "Windows Task
    > Manager" under "Processes" my application's mem usage increase as I
    > allocate more memory. Eg, say to 150,000KB.
    >
    > How do I release this memory?
    >
    > I go:
    > for(i=0;i<MyData.size();i++)
    > delete(MyData);
    > MyData.clear();
    >
    > But my mem usage does not reduce... so when i run the process a few
    > times the usage gets really really large, and windows gives me virtual
    > memory usage warnings (or similar).
    >
    > When I run the application in debug mode, the same thing occurs, and i
    > can confirm the "delete" operations are being executed. MSVC++ 6.0 does
    > not show any memory leaks, so why is my memory usage not clearing?
    >
     
    Dan Bloomquist, Nov 28, 2006
    #6
    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. John Hoge

    Very, very, easy question

    John Hoge, May 27, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    390
  2. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    510
    Raymond Hettinger
    Jul 27, 2003
  3. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,704
    Merrill & Michele
    Oct 19, 2004
  4. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    454
    jacob navia
    Oct 17, 2004
  5. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    534
    Dave Thompson
    Feb 14, 2005
Loading...

Share This Page