Solved

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

Posted on 2016-07-17
12
125 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 80

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
Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

 
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 33

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 33

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 33

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 33

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

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

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

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

830 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