Is there a way to get the used memory of the current running program while the program is running? | SoloLearn: Learn to code for FREE!

+27

Is there a way to get the used memory of the current running program while the program is running?

8/12/2019 6:37:39 PM

Worm

10 Answers

New Answer

+28

GAWEN STEASY but I need the used memory of my program and then the execution time doesn't help 😉

+25

GAWEN STEASY that helps to get the time the program used to execute the code between two points. I want to get the used memory of the hole program without using calloc() for each used variable. But maybe I have to use calloc() this way.. 🤔

+24

Maybe this two links help: https://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process#64166 https://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-runtime-using-c

+20

BootInk I think that is a possible solution to realize that. But if you are using array lists you have to document the number of elements of that list. I'll try a solution and when I'm ready I'll post it here.

+16

BootInk thanks!

+8

This thread might help you https://www.sololearn.com/discuss/1923548/?ref=app

+7

Worm std::chrono is used for getting execution time, you can see that in the link of the thread.

+2

The more number of times you use that specific pointer; each time it is updated to a new value. Just make sure you do all your modifications before free(), After which the used up memory is actually released and there is no particular scope to refer to it thereby

+2

I may be wrong, and this might be entirely unrelated to your question, but couldn't you implement a container (queue, list, etc) containing the pointers to all the variables, and update that whenever a new variable/pointer is created (or destroyed)? Then, when you want to retrieve the amount of memory at runtime, you could call a function that iterates through the container, de-referencing and calling the sizeof() function on each element, adding the result to an accumulator variable. If you're wanting a library function on this, I'm afraid I can't help on that.

+2

Try studying the std:list object here: http://www.cplusplus.com/reference/list/list/list/. Lists do require a starting size for each list object you make, but as you add new elements to the objects, they can dynamically change their size. However, each object is still restricted to their type, whether they are builtins, or user defined classes or structs. If you decide to use 'new' to dynamically allocate memory, for example, it would have to be the same pointer type as those already found within the list. So, you can't put a int in a float list, or you can't put a user defined object within a builtin list. Hope this helps somewhat. I've decided to study the std library for C++, and found this along the way.