The Rules
  • Feel free to leave constructive criticism, or point out a better way to do something.
  • Personal attacks or flames, on me or anyone else, will be deleted.
  • Past history has shown that 99% of comments I can't read (i.e. those in other languages) to be spam. Therefore, any comment I can't read will be removed.
  • I'm pretty mellow concerning profanity, but excessive (as determined subjectively by me), bad language will be removed.

Thursday, August 30, 2007

Objective-C Syntax in gVim

For those who know me, it's no secret that I'm a Vim man. You know how some people are fish out of water without Visual Studio or (insert IDE here)? I'm a fish out of water without my .vimrc. Thanks to Vim's superior editing capabilities, I can get work done on any computer I need to as long as it at least has a basic Vim installation on it, but when I can use my own macros and settings, let the good times roll.

The other day I decided that since I'm also a Mac man, I ought to start wrapping my head around Objective-C. So I got a book and started in. I quickly discovered that for some reason, Vim wasn't detecting the .m filetype extension for my Objective-C source files. So I did some digging around through the Vim documentation and found the following:

You need in your home directory (or whatever your $VIMRUNTIME directory is) a directory named ".vim". Inside this directory you'll need to make a file called "filetype.vim". Paste the following into that file:







"Check to see if the filetype was automagically identified by Vim

if exists("did_load_filetypes")

finish

else

augroup filetypedetect

au! BufRead,BufNewFile *.m setfiletype objc

augroup END
endif



What happens, or at least what happened to me, is that for some reason Vim didn't automagically detect the ".m", or it was conflicting with another filetype for some reason. What the above snippet of vimscript does is ask Vim if it loaded a filetype. In my case, it hadn't. So it skips the "finish" command and moves to the "augroup" section, checking the file extension you gave the file (in this case ".m" and says, "oh hey, that's an Objective-C file", and then sets the filetype as such.

Now I know that some of you may start yelling at me telling me that this is already there on Vim's documentation, blah blah blah. I posted this as a convenience for those who don't want to spend hours digging for it.

Monday, March 5, 2007

Is my Mac using too much memory???

Edit/Update: Modern releases of OS X (Mountain Lion and particularly Mavericks) treat memory much differently. I'm leaving this post as-is for historical reasons, but if you're running Mavericks you'll see things pretty differently.

I have been a linux user for a few years, and I recently (6 months ago) switched to a MacBook. One of the reasons I originally switched from Windows to Linux was resource efficiency. There were others, but this is the one that leads to this discussion. A couple of weeks after I got my MacBook, I was working one day on a programming project for school with nothing open but iTerm and a couple of gvim windows. I happened to look at a resource monitor widget I had in my Dashboard, and was immediately concerned that I was only showing around 100 Mb free RAM. Concerned that I had some kind of memory leak going on I started digging around to find out where the "bug" was. It turns out that the bug was in my understanding of how OSX uses--and labels--memory. A friend of mine (Quinn) helped illuminate some of this for me. So here it is.

If you open the Activity Monitor (found in utilities) and select the "System Memory" tab at the bottom, you'll see something that looks roughly like this:
OSX labels physical memory (RAM) as follows: Wired, Active, Inactive, and Free.

Wired memory is used by the OS and is pretty much untouchable. Another application can't "borrow" wired memory.

Active memory is what is currently in use by running applications. Note that thanks to the splendors of virtual memory, all of the memory needed by an application isn't necessarily contained here. If you look at a running process in the Activity Monitor list, you'll see a column for Real and a column for Virtual Memory. Since we are talking about the amount of RAM in use, we won't worry about virtual memory for the moment. If there is no inactive, or free memory, active memory can be used by other applications, but this causes the OS to write the current state of the active memory being traded to its owner's virtual memory pages on disk before granting the memory to another application.

Inactive memory is memory that has recently been used by an application that is no longer running. OSX keeps track of what this is and what it belonged to because of the idea of temporal locality, the idea being that if you opened an application you are somewhat likely to do so again and if the memory is still labeled, the application can start very quickly. In the absence of sufficient free memory, inactive memory will be reclaimed by another running application that needs memory.

Free memory is just that, free. Nothing has a claim on it, and it's up for grabs for any application that needs it.

As you can see from my screen-shot image above, I tend to run with around 100-200 Mb free, between 150 and 300 Mb wired, and the rest split between active and inactive. What does this memory labeling have to do with how fast or slow the machine runs? Simple. When you log in, OSX claims the memory it needs to do all of its chores. This is wired. Other applications claim a smallish chunk of active memory as they are opened. Most applications that need to keep track of any kind of history or user data gradually use up more memory the longer they are open. Every now and then an application will need to use some part of its memory that it doesn't use frequently and OSX has to get this from disk and place it in that application's active memory allocation. All of these things can slow down a system. Also, virtualization applications such as Parallels or VMWare's Fusion require a large chunk of memory for the virtual machine that gets marked as wired.

The main thing you want to be aware of is not necessarily how much memory you have marked as free, but how much wired, active, and inactive memory you have and how often OSX has to swap memory pages in from or out to disk (Page ins/outs in the Activity Monitor). For example, my MacBook (which has 1 Gb of RAM) is almost always very responsive, except when I'm running anything in a virtual machine. When I start up Windows (which happens as rarely as possible), Parallels grabs its virtual machine memory and my usage graph looks like this:
You'll notice that wired has more than doubled in size, there is significantly less inactive memory, and next to none free. Page ins/outs are ok. "Ins" has a pretty high count from a human standpoint, but it's no big deal. "Outs" is still pretty low and my system is still fairly responsive. However, if I then try to run lots of other applications, I run into problems.Just opening Firefox caused the "Outs" to nearly double almost instantly. Inactive memory has gone down even more, and my system is noticeably less responsive. Continuing on this trend leads to a condition where "Ins" and "Outs" are nearly equal, and climbing fast. When this happens, OSX is spending all its time shuttling memory in and out of the virtual memory pages on disk, and doesn't have time to do much else. Similarly, your running applications are all waiting on OSX to swap memory to and from disk, and take much longer to do what you want them to do. This is bad. Closing the memory hogs in a condition like this will lead to almost instantaneous relief.

In this case, you'll notice that closing Parallels immediately caused a huge leap in free memory, and a marked decrease in active. This is sort of an isolated case in that the memory that was previously wired for the virtual machine is now free. In a normal case, closing applications like Photoshop, Office programs, etc. will cause a slower increase in free memory and a more immediate increase in inactive memory, with an additional decrease to active. It will take a little while for your system to get "balanced" again, and the more applications you close, the faster this will happen. You'll notice that in the time it took me to shut down my windows system running in Parallels, close Parallels, and take another screen-shot that page "Outs" increased by 9 times, and by proportion were rapidly gaining on page "Ins".

The moral of the story is: If you happen to look at Activity Monitor or some other system resource monitor and notice that you only have about 10-15% of RAM free, don't worry about it unless your system is noticeably unresponsive. This is not to say that some third party applications don't have memory leaks, but that's a topic for another day.