How to see how much free memory is available in Linux

The amount of available memory is equal to the amount of free memory + the amount of cached memory. Keep in mind that RAM disks are stored in the cache and memory used by RAM disk content cannot be reclaimed by the system until that content is deleted from the RAM disk (maybe). The last documentation I read about RAM disks stated that they only grow. They never shrink. This may be different now in newer kernels.

The following command can be used to see how much RAM is available for use:

cat /proc/meminfo | \
awk '{ \
if ($0 ~ /^MemTotal:/) { TotalMemory = $2; } \
if ($0 ~ /^Cached:/) { CachedMemory = $2; } \
if ($0 ~ /^MemFree/) { FreeMemory = $2; } \
} \
END { print \
"Available memory = " (TotalMemory - CachedMemory) / 1024 \
"MB, Free memory = " (FreeMemory + CachedMemory) / 1024 "MB" \
}'

I’m not sure which value in the report is more accurate: “Available memory” or “Free memory”, but I think it’s the latter because it takes miscellaneous allocations into consideration. Here is an example of the output on my workstation, which contains 2GB of system RAM:

user@host:~$ echo $SHELL
/bin/bash

user@host:~$ cat /proc/meminfo | \
> awk '{ \
> if ($0 ~ /^MemTotal:/) { TotalMemory = $2; } \
> if ($0 ~ /^Cached:/) { CachedMemory = $2; } \
> if ($0 ~ /^MemFree/) { FreeMemory = $2; } \
> } \
> END { print \
> "Available memory = " (TotalMemory - CachedMemory) / 1024 \
> "MB, Free Memory = " (FreeMemory + CachedMemory) / 1024 "MB" \
> }'
Available memory = 1560.73MB, Free Memory = 1525.25MB

user@host:~$ cat /proc/meminfo
MemTotal:        2059248 kB
MemFree:         1105060 kB
Buffers:           84752 kB
Cached:           460936 kB
SwapCached:            0 kB
Active:           511048 kB
Inactive:         350320 kB
Active(anon):     315976 kB
Inactive(anon):     3380 kB
Active(file):     195072 kB
Inactive(file):   346940 kB
Unevictable:          16 kB
Mlocked:              16 kB
HighTotal:       1186160 kB
HighFree:         389832 kB
LowTotal:         873088 kB
LowFree:          715228 kB
SwapTotal:       1952764 kB
SwapFree:        1952764 kB
Dirty:               132 kB
Writeback:             0 kB
AnonPages:        315692 kB
Mapped:           103636 kB
Shmem:              3680 kB
Slab:              45576 kB
SReclaimable:      35152 kB
SUnreclaim:        10424 kB
KernelStack:        2304 kB
PageTables:         5476 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     2982388 kB
Committed_AS:    1243316 kB
VmallocTotal:     122880 kB
VmallocUsed:       63084 kB
VmallocChunk:      40444 kB
HardwareCorrupted:     0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       4096 kB
DirectMap4k:       36856 kB
DirectMap4M:      872448 kB

, , , , ,

Leave a comment

VirtualBox trashed my drive

