Welcome to LinuxQuestions.org, a friendly and active Linux Community.You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features.
Registration is quick, simple and absolutely free. Today!Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.Are you new to LinuxQuestions.org? Visit the following links: If you have any problems with the registration process or your account login, please. If you need to reset your password,.Having a problem logging in?
Please visit to clear all LQ-related cookies. Introduction to Linux - A Hands on GuideThis guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.to receive this Complete Guide absolutely free.
BackgroundLinux implements a demand-paged virtual memory system.Processes have a large (4 gigabyte) virtual memory space. Asvirtual memory is referenced, the appropriate pages are transferredbetween disk and physical memory.When there are no more physical memory pages available, thekernel swaps some older pages back to disk. (If they are code pagesthat have not been changed, then they are just discarded; otherwisethey are written to the swap areas.)Disk drives are mechanical devices; reading and writing todisk is several orders of magnitude slower than accessing physicalmemory. If the total memory pages required significantly exceed thephysical memory available, the kernel starts spending more timeswapping pages than executing code. The system begins thrashing,and slows down to a crawl.
What's going on? Linux is borrowing unused memory for disk caching. This makes it looks like you are low on memory, but you are not! Everything is fine! How to Calculate Memory Usage on Linux (Doc ID 1630754.1) Last updated on OCTOBER 28, 2018. Applies to: Linux OS - Version Oracle Linux 4.4 and later Oracle Cloud Infrastructure - Version N/A and later Linux x86 Linux x86-64 Linux Itanium Goal. This document describes how to calculate Linux memory usage.
If this increases to a point where theswap device becomes fully utilized, the system can virtually cometo a standstill. This is definitely a situation we want toavoid.When extra physical memory is not in use, the kernel attemptsto put it to work as a disk buffer cache. The disk buffer storesrecently accessed disk data in memory; if the same data is neededagain it can be quickly retrieved from the cache, improvingperformance. The buffer grows and shrinks dynamically to use thememory available, although priority is given to using the memoryfor paging. Thus, all the memory you have is put to gooduse.
Tools for Measuring Memory UtilizationIn order to know what your memory situation is and whetherany changes you make are resulting in improvement, you need to havesome way of measuring memory usage. What tools do we have at ourdisposal?When the system first boots, the ROM BIOS typically performsa memory test. You can use this to identify how much physicalmemory is installed (and working) in your system, if you don't knowalready. On my system, it looks something like this:ROM BIOS (C) KB OK WAIT.The next piece of useful information is displayed during theLinux boot process. Output such as the following should bedisplayed:Memory: 7100k/8192k available (464kkernel code, 384k reserved, 244k data).Adding Swap: 19464k swap-spaceThis shows the amount of RAM available after the kernel hasbeen loaded into memory (in this case 7100K out of the original8192K).
You can also see if the swap space has been properlyenabled. If the kernel bootup messages scroll by too quickly toread, on many systems you can recall them at a later time using the“dmesg” command.Once Linux is running, the “free” command is useful forshowing the total memory available (which should match that shownduring boot-up), as well as a breakdown showing the amount ofmemory being used, and the amount free.
(If you don't have a “free”command, you can use “cat /proc/meminfo”.) Both physical memory andswap space is shown. Here is a typical output on my system. Here is a typical output on my system:totalusedfreesharedbuffersMem:802328Swap:42800The information is shown in kilobytes (1024 bytes). The“total” memory is the amount available after loading the kernel.Any memory being used for processes or disk buffering is listed as“used.” Memory that is currently unused is listed in the “free”column. Note that the total memory is equal to the sum of the“used” and “free” columns.The memory indicated as “shared” is an indication of how muchmemory is common to more than one process. A program such as theshell typically has more than one instance running.
The executablecode is read-only and can be shared by all processes running theshell.The “buffers” entry indicates how much of the memory in useis currently being used for disk buffering.The “free” command also shows very clearly whether the swapspace is enabled, and how much swapping is going on.To better understand how the kernel uses memory, it isinstructive to watch the output of the “free” command as the systemis used. I'll show some examples taken from my own system; Isuggest you try similar experimentation yourself.On bootup, with one user logged in, my system reports thefollowing:total used free shared buffersMem: 7096 2672 4424 1388 1136Swap: 19464 0 19464Note that we have considerable free memory (4.4MB) and arelatively small disk buffer (1.1MB). Now watch how the situationchanges after running a command that reads data from the disk.
(Inthis case I typed ls -lR /.)total used free shared buffersMem: 7096 5104 1992 1396 3460Swap: 19464 0 19464We see that the disk buffer has grown by over 2 MB. Thisbrings the “used” memory up correspondingly, and the free memorydown. Next, I start up the X Window system and examine theresults:total used free shared buffersMem: 7096 70 3792Swap: 19464 8 19456This has caused the memory used to increase to 7MB, leavingonly 80K free. The increase is to support the additional processesrunning (the X server, window manager, xterm, etc.).
Note thatthe disk buffer didn't shrink, because there is still free memory.Remember: “free” memory means memory that is being wasted.Now I start up the GNU chess program, having it play againstitself. This starts two instances of a rather large program:total used free shared buffersMem: 7096 70 860Swap: 14436We see now that the disk buffer has shrunk down to less than1MB and we are 5MB into swap to accommodate the large processes.Because of the swapping, the system has slowed down, and heavy diskdrive activity can be heard. There is still a small amount of freememory. Increasing Available MemoryNow that we have some measurement tools at our disposal, itstime to try to improve the memory situation. The first line ofattack is before Linux boots—your ROM BIOS setup program has someoptions that may increase the amount of memory available. Manysystems can shadow the ROM address ranges in RAM, because it isfaster than ROM.
Unlike MS-DOS, however, Linux doesn't use the ROMBIOS routines, so disabling this can free close to 200K of memory(if you still run MS-DOS occasionally then you may not want to dothis).Incidently, now is also a good time to look at your othersetup options and do some experimentation. You may be able toimprove CPU performance with the options to enable caching andsetting the CPU clock speed. One way to measure this is to use theBogoMIPs rating displayed when Linux boots as an indicator of CPUspeed (this is not always accurate though, because as everyoneknows, BogoMIPs are “bogus”). If you boot Linux from a hard disk,you may also be able to speed up reboot times by disabling thefloppy disk drive seek at bootup. Don't change too many settings atonce, or you may not know which changes are having a positiveeffect. Be sure to write down your original settings in case youput your system in a state where it will no longer boot. Recompiling the KernelAre you still using the default kernel that came when youinstalled Linux?
If so, shame on you! Kernel memory isspecial—unlike the memory pages used by processes, the kernel isnever swapped out. If you can reduce the size of the kernel, youfree up memory that can be be used for executing user programs (notto mention reducing kernel compile times and disk storage).The idea here is to recompile the kernel with only theoptions and device drivers you need. The kernels shipped with Linuxdistributions typically have every possible driver and file systemcompiled in so that any system can boot from it. If you don't havenetwork cards, CD-ROM, SCSI, and so on, you can save considerablememory by removing them from the kernel.
Besides, you can't reallyconsider yourself a Linux hacker if you've never recompiled acustomized kernel yourself.If there are drivers that you only need occasionally,consider building several kernels, and set up LILO to let youchoose an alternate kernel when booting. If you have a mathcoprocessor, you can consider taking out the FPU emulation routinesas well. You can also remove any of the Linux file systems that youdo not require.More advanced Linux hackers might want to look at the“modules” facility which allows for loadable device drivers. Withthis you can dynamically add and remove drivers without rebooting.This facility has been available for some time to kernel hackers,and it has now become a part of the standard kernel. This facilityis particularly useful for rarely used devices such as tape drivesthat are only occasionally used for backup purposes.Finally, make sure you are running a recent kernel.
Newerkernels, as well as (in most cases) being more stable, also haveimprovements in memory usage. Compiling ApplicationsIf you develop your own applications, or compile code youobtain from the Internet or bulletin board systems, then using theright compile options can reduce the memory used. Turning onoptimization will generally produce code which is smaller andexecutes faster, as well as requiring less memory. A fewoptimizations, such as in-line functions, can make the code larger.You should also check that your executables are dynamically linkedand stripped of debug information.Which optimizations are best depend on the specificapplication and even on the version of compiler used; you may wishto experiment.init (this starts all other processes).update (this periodically writes the disk buffersto disk).a single getty (which becomes your shell whenlogged in)Run “top” and see what is running on your system.
How manygetty processes do you need? Do you really need all those otherprocesses such as lpd, syslogk, syslogd, crond, and selection? On astandalone system, you don't need to run full networkingsoftware.If you are using an init package that supports multiple runlevels, you might want to consider defining several different runlevels. This way you could, for example, switch your system betweenfull networking and running standalone, allowing you to free upresources when you don't need them.You can also examine some of your larger executables to seeif they were built with the appropriate compiler and linkeroptions. To identify the largest programs, try using a command suchas this:ls -s1 /bin /usr/bin /usr/bin/X11 sort -n tailStrictly speaking this only finds the largest files, but filesize is usually a good indication of the memory requirements of aprogram.The most common shell under Linux is GNU BASH.
While veryfunctional, it is also quite large. You can save memory by using asmaller shell such as the Korn shell (usually called ksh orpdksh).The emacs editor is also big; you could use a smaller editorsuch as vi, jove, or even ed instead. The X Window SystemIf you ran the command line described earlier, one of yourlargest binaries was probably the X server. The X Window systemtakes a lot of memory resources.The first question to consider is, do you really need to runX? Using the virtual consoles and selection service you can havemultiple windows supporting cut & paste of text using a mouse.Particularly while performing large compiles (such as the kernel),you should consider the option of simply not running X.There is also a windowing system called “mgr” than can beused as an alternative to X, but requires less memory.If you decide to use X, then you can obtain replacements forsome of the standard tools that require less resources. “Rxvt” issimilar to xterm, but requires significantly less memory. Thewindow manager “fvwm” will also use less resources than others, and“rclock” is a small X-based clock program.
These three tools,written by Robert Nation, can make running X feasible on a machinethat constantly swapped before.How many programs do you run on the X desktop? Run “top” tosee how much memory is being taken by xclock, xeyes, xload, and allthose other goodies you think you need.The “Tiny X” package, put together by Craig I. Hagan,contains the Korn shell, fvwm window manager, rxvt, rclock, Xserver, and the minimum of other files needed to run X. The packageis small enough to fit on one 3.5' floppy disk. Also included aresome useful notes on saving memory under X.With the techniques described here, you can run small Xapplications reasonably well on a machine with only 4 megabytes ofmemory. On machines with more memory, the same methods will allowyou to run larger applications and free up memory to use for diskbuffering. For More InformationThe software mentioned in this article is available on anumber of Internet archive sites, including sunsite.unc.edu andtsx-11.mit.edu.
I suggest getting a copy of the Linux Software Mapto help track down the software you need.If you want to learn more about how the Linux kernelimplements memory management, check out ”The Linux Kernel Hackers'Guide“, by Michael K. Johnson, part of the Linux documentationproject. Appendix A of that document includes an extensivebibliography of books covering operating system concepts ingeneral.”How to Maximize the Performance of X' is periodically postedto the Usenet newsgroup news.answers, and contains more ideas forimproving X performance on small systems.