How to determine CPU and memory consumption from inside a process in C++?
Hello everyone! I've been writing in C++ for about a year, and I've encountered a problem that has stumped me. I need my program (an image processing server) to monitor in real time how much memory and CPU it consumes. The problem is that I don't understand how to get this data from inside the process itself.
For example, I want to output something like this to the log: "Peak memory consumption: 150 MB, CPU load: 45%". I tried using simple variables like `sizeof()` for objects, but that doesn't give the real picture — memory is allocated on the heap, plus there are overheads for the stack and system resources. With CPU it's even more complicated: I can measure the execution time of a piece of code using `clock()`, but that time is only for my thread, not the entire program, and it doesn't account for I/O wait time.