I am constantly amazed at the dichotomy of quality in Free Open Source Software.  Some is amazingly powerful, well tested, and supported.  However, much is not and unfortunately there are times when software you might expect to be very stable and well made, isn’t.  For example, I was using Oracle VirtualBox this past weekend to create a virtual mahcine equivalent of a limited number of physical machines that are used for a very specialized task.  Each physical machine is an exact clone of the others so a virtual machine is a naturally superior solution.  I booted a live Linux CD in the virtual machine and partitioned its disk.  Then I connected a physical hard disk to the VM via a USB ↔ SATA adaptor and mounted the hard disk as read only while running the live CD and proceeded to manually construct the virtual hard disk by porting over the contents of the physical disk.  After porting part of the contents over I decided to test the new VM by rebooting it so I unmounted the VM and physical disk volumes and rebooted.  Later, after testing the VM, I booted the live CD again and attempted to mount the physical hard disk only to find that the partition table was trashed.  This surprised me because there were never any writes to the physical disk and its volumes were mounted read only.  I manually reconstructed the partition table on the physical hard disk and found the the volumes were trashed.  Something went terribly wrong along the way.  I know that most will assume that it was operator error and I must have made a mistake, but I assure you that I was extremely careful to prvent this.  My  USB ↔ SATA adaptor also has no problems I know of.  I’ve been using it for about two years and have never had any problems working with either PATA or SATA drives in both Windows and Linux.  All I can figure is that VirtualBox falls in the not so well developed and/or tested category – but to be fair, I got exactly what I paid for.  Let this be a lesson to all others out there that are banking their busniess on free software.  Be careful and do your due dilligence.  If you’ve got money or jobs on the line then it’s probably best to stick with very old and well tested free software and/or software with commercial backing.  I’ve found that commercially backed software is typically much more polished and bug free because when it’s not the company producing it loses business (or goes out of business), so they’re highly motivated and responsive to paying enterprise level customers. Don’t get me wrong; there are many very responsive FOSS developers out there that are even more responsive than a corporation and who make very polished very high quality software. This post is not intended to imply that FOSS is somehow beneath propritary software – because it is not. Both are simultaneously awesome and flawed.

P.S.  I was able to build the VM using VMware and encountered no problems.  To my surprise, the VMware VM also seems to run quite a bit faster, but this could be caused by the configuraiton or something.  I didn’t spend time analyzing it to find out.

P.P.S.  I got lucky.  Fortunately I had one clone of the physical hard disk that was trashed  so I didn’t lose the machine.  But if I hadn’t had that clone then I would have suffered a severe unrecoverable loss.

1 Comment

Killing boredom

This was originally my response to a fellow redditor who posted a request for ideas about how to kill boredom [with his computer].  It was so well received that I thought it would be a good idea to post it here:

The hardest part of killing boredom is getting the ball rolling. Most people actually do have something(s) they would like to do, but they’re reluctant to get started on a hobby basis or they’re not self starters. Starting a project is often the worst/hardest part of the project. Once it has been started and there is some momentum, it’s relatively easy to figure out what to do next. Identify what you would like do [with your computer] by analyzing your past experiences and current fantasies. Make a list of the intersections. Prioritize that list. Start with the first item on the list and schedule/dedicate some time to get started on that item and force yourself to break through the “getting started” barrier by working on it until you’re either exhausted or have created a solid foundation and can envision some things to do next. Then continue scheduling time and work on the project until it either becomes an ongoing passion or fizzles out because you’ve scratched the itch.

Leave a comment

Press On

Nothing in the world can take the place of persistence.
Talent will not; nothing is more common than unsuccessful men with talent.
Genius will not; unrewarded genius is almost a proverb.
Education will not; the world is full of educated derelicts.
Persistence and determination alone are omnipotent.
The slogan “Press On” has solved and always will solve the problems of the human race.

– Calvin Coolidge

Leave a comment

Ramblings about genuine wealth.

“Most middle-class people resist taking on additional uncertainty. They are more afraid of losing what they have than they are desirous of accumulating more wealth. This is the mindset of the middle class: “Enough is enough.” It is the desire for security. It is the desire to buy insurance against failure.”

— Gary North

He is so right.

The desire for security is driven by the fear of loosing what one has.  Loosing what one has means reacquiring it, which means re-suffering the cost of the acquisition, which means putting forth time and effort (i.e. doing work).  However, it’s likely that those same things would not be re-acquired because the looser would naturally deviate from his previous course in search of different things – acquiring only the small/important fraction of what he once had.  (I’ve always done this, even when I thought I wouldn’t.  I’ve discovered about myself that I never go back.)

Fearing the cost and perceived pain of of reacquiring something, and therefore avoiding all risk that might cause its loss, is driven primarily by laziness.  It’s a vicious cycle that culminates in a total waste of human potential.  If certain (most) people were rich so that they did not have to work to survive and have the possessions they desire then those same people would naturally ultimately become complete wastes of life on earth – squandering all opportunities to grow.

