Memory problems (possibly very easy question)

H

hamishd

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?
 
G

Gavin Deane

hamishd said:
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
 
G

Gernot Frisch

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.
 
P

peter koch

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
 
D

Dan Bloomquist

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top