Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Memory problems (possibly very easy question)

Reply
Thread Tools

Memory problems (possibly very easy question)

 
 
hamishd
Guest
Posts: n/a
 
      11-28-2006
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[i]);
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?

 
Reply With Quote
 
 
 
 
Gavin Deane
Guest
Posts: n/a
 
      11-28-2006

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[i]);
> 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

 
Reply With Quote
 
 
 
 
Gernot Frisch
Guest
Posts: n/a
 
      11-28-2006
>
> 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[i]);
> 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.


 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      11-28-2006

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[i]);
> 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

 
Reply With Quote
 
Eric Hill
Guest
Posts: n/a
 
      11-28-2006
> 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
 
Reply With Quote
 
Dan Bloomquist
Guest
Posts: n/a
 
      11-28-2006

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[i]);
> 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?
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
very very very long integer shanx__=|;- C Programming 19 10-19-2004 03:55 PM
very very very long integer Abhishek Jha C Programming 4 10-17-2004 08:19 AM
Very, very, easy question John Hoge ASP .Net 3 05-28-2004 07:10 AM
Quick Book file access very very very slow Thomas Reed Computer Support 7 04-09-2004 08:09 PM
very Very VERY dumb Question About The new Set( ) 's Raymond Arthur St. Marie II of III Python 4 07-27-2003 12:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57