The solution is to learn and accept that the purpose of life is to “play the game”, not to win.  Fortunately the game is incomprehensibly large and complex, with enough detail to support the needs of each individual player, which allows each player to engage in that which has meaning and value to them and avoid that which does not.

The most damning force that can be applied to nearly all persons on earth is financial “wealth” and “security”, which drives them to be idle and passive.  Only the most driven and restless individuals are qualified to manage such a hazardous thing as financial material wealth.

Ask yourself this question: If I were [financially] rich, what would I do?  This, of course, assumes you’re not rich.  If your answer is not an honest “nothing different” then you’re not living well.  You’re living your life in vain without meaning, for other people, or for ideals that are not your own.  If the answer is not “work incessantly to achieve all of my goals without wasting energy on basic survival” then you’re a waste of skin.  All that remains is to squander all the precious educational opportunities and experiences this world is designed to provide.  Why are you even here if you would do that?!  You don’t remember it now, but what got you here is the exact opposite of that kind of thinking.

“The central issue of wealth is this: “What is my expected net value for my remaining heartbeats, and why?””

— Gary North

Simply brilliant.

, , , ,

Leave a comment

How I solved my nVidia driver kernel module API mismatch problem.

I recently ran into a problem with my proprietary nvidia video driver on Ubuntu Linux 9.04.  It seems that there was a conflict between the driver I downloaded directly from http://www.nvidia.com/Download/index.aspx?lang=en-us and the driver installed by Ubuntu’s restricted driver manger (found on the gnome System menu under Administration | Hardware Drivers).  The problem was that the kernel module API installed by Ubuntu did not match the driver installed by the installer I downloaded and the end result was that my screen was completely black/blank and no tty’s worked so pressing Ctrl+Alt+F[1-6] wouldn’t take me to a terminal.  (Executing dmesg at a command prompt would show messages indicating that there was an API mismatch.  This message would also be shown by running “startx” at the command prompt.)

Simply pressing the power button once was sufficient to get Ubuntu to gracefully shutdown the machine, thankfully.  After powering back up I selected the recovery item on the grub boot menu to boot into a root console.  If you don’t have a recovery menu item then add “single” (without the quotes) to the kernel command line in the grub menu and boot from there.  From the root console I did three things: I uninstalled the nvidia drivers that I manually installed earlier (that broke my system), I unloaded the nvidia kernel module, and then I manually searched for and removed all nvidia kernel modules.

Uninstalling the nvidia drivers I installed manually was easy.  All I had to do was run “nvidia-installer --uninstall”.  I got a waring because I was running in single user mode so I had to tell the installer to proceed anyway and it worked fine.  You will also get an error if X is running.  To terminate X you need to kill gdm by executing “/etc/init.d/gdm stop” as root, however it should not be running if you booted into the recovery console.

Unloading the nvidia kernel module was easy.  Simply execute “rmmod nvidia” as root.

Searching for and removing all nvidia kernel modules was tedious, but also easy.  This procedure is not the ideal solution, but I had no working video or networking and had to take relatively drastic measures.  First I had to find all the drivers.  I did this by executing “find / -type f 2> /dev/null | grep -i nvidia | grep -i \\.ko” as root.  This generated output similar to this:

/var/lib/dkms/nvidia/173.14.16/2.6.28-13-generic/i686/module/nvidia.ko
/var/lib/dkms/nvidia/173.14.16/build/nvidia.ko
/var/lib/dkms/nvidia/173.14.16/build/.nvidia.ko.cmd
...
/var/lib/dkms/nvidia/original_module/2.6.28-13-generic/i686/collisions/kernel/drivers/video/nvidia.ko
/var/lib/dkms/nvidia/original_module/2.6.28-13-generic/i686/nvidia.ko
...
/lib/modules/2.6.28-13-generic/kernel/drivers/video/nvidia.ko

Then I manually removed each kernel module by using the “rm -f <pathname_of_file>” command.

Finally I reran the nvidia installer, did a full install of the video driver, and then rebooted the machine by executing the “reboot” command as root.  When the machine finished booting I had video back and all was well in my world again.

, , , , , , , ,

7 Comments