469,883 Members | 1,210 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,883 developers. It's quick & easy.

Memory problems (possibly very easy question)

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?

Nov 28 '06 #1
5 1351

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

Nov 28 '06 #2
>
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.
Nov 28 '06 #3

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

Nov 28 '06 #4
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
Nov 28 '06 #5

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?
Nov 28 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by jacob navia | last post: by
3 posts views Thread by Jerry Morton | last post: by
6 posts views Thread by pauldepstein | last post: by
2 posts views Thread by a_agaga | last post: by
13 posts views Thread by pratap | last post: by
13 posts views Thread by Pep | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.