1

In some programming contests, problems have a memory limit (like 64MB or 256MB). How can I understand the memory used by my program (written in C++) with bash commands? Is there any way to limit the memory used by the program? The program should terminate if it uses more memory than the limit.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Farzam
  • 1,272
  • 3
  • 14
  • 25
  • 1
    I suggest `help ulimit`. You may find AskUbuntu a better place for questions on basic shell features (like resource limits) and monitoring (`ps` will work for that, or `top`) – derobert Jan 31 '12 at 18:26

3 Answers3

4

The command top will give you a list of all running processes and the current memory and swap or if you prefer the GUI you can use the System Monitor Application.

As for locking down memory usage you can always use the ulimit -v to set the maximum virtual address range for a process. This will cause malloc and its buddies to fail if they try to get more memory than that set limit.

RussS
  • 16,476
  • 1
  • 34
  • 62
  • the program runs in less than one second, how could I say the `top` command to monitor my program? in the `top`'s output, which column shows the memory that is used by my program? does `ulimit -v` limit al l the memory that is used by my program?(what does virtual memory mean?) – Farzam Jan 31 '12 at 18:28
  • 1
    If your process is that fast than top would not be the best. You could try Valgrind --tool=massif (your command.) The virtual address space is all of the memory requested by your program to run. Because the operating system is in charge of giving out memory it doesn't have to actually map back to a particular place in ram. For example you might have 20kb at the beginning of a dimm and 20 kb in the middle, but the OS makes it look like one nice chunk. – RussS Jan 31 '12 at 18:34
  • what does the `valgrind --tool=massif MY_COMMAND` should print? it prints ==4587== . what does it mean? – Farzam Jan 31 '12 at 19:01
  • 1
    type "ms_print massif.out.4587" Then you will get this pretty report detailing your memory usage over time. http://valgrind.org/docs/manual/ms-manual.html#ms-manual.running-massif – RussS Jan 31 '12 at 19:15
1

Depending on how much work you want to put into it you can look at getrusage(), getrlimit(), and setrlimit(). For testing purposes you can call them at the beginning of your program or perhaps set them up in a parent process and fork your contest program off as a child. Then dispense with them when you submit your program for contest consideration.

Duck
  • 26,924
  • 5
  • 64
  • 92
1

Also, for process 1234, you could look into /proc/1234/maps or /proc/1234/smaps or run pmap 1234, all these commands display the memory map of that process of pid 1234.

Try to run cat /proc/self/maps to get an example (the memory map of the process running that cat command).

The memory map of a process is initialized by execve(2) and changed by the mmap(2) syscall (etc...)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547