arjun95's blog

By arjun95, history, 8 years ago, In English

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.

  • Vote: I like it
  • +17
  • Vote: I do not like it

| Write comment?
»
8 years ago, # |
  Vote: I like it +1 Vote: I do not like it

If you're using static arrays etc., you can use Custom Invocation to find out how much memory your code takes on Codeforces' servers.

  • »
    »
    8 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    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

    • »
      »
      »
      8 years ago, # ^ |
        Vote: I like it +5 Vote: I do not like it

      (sizeof(array_0) + sizeof(array_1) + sizeof(array_2) + ... + sizeof(array_k)) / 1048576.0 gives you the size in megabytes

      • »
        »
        »
        »
        7 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        can you please explain what's this value "1048576.0" !! thanks

        • »
          »
          »
          »
          »
          7 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          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.

        • »
          »
          »
          »
          »
          2 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          1 kb=1024 bytes 1 mb=1024 kb 1 mb=1024*1024=1048576

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

If you are on Linux, you can use POSIX getrusage

#include <sys/resource.h>
#include <sys/time.h>
//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.

»
8 years ago, # |
  Vote: I like it +10 Vote: I do not like it

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 <input1 >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
  • »
    »
    8 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    When I run this command terminal shows -v command not found

    real 0m0.085s user 0m0.076s sys 0m0.004s

    am i missing something?

    • »
      »
      »
      8 years ago, # ^ |
        Vote: I like it +5 Vote: I do not like it

      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 <input1 >output1. Or perhaps add alias time='env time' to your .bashrc.

      • »
        »
        »
        »
        8 years ago, # ^ |
          Vote: I like it +10 Vote: I do not like it

        The intended way to run a program when it is shadowed by a shell built-in is

        > command time …
  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Is there anything related with windows command ? Jakube

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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.

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 math

assuming 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