IMHO the percentage of the free memory on a linux system is NOT a good indicator in this situation because of the way linux memory management works (memory is also used for buffering and caching, there's also swapping, etc) - plenty of info out there on the subject. Which means using such indicator would cause cleaning the cache earlier than actually necessary which I imagine comes with a price in overall performance.
I found that the percentage of swap used is usually a much better indication of the system being short on memory while taking all these into account: as long as it can still allocate memory it tipically won't use the swap.
As a bonus this doubles as a performance indicator as well - a system using over 4G of swap for example is usually barely crawling (think how much it takes to copy a 4G disk file from one place to another).
The swap info be found in /proc/meminfo, so the approach that chaos suggested can be adapted accordingly:
SwapTotal: 16777212 kB
SwapFree: 16777212 kB
You may need to experiment a bit to find out which swap usage percentage value is better fit for your particular case.