0

I want to ensure that a long-running number crunching algorithm doesn't use too much memory. The algorithm is written in C++ and runs on OS X. A drastically simplified version is:

int main() {
    while (someCondition) {
        // notice nothing is allocated on the heap
        vector<int> v(10, 0);
    }
}

I've profiled the code using Instruments (allocations and leaks). I don't see any leaks. And while the "live bytes" count looks fine (hovers around 20 MB) the "overall bytes" count keeps growing. What concerned me is when the "overall count" reached about 80 GB I received an OS X warning about lack of hard disk space (I have a 120 GB solid state disk). I don't know much about OS/process interaction so I thought I'd ask:

Is memory used by a long running process on a UNIX-based OS available to other processes before the first process is killed or no longer running?

Edit: Looks like I'm misinterpreting the "overall bytes" number in Instruments:Instruments ObjectAlloc: Explanation of Live Bytes & Overall Bytes. When I check out the process in Activity Monitor the "real memory" is essentially constant.

Community
  • 1
  • 1
SundayMonday
  • 19,147
  • 29
  • 100
  • 154
  • 1
    I'd have to say that if the usage is growing like that, something somewhere is being allocated on the heap and not freed. – Celada Feb 23 '13 at 01:17
  • Have you run the process under `valgrind`? If not, why not? Or, more to the point, do so. Something is leaking, whether it is the vector shown or something else, and you need to find it. Make sure you can stop your process. – Jonathan Leffler Feb 23 '13 at 01:28

1 Answers1

1

The reason you get a disk space warning is probably related to virtual memory allocation. Every time your process (or the OS) requests memory it is usually first "allocated" in backing-store - swap.

Total virtual memory is size of available swap plus RAM. I do not have access to OSX, and I know it plays by its own rules, but there must be a command that shows swap usage

swap -l  (Solaris)
swap -s   (Solaris)
free (linux)

The only command I came up with is vm_stat, plus top - it appears top is probably the closest to what I am talking about.

jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
  • The command on Mac OS X is not 'swap'. I'm not sure what it is, but it isn't that (or, at least, there's no `swap` in `/bin`, `/usr/bin`, `/sbin`, `/usr/sbin`, `/usr/local/bin` on Mac OS X 10.7.5). That doesn't invalidate your answer...it just qualifies it. – Jonathan Leffler Feb 23 '13 at 01:27
  • @Johnathan - you are right I was attempting to show UNIX-like commands the work elsehwere, with what I could dig up for OSX. Made edit. – jim mcnamara Feb 23 '13 at 02:23