0

There's plenty of detailed instructions to measure C++ memory usage. (Example links at the bottom)

If I've got a program processing and displaying pixel data, I can use Windows Task Manager to spot memory leakage if processing/displaying/closing multiple files in succession means the executable's Memory (Private working set) grows with each iteration. Yes, not perfect but processing 1000s of frames of data, this works as a quick'n'dirty solution.

To chase down a memory bug in that (large) project, I wrote a program to accurately measure memory usage, using the Lanzelot's useful answer. Namely, the part titled "Total Physical Memory (RAM)". But if I calloc the size of 1 double, I get 577536. Even if that's a quote in bits, that's a lot..

I tried writing a bog standard program to pause, assign some memory (let's say calloc a Megabyte worth of data) and pause again before free'ing said memory. Pause are long enough to let me comfortably look at WTM. Except the executable only grows by 4 K(!) per memory assignment.

What am I missing here? Is QtCreator or the compiler optimising the assigned memory? Why does the big complex project seemingly allow memory usage resolution down to ~1MB, while whatever memory size I fruitlessly fiddle with in my simple program, bare moves the memory shown in Windows Task Manager at all?

C++: Measuring memory usage from within the program, Windows and Linux

https://stackoverflow.com/a/64166/2903608

--- Edit --- Example code, as simple as:

double *pDouble = (double*) calloc(1, sizeof(double));
*pDouble = 5.0;
qDebug() << "*pDouble: " << *pDouble;

If I look through WTM, this takes 4K (whether 1, or 1000000 double's). With Lanzelot's solution, north of 500k..

PhilPhil
  • 173
  • 4
  • 18
  • 1
    "this takes 4K" - size of one memory page. Memory allocation just reserve memory, as soon as you touch it (assign to it) - page fault happens, you get another page into your process space – Severin Pappadeux May 31 '18 at 14:35
  • And I would advice to move to ProcExp from sysinternals - it is a lot better way to watch you process(es) than WTM – Severin Pappadeux May 31 '18 at 14:41
  • The correct way to chase down a memory bug is with a profiler. If you have to do it yourself, then you want to override the operator new and delete; but really, use something like DrMemory or valgrind. Then you don't need quick or dirty - or any investigation. It just tells you where memory was allocated but not deleted – UKMonkey May 31 '18 at 14:44
  • @SeverinPappadeux So WTM effectively just shows number memory pages used (well, times 4)? That doesn't seem to tally with the memory consumption in my large project, which much more closely tracks how much memory is being used. – PhilPhil Jun 04 '18 at 09:55
  • @SeverinPappadeux By ProcExp from sysinternals, do you mean this? https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer – PhilPhil Jun 04 '18 at 09:56
  • @UKMonkey DrMemory looks great, unfortunately my program is 64 bits (which is unsupported on Windows apparently). Valgrind would be great as it's part of QTcreator. However, it's still cannot be run on Windows apparently. I wish I didn't have to do so much C++ programming under Windows.. :-p – PhilPhil Jun 04 '18 at 09:59
  • Yes, that ProcExp/procexp64 – Severin Pappadeux Jun 04 '18 at 14:04
  • Yes, Private Bytes and Working Set are in 4K pages, this is the atomic unit of virtual memory subsystem. When you ask to allocate several bytes, several outcomes might happen. One, there is memory once allocated, then freed, so malloc returns it from internal buffers. You see nothing. Two, there are no memory in buffers, malloc will grow them and return memory for you, but there is some memory in the pages of your process, again you'll see nothing. Third, no memory in buffers, and no memory in process pages. Malloc will reserve memory and ask VM subsystem to expand address space - in 4K pages. – Severin Pappadeux Jun 04 '18 at 14:44
  • @SeverinPappadeux Thank you for clarifying that. I am increasingly convinced that a "quick and dirty" solution isn't appropriate here.It's worth investing the effort into learning a proper debugging tool. [mtuner](https://milostosic.github.io/MTuner/) looks promising, though I'm currently struggling to load the symbol for Qt's DLLs. – PhilPhil Jun 06 '18 at 15:15

0 Answers0