Why OS X use swap when there is lots of "inactive memory"?

22

13

I am using OS X from few months (Lion and now Mountain Lion). I have 8 GB on my mini and almost daily now it get close to that. On Windows 7 machine with 8 GB I never had that kind of problem. Anyway, I read over the net, that the inactive memory is app cache of the programs that are recently closed and can be used for faster reopening.And this inactive memory can be released to a new app if needed. It is not released. Instead OS X starts swapping. So my question is why OS X use swap when there is lots of "inactive memory"? Here a screen that shows what I mean:

enter image description here

I really hope there is a away to make OS X to use those 2.69 GB before start swapping.I really do.

Balchev

Posted 2012-08-12T12:29:16.777

Reputation: 315

Yea it is strange. Is it one application taking up all the RAM? Maybe OSX limits applications not to use more than 80% RAM at once. Inactive does not mean its free though. It is reserved for something. – Piotr Kula – 2012-08-17T10:36:24.450

No - many apps, Browsers, Eclipse and etc From what I read it is free since is a recently closed apps cache.There should be way to make OS X to not swap when there is inactive memory – Balchev – 2012-08-17T12:33:32.387

I can reproduce it anytime and can make a screen of the activity monitor if is needed – Balchev – 2012-08-17T12:43:23.573

2Inactive memory is not free memory. – kinokijuf – 2012-08-19T12:16:28.813

2@kinokijuf it should, however, act as free memory when there is no free memory left. If inactive memory is always swapped to disk anyway, there's no real point in making the active - inactive distinction. – Pieter – 2012-08-20T12:58:00.730

You should read this ServeFault answer. Swap isn't always bad, and there are different strategies an operating system might use: http://serverfault.com/questions/305295/why-does-windows-2008-use-swap-before-the-memory-is-full/305354#305354

– Joel Coehoorn – 2012-12-19T22:55:53.883

Answers

18

The swapping apparently happened when the inactive RAM pages were actually active.

(Update: as it was clarified in a comment, this is not your case. So people with the same problem can skip forward to the horizontal rule.)

I.e. you had many programs running and the kernel swapped-out some pages. Then you quitted some programs. The kernel marks their RAM pages as inactive. But it won't swap-in pages back to RAM until these pages are needed. This results in having both inactive and swapped-out pages.

Why not preemptively swapping-in pages? Because that would be betting against the odds: in the long-run you lose. Let's think of a simplified example: Two programs A and B that don't fit in RAM at the same time. Program A is still running and all the swapped-out pages belong to A. Program B has quitted and all the inactive pages belong to B.

If kernel preemptively swaps-in A's pages and imediately after:

  • program A needs to access it's pages -> You win - the pages are already in the RAM.
  • you launch B again -> You lose - you "paid" the cost for bringing the pages to RAM and now you have to send them back.
  • you launch another program C -> You lose if A and C don't fit in the RAM at the same time. If they fit, you are even.

Also take into account that swapping-out (writing to disk) is more expensive than swapping-in (reading from disk). Which makes this "bet" even more unatractive.

In short: trust your kernel and don't try to outsmart it.


Update: Turns out that inactive memory doesn't work as the Using Activity Monitor to read System Memory article has led many people to believe it works. The definition given in the article for inactive memory is correct:

This information is in RAM but it is not actively being used, it was recently used.

But the following example is totally misleading and over-simplified (like my example to be frank):

For example, if you've been using Mail and then quit it, the RAM that Mail was using is marked as Inactive memory. Inactive memory is available for use by another application, just like Free memory. However, if you open Mail before its Inactive memory is used by a different application, Mail will open quicker because its Inactive memory is converted to Active memory, instead of loading it from the slower drive.

I searched for more online resources and ended up to this thread in the darwin kernel mailing list which is quite informative. Quoting Jim Magee (from the darwin team - I think):

In short, the kernel VM system when dealing with memory pressure scans through in-use pages and tries to keep them in a balance between active and inactive markings. The inactive pages are scanned for reuse while marked as inactive. If they have been reused, they are marked as active and some other page must move from active to inactive state to detect if it is in active use. So, inactive is a misnomer. It is shorthand for "possibly inactive, lets try to verify that."

As you discovered, the internal balance we (currently) strive for is approximately 2/3 active vs 1/3 inactive...

This explains the behaviour you observe. I.e. the inactive pages you see belong to running programs which haven't been recently used. So, when you fire-up a new program, inactive pages are swapped out. At the same time pages from other programs are marked as inactive to maintain the 2/1 ratio of active vs inactive.

The thread also contains some suggestions to learn more about the darwin internals. There are also some suggestions in case you started investigating the memory usage because of beachball problems (which usually have little to do with it).

