Solved

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

Posted on 2016-07-17
12
163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 81

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 34

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
 
LVL 34

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 34

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 34

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

Don't Miss ATEN at InfoComm 2017!

Visit booth #2167 to see the  new ATEN VM3200 32 x 32 Modular Matrix Switch. Other highlights include the VE8950 4K HDMI Over IP Extender, VS1912 12-Port DP Video Wall Media Player  and VK2100 ATEN Control System. Register now with Free Pass Code ATEN288!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Search file 10 29
global cyber attack of may 2017 2 58
Unable to start workstation service 12 407
COM server issue 2 24
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

734 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