What you're seeing is normal, and desired behavior.
OS X, like Linux and BSD, has a disk cache. This means anything read from or written to disk is kept in memory. The "Inactive" memory includes this disk cache.
This answer on AskDifferent explains it, as does this post on macosxhints.com:
However, X's underpinnings (ie the UN*X kernel) provide both these
features without any inputs being needed by the user. It's called the
file system buffer cache. The one most significant difference is that
the size of this buffer cache is dynamic. It starts of with some small
size and can grow and shrink as the i/o demands and Apps memory
requirements vary over time.
It's called a 'buffer cache' because it buffers the i/o data on its
way to/from the disk. When an App writes data it first will be
deposited into the Apps file buffer memory region and will
subsequently be requested via library routines to have the kernel (the
OS) copy it from the App's buffer to disk. The kernel will oblige and
will copy it first to its buffer -- the file system buffer cache. If
the kernel requires more room in its buffer cache it will obtain it
from the free memory. When this happens the free memory value, in say
the Terminal's top command, will immediately show a reduction of free
memory. At some later point the kernel will copy this data (referred
to has dirty buffers) to the appropriate disk location. I believe the
frequency of this being done is 30 secs -- called sync-ing to disk.
Note that having memory paged out to disk is not always a bad thing. If memory pages are completely inactive, paging them out to disk can improve performance, because they are wasting RAM which could otherwise be used for disk cache. Once they are paged out and you stop watching movies, when needed they will be paged back in, automatically replacing the buffers and disk cache. You don't need to run purge
each time. Just let OS X manage the disk cache as it was designed to do.
For more information on this, consult the wikipedia article on paging, and the article on the Page cache.
TL;DR information:
Here's an article describing how swap works under Linux. WHile not 100% applicable to OS X, the concept is the same. I'll quote the relevant part:
When an application needs memory and all the RAM is fully occupied,
the kernel has two ways to free some memory at its disposal: it can
either reduce the disk cache in the RAM by eliminating the oldest data
or it may swap some less used portions (pages) of programs out to the
swap partition on disk. It is not easy to predict which method would
be more efficient. The kernel makes a choice by roughly guessing the
effectiveness of the two methods at a given instant, based on the
recent history of activity
Since you're watching a DVD or a movie, the most recently active pages of memory are the disk cache, so OS X decides to keep that in RAM and swap out pages of memory which have been inactive from before you started accessing the video files. Once you stop accessing video files and access the programs using the memory pages which were swapped out, the reverse is true: OS X will discard the disk cache containing the video data and replace it with the memory pages which it swapped out. Then your swap usage will decrease.
Here's a bit more information, specific to FreeBSD (which is similar to Mac OS X):
FreeBSD will use 'all of memory' for the disk cache. What this means
is that the 'free' bucket typically contains only a few pages in it.
If the system runs out, it can free up more pages from the cache
bucket.
System activity works like this: When a program actively references a
page in a file on the disk (etc...) the page is brought into the
buffer cache via a physical I/O operation. It typically goes into the
'active' bucket. If a program stops referencing the page, the page
slowly migrates down into the inactive or cache buckets (depending on
whether it is dirty or not). Dirty pages are slowly 'cleaned' by
writing them to their backing store and moved from inactive to cache,
and cache pages are freed as necessary to maintain a minimum number of
truely free pages in the free bucket. These pages can still be
'cleaned' by allocating swap as their backing store, allowing them to
migrate through the buckets and eventually be reused.
[...]
The VM buffer cache caches everything the underlying storage so, for
example, it will not only cache the data blocks associated with a file
but it will cache the inode blocks and bitmap blocks as well. Most
filesystem operations thus go very fast even for tripple-indirect
block lookups and such
[...]
FreeBSD has arguably some of the best swap code in existance. I
personally like it better then Linux's. Linux is lighter on swap, but
doesn't balance system memory resource utilization well under varying
load conditions. FreeBSD does.
FreeBSD notes the uselessness of existing pages in memory, and decides
that it might be advantageous to free memory (enabled by pushing pages
to swap), so that it can be used for more active purposes (such as
file buffering, or more program space.) It is a terrible waste to
keep unused pages around, for the notion of saving (cheap) disk
space. Since low level SWAP I/O can be faster, with less CPU overhead
than file I/O, it is likely desireable to push such unused pages out
so that they can be freed for use by higher overhead mechanisms.
(note 1)
(Emphasis mine, check that bolded part for specifically what you're asking about, that is, "Why is swap being used when I have inactive memory?)
I would try adjusting your procedure. Since as noted the quoted steps provided below in practice are causing you to unintentionally wake the Mac from sleep in step 3, which may also problematically be waking it from sleep before it finishes going to sleep in the first place. So in a sense you were telling your MacBook Air to go to sleep and while your at it wake up from sleep, remove a display and all this other stuff at the same time. That may be the cause of the freezing, the shock of all the external devices getting removed while in sleep or while the mac is going to sleep.
- Move mouse to HotCorner to put monitor to sleep (I do this instead of closing the lid since Lion has lid closing assume the external is
taking over as primary display)
- Close lid to put computer to sleep
- Disconnect cables (power, display port, usb; maybe ethernet and audio depending on location).
USB activity as noted in Mac OS X: Why your Mac might not sleep or stay in sleep mode is a perfect way to wake your mac from sleep.
I would recommend doing the following instead:
- Save your important files and data in open applications in case things go wrong.
- Disconnect cables (power, display port, usb; maybe ethernet and audio depending on location).
Wait for you mac to recognize that the external display is disconnected, usually screen fades to blue for a few seconds, before going back to normal view of your Mac. etc.
Close lid to put computer to sleep or choose Apple > Sleep, etc...
Verify the sleep indicator light is slowly pulsing, before placing the MacBook into any case.
So ya try this out and see if it stops the random freezing. If it does not then there may be other hardware / software problems at hand, check your system logs, etc.
Best Answer
You could use a software like Caffeine to easily achieve your need :
Caffeine is a tiny program that puts an icon in the right side of your menu bar. Click it to prevent your Mac from automatically going to sleep, dimming the screen or starting screen savers. Click it again to go back. Right-click (or ⌘-click) the icon to show the menu.