High memory usage

Hello all,

I have developed a program in VS c++ which is about the solving of a large system of non-linear equations. I added a new solver to my program as a new header file but when I run it, it gradually uses all the memory (16 GB). Is there any way to find which part or which array is using this high memory?


Look for Analyze->Performance Profiler or press Alt+F2. This will open the performance profiler. Make some snapshots and see what consumes your memory. If it uses 16GB then that's a pretty big memory leak.
I opened performance and diagnostics then I chose .NET memory allocation (track managed memory allocation) but after execution, it says "no data was collected". I installed C++ Memory Validator software to find the leak source and I analyzed my program with that. There is much information in the memory allocation for example following is one of the parts with a high memory usage. It says that it is related to the matrix mult in Multiply function but I can't understand what is the problem because it's similar to my other functions.

id:297,087 <<7,633,054 objects>> double[] : 366,386,592 bytes, largest allocation 48 bytes at 0x0000025bd1c03ad0 : [c:\desktop\code\gmres.h Line 80]

code.exe Multiply : [c:\desktop\code\gmres.h Line 80]
75 : double **Multiply(int r1, int c1, int r2, int c2, double **A, double **B)
76 : {
77 :
78 : double **mult = new double*[r1];
79 : for (int i = 0; i <= r1 - 1; i++)
80 : mult[i] = new double[c2];
81 :
82 : // Initializing elements of matrix mult to 0.
83 : for (int i = 0; i <= r1 - 1; i++)
84 : {
85 : for (int j = 0; j <= c2 - 1; j++)

code.exe GMRES : [c:\desktop\code\gmres.h Line 292]
287 : {
288 : double**AV = new double*[n];
289 : for (int i = 0; i <= n - 1; i++)
290 : AV[i] = new double[m+1];
291 :
292 : AV = Multiply(n, n, n, m+1, A, V);
293 : for (int j = 0; j <= n - 1; j++)
294 : av[j] = AV[j][i];
295 :
296 : delete[] AV;
297 :

I opened performance and diagnostics then I chose .NET memory allocation (track managed memory allocation)

Because you don't use .NET. Don't select managed memory.
Last edited on
You're asking for trouble, managing dynamic memory directly. You can't do it properly by being careful. Things like
double**AV = new double*[n];

are prone to be leaked.

That's kinda the problem with C. C++ offers lots of help: standard containers, smart pointers, user defined types.
Last edited on
Why do you allocate memory on line 288, when the function Multiply does this as well on line 78 ?
Watch out of high memory use, it tends to make problem and create errors. Unless your'e using some programs as checkmarx it is going to be hard to detect those errors.
Good luck!
C does not use new.
therefore its C++ and vectors could be used. For performance reasons, you want your memory to hang around rather than allocate and destroy all over the place, which vectors can disguise. Double allocation and delete of temporary intermediate values kills matrix classes if you have more than trivial amounts of rows and cols. That aside, use vectors, and watch the memory abuse :)
I'd recommend you to watch out with spending all of your memory as it tends to generate some bugs that are really hard to be detected later. There are program that might help with it, such as checkmarx for example, but it recommended to try and avoid this kind of problem.
Good luck.
Topic archived. No new replies allowed.