1

How can I use C/C++ program to get the currently "free" memory in OSX and linux platform? In OSX I can get the "maximum" amount of the memory by sysctl and sysconf system call for linux. However I have no idea about how to get the free memory at runtime.

Jes
  • 2,614
  • 4
  • 25
  • 45

1 Answers1

4

There is no such thing as the total amount of free memory at runtime. Memory allocated by your process and used effectively most likely was used before by another process or just the file system cache, and you can actually allocate and use more memory than is physically available on your motherboard (albeit very inefficiently). The same holds for Linux and Windows: all these operating systems use virtual memory.

You can rely on system information to tell you how much physical memory is available (but this may be fake in a virtual machine). Figuring how much memory is available before swapping to compressed memory or even the SDD is more difficult and system specific, it differs from one version to another of the OS.

As a rule of thumb, use a configuration file or environment variable to tell your program how much memory to use and tune that number for your particular environment:

  • if your program executes on a dedicated system, it should leave enough memory to the system for proper operation and a decent amount of disk cache. Use tools such as top (Linux) or the activity monitor (OS/X) to see how much compressed memory or how much swap the system ends up using and reduce the setting to minimize these.

  • if your program needs to coexist with the user doing his stuff, you should reduce this quota significantly to leave at least half the memory available for other tasks.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • IIRC, there's even the problem that some (default configurations of) operating systems allow user processes to allocate more memory than available (even in virtual memory), postponing failure-at-allocation to failure-at-use. –  Nov 29 '15 at 22:31
  • Yes, I wrote that: *you can actually allocate and use more memory than is physically available on your motherboard*. How slow your system will become depends on your usage pattern of said memory. Abusing this feature may cause system failure indeed. Some systems prevent this by limiting the amount of memory allocatable by a single process or a single user, but it is easy to circumvent these limitations. – chqrlie Nov 29 '15 at 22:40
  • Maybe I wasn't clear that I meant VM overcommitment (i.e. the OS allocates more memory than the configured VM limit), not just the concept of virtual memory. It's like overbooking a flight and hoping that some passengers won't show up. –  Nov 29 '15 at 22:46
  • That's a good comparison: overbooking a flight leads to leaving some random passengers at the gate that will hopefully take to next flight. The penalty for them is very similar to what happens to processes hit by swapping, except much worse: an operation that should take a few nanoseconds ends up lasting from milliseconds to even full seconds (!) Imagine you are bounced from the shuttle flight and you wait 1 million hours for the next... – chqrlie Nov 29 '15 at 22:55