arjun95's blog

By arjun95, history, 4 years ago,

is there is a way to find out the total memory being consumed by my c++ program. is there is any tool or library by which i can find memory being consumed by my program.

• +17

 » 4 years ago, # |   +1 If you're using static arrays etc., you can use Custom Invocation to find out how much memory your code takes on Codeforces' servers.
•  » » 4 years ago, # ^ | ← Rev. 2 →   0 i need to calculate memory on my computer, is there any other way? as i need to experimental comparison of space complexity of two algorithms for my project
•  » » » 4 years ago, # ^ |   +5 (sizeof(array_0) + sizeof(array_1) + sizeof(array_2) + ... + sizeof(array_k)) / 1048576.0 gives you the size in megabytes
•  » » » » 4 years ago, # ^ |   -18 is there any function as we can calculate total execution time using clock function in c++, is there a library or something else that can print total memory taken as online judges shows how much memory taken by program, i want to calculate it for experimental purpose, i know the formula that you are answer me but i want actual memory same as online judges shows to us. do you know how online judges calculating total memory consumed by our program?
•  » » » » 3 years ago, # ^ |   0 can you please explain what's this value "1048576.0" !! thanks
•  » » » » » 3 years ago, # ^ |   0 The sizeof(a) function returns the size of array a in bytes, so you need to divide the result by 2^20 = 1048576 to convert it into megabytes.
 » 4 years ago, # |   0 If you are on Linux, you can use POSIX getrusage #include #include //above libraries have to be included int getrusage(int who, struct rusage *usage); Read the man page to find out how it works. long ru_idrss; /* integral unshared data size */ long ru_isrss; /* integral unshared stack size */ These are the 2 elements of the struct you should be looking for.
•  » » 4 years ago, # ^ |   0 thanks. i understand now
 » 4 years ago, # |   +10 If you don't need those informations in the program you can use the terminal program (if you use Linux) /usr/bin/time with the -v flag. /usr/bin/time -v ./a.out output1 Command being timed: "./a.out" User time (seconds): 0.10 System time (seconds): 0.00 Percent of CPU this job got: 99% Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.11 Average shared text size (kbytes): 0 Average unshared data size (kbytes): 0 Average stack size (kbytes): 0 Average total size (kbytes): 0 Maximum resident set size (kbytes): 29268 <= This is the maximum memory usage of your program Average resident set size (kbytes): 0 Major (requiring I/O) page faults: 0 Minor (reclaiming a frame) page faults: 1361 Voluntary context switches: 1 Involuntary context switches: 3 Swaps: 0 File system inputs: 0 File system outputs: 1136 Socket messages sent: 0 Socket messages received: 0 Signals delivered: 0 Page size (bytes): 4096 Exit status: 0 
•  » » 4 years ago, # ^ |   0 When I run this command terminal shows -v command not foundreal 0m0.085s user 0m0.076s sys 0m0.004sam i missing something?
•  » » » 4 years ago, # ^ |   +5 Apparently you are using the Bash shell where time is a reserved word (see man bash — SHELL GRAMMAR — Pipelines). You have to spell it out as env time -v ./a.out output1. Or perhaps add alias time='env time' to your .bashrc.
•  » » » » 4 years ago, # ^ |   0 thanks now it works.
•  » » » » 4 years ago, # ^ |   +10 The intended way to run a program when it is shadowed by a shell built-in is > command time …
•  » » » » » 4 years ago, # ^ |   0 thanks it also works.
•  » » 16 months ago, # ^ | ← Rev. 2 →   0 Is there anything related with windows command ? Jakube
•  » » » 16 months ago, # ^ |   0 You mean you want to do the same thing on Windows?From what I know, with the Windows Subsystem for Linux you have a (almost) full Linux environment and can just execute the same thing.Alternatively you can also install any of other terminal emulators that are available, like cmder or Terminus. With those you can run the same command.There's probably also some way of getting this to work on the Windows Command Prompt, or the Windows Powershell (later one is a lot more likely than the first), but the command will be completely different, and as I don't use Windows (neither for personal things or at my company), I have no idea.
•  » » » 16 months ago, # ^ |   0 Thanks
 » 4 years ago, # |   0 if u only use arrays, or can figure out the maximum size of the data structure that you are using (vector,map,ect), you can just do mathassuming you declare int a[1000000], it means you declare 1000000 integers, 1 integer = 4 byte so 1000000 integers = 1000000 x 4 = 4000000 bytes = ~4 MB (1 B = ~1000000 MB)same thing for vectors and others, but vectors are dynamic memory, so you can do it by calculating the worst case ammount of data in the structure