Solved

Task manager indicates my c++ program memory consumption is growing?

Posted on 2016-07-17
12
89 Views
Last Modified: 2016-07-28
I have used _crt API and Microsoft Diagnotstic tools to tell me that there is no memory leak, but in Task manager, if I run the program hundreds of time, the memory slowly keep going up. Is this a real memory leak, or it is just a false alarm. How Do I fix this problem

Thanks for any help.

Evan
0
Comment
Question by:Evan Li
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 41716247
yes it is a memory leak. check that each malloc has a free
0
 

Author Comment

by:Evan Li
ID: 41716298
Thank you for answering.

Here is my testing code:

      _CrtMemState state1;
      _CrtMemCheckpoint(&state1);
       MyFunction(); //Testing my function here
      _CrtMemDumpAllObjectsSince(&state1);

The output in debug window is as follows:

Dumping objects ->
Object dump complete.

This means no leak, If I intentionally removed one free or delete, this output will tell me here I did wrong. But this tells me there is no leak, why task manager still reports memory goes up. If stop running the program for a while the memory goes down. Please explain why is that? Thanks.
0
 

Author Comment

by:Evan Li
ID: 41716301
MS diagnostic tool says no leak, MS CRT leak detecting function says not leak, what else can tell me there is a leak and tells me where is the leak? Thanks for more answer.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41716336
https://www.parasoft.com/product/insure/
I have had so much success with this Insure++ product in both C and C++ projects, that I suggest that you take advantage of their free trial. (They may say 2 days, but you should be able to negotiate 2 weeks or more if you need it.) Their technical support during the trial period was great. I used this product from 1995 through 2005.

It will identify where the memory leaks are occurring as well as problems in over a hundred categories.
0
 
LVL 32

Expert Comment

by:sarabande
ID: 41717093
it not necessarily is a leak. if your program requires dynamic heap memory, it rarely could avoid memory is increasing, since the heap manager would not reuse smaller pieces of free memory, as long as it could get larger parts.

to come out from this, you could try to use stack memory before heap memory wherever possible, for example instead of

MyClass * p = new MyClass;
p->run();
delete p;

Open in new window

you should do:

MyClass mc;
mc.run();

Open in new window


if you were using dynamic array, you could reserve memory for a reasonable size of the array such that resizing of the container only would be needed sometimes. often you could use static containers if multi-threading is not required, and so on. a good method against Memory growing also could be to 'restart' your program on idle times, that is to start a new instance of your program and pass current memory data to the new instance before exiting the old instance. such doing also has the advantage that you can 'update' your program with the new instance.

Sara
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41717416
>> if I run the program hundreds of time, the memory slowly keep going up. Is this a real memory leak, or it is just a false alarm.

When you close your program after each of the "hundreds of time", does task manager still show total memory usage as increasing, or does the usage drop to where it was before?

Could it be that each time you do your run, the input increases a small amount resulting in more memory usage?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 32

Expert Comment

by:sarabande
ID: 41717473
or does the usage drop to where it was before?

actually, i never experienced that memory dropped down while i was watching a normal process in taskmanager (and therefore no other memory-consuming process was active). the only way i ever seen that a process freed a considerable amount of memory (and showed it in taskmanager view) was when the process freed a very big amount of memory, and a second process required memory somewhat less than that was freed by the first process.

so from my experience it is wasted time to watch memory consuming with taskmanager since the figures are not reliable. better buy some more RAM if you feel it is going to become an issue.

Sara
0
 

Author Comment

by:Evan Li
ID: 41717648
Thank Sara, is there any garbage collector function can help to reduce the memory the memory consumption at least on Taskmanager?
0
 
LVL 32

Expert Comment

by:sarabande
ID: 41717965
from my experience garbage collection ease memory management on the cost of memory consuming. of course it is fine if you don't have to care for freeing memory space but of course if you hold references to memory no longer used, the gc was not able to free the memory. so actually it is a "sunshine technology" which worked well if you - anyhow - be aware of the lifetime of objects. if you are not, it could make things worse.

Sara
0
 

Author Comment

by:Evan Li
ID: 41719540
Are you saying that GC does not collect memories that were broken in pieces, it do more harm than do good?
0
 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
ID: 41720919
Are you saying that GC does not collect memories that were broken in pieces,

it is not a matter of gc but of windows and heap manager. gc only checks whether an object is still referenced or could be freed. the heap manager always would try to use smaller pieces of free manager if possible. but assume a dynamically growing array which needs contiguous memory. in case of growing, the heap Manager firstly would the new bigger piece of contiguous memory, then copy the contents, then free the smaller piece. obviously, it is a rare chance that one of those smaller pieces ever could be used for this array again since the memory would get more and more fragmented.  

Windows taskmanager will not get full Information of the memory used by your process. it only will get the total of the requested memory minus those chunks which were returned by your heap as no longer needed. at this point accuracy gets lost, since the heap Manager only would return memory if it was requested by Windows. it only would return bigger pieces of contiguous memory. this will happen when another process requests more memory than available or periodically on idle times. as told, from my experience you can't wait on this especially if your computer is well-equipped with physical memory or if it is not used to capacity. even then, because of fragmentation, memory used by your process rarely will go down and the total memory shown in taskmanager will become less and less reliable.

Sara
1
 

Author Closing Comment

by:Evan Li
ID: 41734201
Thank Sara for your help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now