473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1506

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: jacob navia | last post by:
In C, we have read-only memory (const), read/write memory (normal data), and write only memory. Let's look at the third one in more detail. Write only memory is a piece of RAM that can only...
3
by: Jerry Morton | last post by:
Hi, My ASP.NET application will be installed on multiple web sites on one server. This means that the application DLL will be loaded once for each installation - which seems inefficient. If I...
9
by: burningsunorama | last post by:
Hi guys! This is maybe a too 'academic problem', but I would like to hear your opinions, something like pros and cons for each approach.... ... Recently we've had at work a little talk about the...
6
by: pauldepstein | last post by:
Could anyone explain (by giving a URL possibly) how to solve problems of the form "How much memory does the following piece of code take?" For example, consider the declaration/definition ...
2
by: Robert | last post by:
Hello javascript group readers, I have a question regarding how to prevent memory leaks in Internet Explorer when using closures. I already knew about the circular reference problem, and until...
2
by: a_agaga | last post by:
Do you know are there some reasons why many do not make processes to communicate through memory? Why network connections (sockets) are used so commonly in IPC (inter process communication)...
13
by: pratap | last post by:
how could i find out how much memory is blocked(or has been allocated to a pointer) consider, int *p=new int; or int *p=new int; suppose i dont know the right hand side of the statement...
9
by: dspfun | last post by:
Hi! I have stumbled on the following C question and I am wondering wether the answer is implementation specific or if the answer is always valid, especially question b). BRs! ...
13
by: Pep | last post by:
I have recently eradicated a lot of memory leaks in a very old C++ source set. However, whilst they were all fairly easy to resolve, I am confused by the last one. This seems to be related to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.