The conclusion remains the same: Trust your kernel and don't try to outsmart it. :-)

m000

Posted 2012-08-12T12:29:16.777

Reputation: 846

1Hi, Not sure you understand me - I am talking for the moment when you have like 100-200MB free memory, 2.6 GB "inactive" and start another program, let say eclipse, xcode and etc What happens is that it does not use those 2.6 GB and instead swap from the active memory.Not sure if you get what I mean.Anyway, thanks for your answer – Balchev – 2012-08-23T11:04:35.720

This is clear now. Maybe you should add the example in this comment to your question. I have added additional information which I think provide an adequate explanation for what you observe. – m000 – 2012-08-23T20:29:11.723

Not sure if I shall edit-out my original answer. It explains a different case (as I didn't get your question right before your comment) and may confuse people. – m000 – 2012-08-23T20:31:07.480

So as Radoo says in his comment - "OS X is a hungry beast". I wasn't expecting OS X to be that much memory hungry (both Lion and now Mountain Lion) and that's why i thought there is something fishy here.Thank you for your updated answer. – Balchev – 2012-09-10T08:33:12.553

6

You can disable paging safely if you have enough ram.

Try these commands.

sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist
sudo rm /private/var/vm/swapfile*

Then restart and verify that dynamic_pager process is no longer running.

Make sure no swapfiles were created in /private/var/vm/.

To re enable try following commands:

sudo launchctl load -wF /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist

You can also disable Spotlight to free up more ram and reduce disk activity. The following commands are used to disable and enable Spotlight.

sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist
sudo launchctl load -wF /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

arundevma

Posted 2012-08-12T12:29:16.777

Reputation: 1 404

3“enough memory, like 8 Gb” – sorry, that’s so cute… :) – Bombe – 2016-05-15T12:59:36.120

I read over the net that disabling swap on OSX may lead to unexpected behavior of the system and crashes. Is this true? – Balchev – 2012-08-23T11:11:06.410

When your memory is low it will. But if you have enough memory, like 8 Gb, it wont cause any problem. – arundevma – 2012-08-23T13:00:04.413

0

There's not much you can do. Why it does that? Because it's just how OS X works. The interesting part is how the occupied memory is getting bigger with every sleep the Mac gets.

What can you do:

  1. Upgrade RAM.
  2. Run the holy "purge" command to clear up most of the inactive memory, whenever is necessary.
  3. Disable the swap http://osxdaily.com/2010/10/08/mac-virtual-memory-swap/

user127350

Posted 2012-08-12T12:29:16.777

Reputation:

Hi, I used purge command, but most of the time it free less then half of the "inactive" memory. I read that disabling swap on OSX may lead to unexpected behavior of the system and crashes.So I went with 1) Now it is fine with 16 gb :) Just that amount of memory use to be for servers, not desktops :) Thanks for your answer – Balchev – 2012-08-23T11:09:32.200

I have 16GB on my Macbook pro, that's not a server. :) It's well known OS X is a hungry beast when it comes to memory, especially when multiple mid/big apps are used at the same time. I managed to fill all those 16GB without opening any image/video editing software, just some games and small apps. The inactive memory was about 4GB... – None – 2012-08-23T12:16:59.470

You must also take into account that many of OS X apps are already 64bit. 64bit apps allocate more memory than 32bit apps, due to addressing space getting bigger. – None – 2012-08-23T12:20:21.680

0

The system is working as designed. Even when there's no memory pressure, it can make sense to write some pages that cannot be discarded but haven't been recently used to swap when the system isn't busy. That way, if there is memory pressure later, these pages can be evicted from RAM without having to first write them to swap while the system is busy.

David Schwartz

Posted 2012-08-12T12:29:16.777

Reputation: 58 310

1it's badly designed then. it's too aggressive in allocating and using swap space. – mendota – 2018-05-29T23:12:06.797

Why does it matter? If there's lots of free swap, allocating swap space is harmless. This is especially true when the swap doesn't contain needed data because removing the allocations has no cost. This is good design -- doing hard work when it's nearly costless rather than deferring it to later when the system is under pressure. – David Schwartz – 2018-05-30T12:28:58.573

2it's bad design when it's too aggressive and causes stuttering or hangups in programs it mistakenly tags as inactive and starts moving into swap. meanwhile there's another eight gigs free in RAM :/ – mendota – 2018-05-30T16:20:00.947

I agree with mendota. Once I completely disabled swap, my system is running smoothly without any stuttering. – Anton Kuzmin – 2019-09-19T07:29:53.330

@AntonKuzmin That really has nothing to do with what this question is about. If you read this question, it says nothing at all about stuttering or any problems whatsoever. – David Schwartz – 2019-09-19T16:00:07.740