0

Is it possible to allocate big chunk (512Mb-1Gb with 4Gb installed) of real memory without dropping it to swap?

My final intention is to free system memory: os x has a problem when free memory is near 0 — it doesn't try hard to put unused memory to swap and everything becomes very slow trying to get memory. So, I decided that if some process grabs some memory (I grab 1/8 to 1/4 of total system memory) by force, and than frees it, than system will become responsible again. I wrote little ruby script which created and filled memory disk (hdiutil is the only way I found to grab real memory using external utils) and now I decided to rewrite it using just C.

tig
  • 25,841
  • 10
  • 64
  • 96
  • see also http://stackoverflow.com/questions/14157002/can-calloc-or-malloc-be-used-to-allocate-only-physical-memory-in-osx – Nickolay Jun 18 '13 at 23:27

1 Answers1

2

The mlock function will lock pages into physical memory. I'm not sure what the limits are.

You should be extremely sure you really want to do this. It's not a nice thing to do to the rest of the system.

Ken
  • 12,933
  • 4
  • 29
  • 32
  • Already found reading man pages :) But for someone it will be easier to find answer. I'll explain why I did this in question. – tig Jul 09 '10 at 12:12
  • 2
    Ah. See also /usr/bin/purge. It doesn't do the same thing, but it will typically free up a lot of memory. – Ken Jul 09 '10 at 12:58
  • Ouch! purge is hard, it works good (it freed about 1Gb of unused memory), but it unpleasantly totally freezes system for 20-30 seconds. Though thank you for sharing knowledge! – tig Jul 09 '10 at 13:21