# Linux Memory Management or 'Why is there no free RAM?'

## sapphirecat

Linux Memory Management or 'Why is there no free RAM?'

Revision 2.3

Copyright 2004 sapphirecat. The text of this post is licensed under a Creative Commons License.

Sections

Overview of memory management

The mysterious 880 MB limit on x86

The difference among VIRT, RES, and SHR in top output

The difference between buffers and cache

Swappiness (2.6 kernels)

1. Overview of memory management

Traditional Unix tools like 'top' often report a surprisingly small amount of free memory after a system has been running for a while. For instance, after about 3 hours of uptime, the machine I'm writing this on reports under 60 MB of free memory, even though I have 512 MB of RAM on the system. Where does it all go?

The biggest place it's being used is in the disk cache, which is currently over 290 MB. This is reported by top as "cached". Cached memory is essentially free, in that it can be replaced quickly if a running (or newly starting) program needs the memory.

The reason Linux uses so much memory for disk cache is because the RAM is wasted if it isn't used. Keeping the cache means that if something needs the same data again, there's a good chance it will still be in the cache in memory. Fetching the information from there is around 1,000 times quicker than getting it from the hard disk. If it's not found in the cache, the hard disk needs to be read anyway, but in that case nothing has been lost in time.

To see a better estimation of how much memory is really free for applications to use, run the command:

```
free -m
```

The -m option stands for megabytes, and the output will look something like this:

```
             total       used       free     shared    buffers     cached

Mem:           503        451         52          0         14        293

-/+ buffers/cache:        143        360

Swap:         1027          0       1027
```

The -/+ buffers/cache line shows how much memory is used and free from the perspective of the applications. Generally speaking, if little swap is being used, memory usage isn't impacting performance at all.

Notice that I have 512 MB of memory in my machine, but only 503 is listed as available by free. This is mainly because the kernel can't be swapped out, so the memory it occupies could never be freed. There may also be regions of memory reserved for/by the hardware for other purposes as well, depending on the system architecture.

2. The mysterious 880 MB limit on x86

By default, the Linux kernel runs in and manages only low memory. This makes managing the page tables slightly easier, which in turn makes memory accesses slightly faster. The downside is that it can't use all of the memory once the amount of total RAM reaches the neighborhood of 880 MB. This has historically not been a problem, especially for desktop machines.

To be able to use all the RAM on a 1GB machine or better, the kernel needs recompiled. Go into 'make menuconfig' (or whichever config is preferred) and set the following option:

```
Processor Type and Features ---->

High Memory Support ---->

(X) 4GB
```

This applies both to 2.4 and 2.6 kernels. Turning on high memory support theoretically slows down accesses slightly, but according to Joseph_sys and log, there is no practical difference.

3. The difference among VIRT, RES, and SHR in top output

VIRT stands for the virtual size of a process, which is the sum of memory it is actually using, memory it has mapped into itself (for instance the video card's RAM for the X server), files on disk that have been mapped into it (most notably shared libraries), and memory shared with other processes. VIRT represents how much memory the program is able to access at the present moment.

RES stands for the resident size, which is an accurate representation of how much actual physical memory a process is consuming. (This also corresponds directly to the %MEM column.) This will virtually always be less than the VIRT size, since most programs depend on the C library.

SHR indicates how much of the VIRT size is actually sharable (memory or libraries). In the case of libraries, it does not necessarily mean that the entire library is resident. For example, if a program only uses a few functions in a library, the whole library is mapped and will be counted in VIRT and SHR, but only the parts of the library file containing the functions being used will actually be loaded in and be counted under RES.

4. The difference between buffers and cache

Buffers are associated with a specific block device, and cover caching of filesystem metadata as well as tracking in-flight pages. The cache only contains parked file data. That is, the buffers remember what's in directories, what file permissions are, and keep track of what memory is being written from or read to for a particular block device. The cache only contains the contents of the files themselves.

Corrections and additions to this section welcome; I've done a bit of guesswork based on tracing how /proc/meminfo is produced to arrive at these conclusions.

5. Swappiness (2.6 kernels)

Since 2.6, there has been a way to tune how much Linux favors swapping out to disk compared to shrinking the caches when memory gets full.

ghoti adds:

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.

Before the 2.6 kernels, the user had no possible means to influence the calculations and there could happen situations where the kernel often made the wrong choice, leading to thrashing and slow performance. The addition of swappiness in 2.6 changes this.

Thanks, ghoti!

Swappiness takes a value between 0 and 100 to change the balance between swapping applications and freeing cache. At 100, the kernel will always prefer to find inactive pages and swap them out; in other cases, whether a swapout occurs depends on how much application memory is in use and how poorly the cache is doing at finding and releasing inactive items.

The default swappiness is 60. A value of 0 gives something close to the old behavior where applications that wanted memory could shrink the cache to a tiny fraction of RAM. For laptops which would prefer to let their disk spin down, a value of 20 or less is recommended.

As a sysctl, the swappiness can be set at runtime with either of the following commands:

```
# sysctl -w vm.swappiness=30

# echo 30 >/proc/sys/vm/swappiness
```

The default when Gentoo boots can also be set in /etc/sysctl.conf:

```
# Control how much the kernel should favor swapping out applications (0-100)

vm.swappiness = 30
```

Some patchsets allow the kernel to auto-tune the swappiness level as it sees fit; they may not keep a user-set value.

...........................................................................................................

I promised to write about this as soon as I figured out the last thing I was curious about myself. If there are other topics I should cover, please send me a PM.

----------

## andrewy

Thanks, I'm sure this will help many people.

Why Linux seemed to use so much memory is one thing that I never understood when I first started using Linux, it took a long time for me to learn why, so I'm glad someone has taken the trouble to explain it for the people that might be newer to Linux.

----------

## beastmaster

Does that mean that I should get a 1 GB+ DRAM just to take advantage of linux 2.6's memory management system?

I'm currently having 512  :Rolling Eyes: , and by looking at the "free", there are only 77 free mem left  :Shocked: 

```

                    total       used       free     shared    buffers     cached

Mem:             502        425         77          0         10        210

-/+ buffers/cache:        204        297

Swap:          1153          0       1153

```

So, by getting a 1 GB+ I can enjoy even some better locality performance?

----------

## stahlsau

mmh..as i understood, you have too much memory ´cause 70mb are free  :Wink: 

----------

## beastmaster

 *Quote:*   

> 
> 
> mmh..as i understood, you have too much memory ´cause 70mb are free 

 

 :Question:   :Shocked:  , I think 512MB RAM is not linux thirsty enough...

----------

## grzewho

 :Idea:   :Idea:  640kb ought to be enough for everyone   :Idea:   :Idea: 

----------

## sapphirecat

 *beastmaster wrote:*   

> Does that mean that I should get a 1 GB+ DRAM just to take advantage of linux 2.6's memory management system?
> 
> I'm currently having 512 , and by looking at the "free", there are only 77 free mem left 
> 
> ```
> ...

 

Probably not; the cache has a quickly diminishing performance boost. The only reason Linux lets it get so big is because the memory is available and a marginal gain is still a gain.

Besides, if you look at the buffers/cache line, over half your memory is still truly available.  :Smile: 

----------

## beastmaster

 :Smile: 

----------

## dalek

I'm bookmarking this thread.  I am in a lot of forums and people freak out because Linux really USES the memory to make the system faster.  

Finally a guru who explained it.  Would have myself but nobody would listen anyway.    :Shocked:   :Shocked: 

Later

 :Very Happy:   :Very Happy:   :Very Happy:   :Very Happy: 

----------

## neenee

hm.. perhaps this can be made a sticky or something - since

many new users ask the question about why gentoo/linux/etc

'uses up' most of their ram.

----------

## Souperman

Quite informative, thanks.  I figured out how to read meminfo a while ago, but something that I didn't know (not that I really researched it, to be honest   :Wink: ) is what exactly buffers and cache are.

----------

## gentoofailure

Great post this is what I was just looking for and has put my mind at ease

----------

## DooMi

very nice explenation. 

thanks  :Smile: 

----------

## dalek

I need sound.  How do you do a whistle on this thing?  Love that avatar, DooMi.    :Laughing:   :Laughing: 

Later

 :Very Happy:   :Very Happy:   :Very Happy:   :Very Happy: 

edit:  I need to learn to type better.    :Rolling Eyes:   :Confused:   :Shocked: 

----------

## ghoti

 *sapphirecat wrote:*   

> Linux Memory Management or 'Why is there no free RAM?'
> 
> Revision 2

 

Excellent ! 

It is indeed a FAQ, even in french, so I decided to translate your text and post it  on the french forum .

I only hope not to have betrayed your mind !  :Wink: 

----------

## sapphirecat

 *ghoti wrote:*   

> Excellent ! 
> 
> It is indeed a FAQ, even in french, so I decided to translate your text and post it  on the french forum .
> 
> I only hope not to have betrayed your mind ! 

 

Great!

I can't read French, but that didn't stop me from trying.  :Wink:  AFAICT, Leander256 expanded on the swappiness section? I would appreciate having that in English to merge in here. Feel free to PM me about that.

----------

## ghoti

 *sapphirecat wrote:*   

>  AFAICT, Leander256 expanded on the swappiness section? I would appreciate having that in English to merge in here. Feel free to PM me about that.

 

In fact, Leander256 was afraid a french speaking n00b could not understand the english word "swappiness", nor my poor translation of the word. So I've added a paragraph to explain the concepts even more. 

I've tried to translate it in english but please be lenient and feel free to adapt this to a less "frenchie english"    :Embarassed: 

 *Quote:*   

> When an application needs memory and all the RAM is fully occupied, the kernel has two "relief tanks" at 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.
> ...

 

----------

## sapphirecat

 *ghoti wrote:*   

> I've tried to translate it in english but please be lenient and feel free to adapt this to a less "frenchie english"   

 

Alright, revision 2.1 done. I hope you don't mind me taking the liberty of condensing it quite a bit.

----------

## revertex

Hi, i have 512Mb ram, and i'm trying to use every mega as possible, but changing the swappiness priority seems does't work well in gentoo.

I'm trying change  

```
/proc/sys/vm/swappiness
```

 to zero to prevent things to be swapped to disk but ever time i reboot it comes to 83.

I had set swap  priority in fstab to zero and edited 

/etc/sysctl.conf. if it matters.

http://kerneltrap.org/node/view/3000

----------

## beastmaster

hi revertex,

After a semester of "machine arch" course, 

I remember my prof said about it's good to have big size L1, L2, and L3 caches, just so it can greatly redude cache misses (capacity misses in this case)...   :Surprised: 

The idea is that not needing to touch the swap space as much as possible (since it's on-disk, so the move to transfering data into ram is like... I think it's said 100x ~1000x slower), since kernel 2.6 will make fully use of the RAMs.

so in your case (maybe a lot of mem intensive programs hogging), if you are running out of physical mem, just buy another 512 MB RAM, and once again every bite is worth the money  :Wink: 

edit: I may be wrong, so correct me if there is   :Very Happy: 

----------

## sapphirecat

 *revertex wrote:*   

> I'm trying change  
> 
> ```
> /proc/sys/vm/swappiness
> ```
> ...

 

Is it one of the kernels which auto-tunes swappiness? Does the value change after you run it a while (say, while emerging something)?

----------

## revertex

sory for delay, now my swap file is untouched!

my box stay long time whitout reboot, maybe i forgot it  :Embarassed: 

thanks all for reply, i just noted that my emerge sync now take ages, dunno why.

my kernel is gentoo-dev-sources.Last edited by revertex on Mon Jun 21, 2004 11:23 pm; edited 1 time in total

----------

## dalek

I went to sleep waiting on mine to sync up yesterday.  I did notice that portage had a update.  That may have had something to do with it.

I though it would never finish.    :Shocked:   :Shocked: 

Later

 :Very Happy:   :Very Happy:   :Very Happy:   :Very Happy: 

----------

## revertex

It's happen again!

I'm  working with some svg files, not so big, then my machine start  to use swap, after this  swappiness goes to 94!

```
cat /proc/sys/vm/swappiness           94

```

i will take a closer look in my kernel config later, thanks about the auto-tunes swappiness info.

beastmaster, i'm running fluxbox with some "diet" apps, 512 isn't enough?   :Shocked:   :Shocked: 

therefore another 512 is a matter of time (read money)

----------

## Evangelion

 *sapphirecat wrote:*   

> 2. The mysterious 800 MB limit on x86

 

related to this: do you need to enable high memory-support on AMD64-machines to take advantage of lots of RAM?

----------

## imsdunn

I am having problems as well with very slow performance. I thought it was my internet provider at first, but have come to realize that everything takes a while to open and load.

I tried the free -m command and it appears I have plenty of RAM available

```
 free -m

             total       used       free     shared    buffers     cached

Mem:           440        212        227          0         21        104

-/+ buffers/cache:         86        353

Swap:          494          0        494

```

I am still using a 2.4 kernel with gentoo and kde. It just slowed down this weekend. I have rebooted both the computer and modem several times trying to free it up. Any suggestions? 

 *Quote:*   

> I went to sleep waiting on mine to sync up yesterday.

 

I am also having to wait a terribly long time for sync to run. I emerged sync once each of the last two days.

----------

## beastmaster

I think there are really a lot of possibilites for slow performance   :Rolling Eyes: 

1. internet clustered

2. cpu getting overheated, but if you just have a cold start, and it's still slow on loading, then it's not it.

3. kernel 2.4, if you want to have some memory advantage like the original author posted ealier, you should give kernel 2.6 a try.

4. harddisk DMA transfer is not turned on, or something, hdparm would help about 20%~30%

5. small L1, L2 cache, so have to live with it (like my Intel celeron's on my notebook)

6. the famous optimization flag tweaking  :Very Happy: , it will boost up performance for 30~40% with good and right CFLAGS (for those debian lovers who are lurking it, I dare you that, this is the fact, so don't argue)

and there are also so many other factors that cause a slow performance.

I'm no expert, so I'm out of idea now   :Razz: 

but I would look into kernel, cflags, and harddisk hdparm first.

----------

## nadamsieee

Don't forget this fix for Gnome/KDE performance:

Edit your /etc/hosts file such that 127.0.0.1 points to localhost.localdomain, localhost, and your actual hostname. So for me, its like this:

```
# cat /etc/hostname

alqualonde

# cat /etc/hosts

127.0.0.1       localhost.localdomain localhost

127.0.0.1       alqualonde
```

----------

## smiley

 *Evangelion wrote:*   

>  *sapphirecat wrote:*   2. The mysterious 800 MB limit on x86 
> 
> related to this: do you need to enable high memory-support on AMD64-machines to take advantage of lots of RAM?

 

My understanding is that on 64bit processors, all memory is "low memory," so you wouldn't need high memory support. On the other hand, since amd64 also runs 32bit programs, maybe it's different than other 64bit processors.

smiley  :Cool: 

----------

## flickerfly

A neat followup article on this might be a tweaking article including swappiness settings, hdparm, and all those other little things that can slow down Gentoo simply because they aren't set optimally. That might ought to even be official documentation tacked on the end of the Gentoo Handbook.

----------

## RAPUL

I already knew most of this thread says but it is nice someone wrote all these ideas joint for people who don't know them.

I also discovered swapiness concept. So as everyday i learnt a new thing today.

I discovered these some time ago as i have 1.5GB since 1-2 years ago.

```

crujido [15:09:56] gentoo-rsync # free -m

             total       used       free     shared    buffers     cached

Mem:          1514       1463         51          0        177        667

-/+ buffers/cache:        617        897

Swap:            0          0          0

crujido [15:22:20] gentoo-rsync # cat /proc/meminfo

        total:    used:    free:  shared: buffers:  cached:

Mem:  1588178944 1535356928 52822016        0 186544128 701300736

Swap:        0        0        0

MemTotal:      1550956 kB

MemFree:         51584 kB

MemShared:           0 kB

Buffers:        182172 kB

Cached:         684864 kB

SwapCached:          0 kB

Active:         466964 kB

Inactive:       715128 kB

HighTotal:      655280 kB

HighFree:         3868 kB

LowTotal:       895676 kB

LowFree:         47716 kB

SwapTotal:           0 kB

SwapFree:            0 kB

```

As you can see. 

My machine has 182MB used in buffers and 682MB in cache which are really free memory. Also you can notice i don't use swap because i don't need it. 

As I didn't know swapiness concept i disabled swap when i installed my system.  :Razz: 

Wheeeee....!

----------

## rich0

 *smiley wrote:*   

>  *Evangelion wrote:*    *sapphirecat wrote:*   2. The mysterious 800 MB limit on x86 
> 
> related to this: do you need to enable high memory-support on AMD64-machines to take advantage of lots of RAM? 
> 
> My understanding is that on 64bit processors, all memory is "low memory," so you wouldn't need high memory support. On the other hand, since amd64 also runs 32bit programs, maybe it's different than other 64bit processors.
> ...

 

If you are running your kernel in 64-bit mode (ie you selected x86_64 as your arch type), then I'm pretty sure you are good up to whatever virtual memory size your CPU handles (I think they're up to 48-bit or something like that right now).

If you compile your kernel telling it you just have a regular x86, it will run just fine in 32-bit mode, but then it has the same limitations on memory size (plus it can't use all those extra registers).

I'm guessing that if you run 32-bit apps under the 64-bit kernel in emulation mode, you probably can give each app about 1-4GB of RAM or something like that (not sure exactly how linux maps memory - I believe the 386 can handle 4GB, but the 1GB limit is probably due to using some of the address bits for other kernel purposes).  I'll have to check when I'm at home, but I'm guessing that there isn't even a compile option for big memory support on the amd64.  So, you can have 10 copies of 32-bit gimp running with 1GB of RAM each if you have enough RAM/swap.  Or, you can have a few copies of 64-bit gimp running with 100GB each...  

Keep in mind that while AMD64 supports up to 64-bit addressing in theory, if you read the fine print I think most of the existing processors only support about 40-bit physical RAM, and 48-bit virtual RAM.  That should tide us over for quite a while though, as long as future chips can go all the way to 64-bit.

----------

## erezny

Thanks a lot. this is very useful and isn't explained much anywhere.

----------

## routerguy

Hi, I have 1024 MB of RAM, but linux says I only have 883.  What's up with this?

----------

## RAPUL

 *Mystic0 wrote:*   

> Hi, I have 1024 MB of RAM, but linux says I only have 883.  What's up with this?

 

Solution to that problem was already said in this thread...

Enable this on your kernel.

```

Processor Type and Features ----> 

 High Memory Support ----> 

 (X) 4GB

```

and recompile it afterwards of course.  :Razz: 

----------

## routerguy

I saw that, I just thought it was strange that I have 883, not 800 MBs of RAM.... I thought the limit was 800.

----------

## Joe

 *sapphirecat wrote:*   

> 
> 
> Note that turning on high memory support does slow down accesses slightly, and the current belief is that for exactly 1GB of memory, it's better to leave high memory support off and sacrifice the space for speed.
> 
> 

 

Carefull, this depends heavily on what the machine is doing. If it's already swapping you better try to access every MB of RAM being available.

Regards,

Joe

----------

## bssteph

Just a note, Con Kolivas has said (I believe somewhere on Kernel Trap) that the overhead of enabling highmem has been mostly negated in 2.6. I did some really light "feel" testing and I didn't notice a difference between activated highmem or not when I tried on my GB RAM box, but I certainly did not do very rough benchmarks to stress the RAM system. ATM I am using highmem although this reminds me to look at it again.

----------

## mem7

```
MemTotal:       904296 kB
```

I just got a gig of ram 2 days ago and just assumed because I havent had the time to check untill now, that it was all recognized. Well, thanks once again to a Gentoo forum post another problem (one that I didn't even know had) is solved. But it dose make me wonder why it was detecting 900MB of ram instead of  the supposed 800MB limit with out high memory support in the kernel.

----------

## Abraxas

 *RAPUL wrote:*   

> I already knew most of this thread says but it is nice someone wrote all these ideas joint for people who don't know them.
> 
> I also discovered swapiness concept. So as everyday i learnt a new thing today.
> 
> I discovered these some time ago as i have 1.5GB since 1-2 years ago.
> ...

 

I'd think twice about that setup.  Everything I have read about swapspace in linux says that it is more effiecient to have a swap partition even if you have a lot or memory.  I had a setup with 1GB of mem and no swap but after reading up on it more I decided it would be better off with some swap space.

----------

## qal21

mem7: actually it's 870MB or so it's detecting for you, not quite 900 (a MB is 1024 KB not 100), oh well, I'm probably getting too technical

Anyways, about the main topic. I already guessed that that (about the main explaination) based upon top results. It's a good feature but, my HD is NOT quiet and when the CPU is idle it goes cacheing some of the disk, and very annoying because for 15+ min it's being really noisy .

----------

## stp_1993

Excellent writeup! This clears up a lot of confusion I've had.

 *sapphirecat wrote:*   

> 
> 
> To see a better estimation of how much memory is really free for applications to use, run the command:
> 
> ```
> ...

 

Okay. So in this case, 360MB of non-swap memory can be allocated by user programs?

Also, what do buffers=14mb and cached=293mb mean? I read your post very carefully, but I still do not understand. Why doesn't the used buffers/cache=143mb equal 14mb+293mb?

----------

## meyerm

 *mem7 wrote:*   

> 
> 
> ```
> MemTotal:       904296 kB
> ```
> ...

 

First: The Limit is not 800 MB but higher. (see below)

Second: 

```
meyerm@yavin meyerm $ echo 904296/1024 | bc --

883

```

----------

## rich0

 *stp_1993 wrote:*   

> 
> 
>  *sapphirecat wrote:*   
> 
> To see a better estimation of how much memory is really free for applications to use, run the command:
> ...

 

Yes

 *Quote:*   

> Also, what do buffers=14mb and cached=293mb mean? I read your post very carefully, but I still do not understand. Why doesn't the used buffers/cache=143mb equal 14mb+293mb?

 

143 = 451 - (14+293)

360 = 52 + (14+293)

143 is not the space used by buffer/cache.  It is the amount used after buffers/cache are subtracted out.[/quote]

----------

## nadamsieee

 *qal21 wrote:*   

> mem7: actually it's 870MB or so it's detecting for you, not quite 900 (a MB is 1024 KB not 100), oh well, I'm probably getting too technical

 

Lets not rehash that debate!   :Wink: 

----------

## log

Thanks for explaining that  :Smile: 

About 3 months ago I needed more ram for my PhD research and upgraded to 1GB, ran free and thought "WTF!?"   :Laughing: 

So i remembered that there was that option to enable 4GB in the kernel and tried it out. I think it would be a good idea to have a small description in the kernel that explains if you have exactly 1GB and want it all.

To me, I need the additional memory desperatly (im solving large non-sparse linear programming problems)

----------

## rich0

 *log wrote:*   

> Thanks for explaining that 
> 
> About 3 months ago I needed more ram for my PhD research and upgraded to 1GB, ran free and thought "WTF!?"  
> 
> So i remembered that there was that option to enable 4GB in the kernel and tried it out. I think it would be a good idea to have a small description in the kernel that explains if you have exactly 1GB and want it all.
> ...

 

I got an AMD64 system with 512MB up and running for about $600 (some commodity parts like DVD-ROM and floppy drive were recycled, but all in all it would only cost about $700 from scratch).  Once you are 64-bit you can expand the memory size quite a bit without penalty.

If this is for your PhD perhaps you can get a grant to boost your computing power?  If 4GB is plenty then you may just want to stick with your x86.  Also - if you only have 32-bit binaries of your application for some reason you may also just want to stay with x86.  But if you have the source then most likely you can run it under AMD64 on gentoo just fine.

RAM isn't super-cheap, but if you really do need it then it is probably worth just buying.  And a newer system with faster RAM may also help out for your application.

----------

## log

a-ight, I would love to be 64bit....BUT the uni has provided me with a P4 3.2Ghz HT ("State of the art") and the losers they bought it from are "mr dickhead intel" boys....so unfortunatley there isnt much hope of me going the SUPERIOR AMD way.

All in all, I was luck to get the 512MB upgrade.

sorry, i just came home after having a few beers (im Aussie after all) so if there is gramatical errors...meh   :Razz: 

life is fun, have a good one  :Smile: 

----------

## madmango

I'm pretty sure you do, memory management on x86 and x86_64 are identical. The only difference is where the controller is located  :Very Happy: 

----------

## Joseph_sys

I took my old box with 1GB RAM to a test. I compile one kernel 2.6 with high mem. support and one without and here are some numbers:

1.) Without High Memory Support:

       total      used       free     shared    buffers     cached

Mem:    882        244        638          0         10        123

-/+ buffers/cache: 110        772

Swap:   956          0        956

2.) With High Memory Support

       total       used       free     shared    buffers     cached

Mem:   1009        357        652          0         13        224

-/+ buffers/cache: 119        890

Swap:   956          0        956

In addition I have run some "hdparm -Tt /dev/hda" test with and without hight memory support and to be honest I did not noticed any difference.

As I run a web-server I check my page_parse_time.log and did not noticed any difference either.

Though I like the number 652 free RAM with High Memory Access much better than 638  :Smile: 

Though I must admit this article is very informative.

----------

## log

After I upgraded to 1GB and enabled high memory, I did not notice a drop in performance. Although the costomised software im using uses more ram than CPU...oh well, im happy   :Very Happy: 

----------

## sapphirecat

 *log wrote:*   

> After I upgraded to 1GB and enabled high memory, I did not notice a drop in performance. Although the costomised software im using uses more ram than CPU...oh well, im happy  

 

Alright, then. I'll mark it "mostly theoretical"; AFAIK when Linux has to look up something in the page tables with high memory support, it has to go through three layers rather than two.

----------

## i_hate_your_os

The .88G limitation was news to me.  Great thread, thanks!

----------

## nadamsieee

 *sapphirecat wrote:*   

> Alright, then. I'll mark it "mostly theoretical"; AFAIK when Linux has to look up something in the page tables with high memory support, it has to go through three layers rather than two.

 

There is definately a real, live performance hit introduced by the extra layer. There is nothing theoretical about it. What is debatable is whether the hit is ever noticable by us low frequency humans. Just nit-picking.  :Wink: 

----------

## tommy_fila

For everyone who is wondering why their swappiness settings are always changing to seemingly random numbers:

Direct quote from bk0

 *Quote:*   

> gentoo-dev-sources (and probably other gentoo 2.6 kernels) contains the Con Kolivas autoregulated swappiness patch. This means that the kernel automatically adjusts the /proc/sys/vm/swappiness value as needed during runtime, so any changes you make will be clobbered next time it updates. A good explanation of this patch and how it works is here on KernelTrap. 
> 
> I repeat: With gentoo-dev-sources it is neither necessary nor possible to permanently adjust the swappiness value. It's taken care of automatically, no need to worry. 

 

This was taken from the following thread:

https://forums.gentoo.org/viewtopic.php?p=1258300#1258300

----------

## Chonhulio

 *Quote:*   

> There is definately a real, live performance hit introduced by the extra layer. There is nothing theoretical about it. What is debatable is whether the hit is ever noticable by us low frequency humans. Just nit-picking.

 

IIRC 3-level page tables are only used when CONFIG_HIGHMEM64G is set in order to support RAM above 4GB (PAE mode).

The usual CONFIG_HIGHMEM4G just changes the adress space layout and adds additional mappings for RAM above 1GB.

So the performance penalty should only occur when actually allocating RAM above 1GB and should be very mild even then.

The major, yet mostly theoretical, downside of CONFIG_HIGHMEM4G  is, that the process space per single process is reduced to 2GB. Even if you got 4GB of RAM, each process may only allocate 2GB, unless you enable CONFIG_HIGHMEM64G. However, this should be pretty irrelevant on workstations and most servers.

Additionally some kernel features and 3rd party patches may not work reliably (or at all) on HIGMEM Kernels. Examples are cryptoapi up to 2.6.4, suspend-to-disk up to 2.6.6 and some poorly maintained drivers, including some binary only crap.

----------

## Clansman

hi everyone.

excelent post by sapphirecat!

yet, i think few people that posted here got the original post's point according to the post's replies... overall, about the memory size and occupancy one can conclude:

Having low free memory reported from 'top' DOES NOT MEAN that your system is desperately and unrecoverably occupying all of your memory (or most of it), meaning that you must rush to the nearest and most expensive store to buy another Gb of ram.

The linux kernel has a memory management policy that cleverly tries to use up as much memory as it possibly can, to improve performance. This performance improvement can be very visible if that used memory becomes disk cache because as stated before, hard drive acesses are about 1000 times slower than RAM, so getting disk's data (cache) from ram is very fast. The RAM is the fastest and second biggest memory available outside the cpu.

That big memory cache forcedly used by the linux kernel to avoid wasted memory can be VERY QUICKLY shrinked up to allow user applications to use memory/more memory.

So, having 70Mb of free ram when you have only 1Gb is not a bad thing and does not mean you have to buy new memory to boost performance. You would probably find out that with your new 2Gb of ram, you'd end up having... 100Mb free...

[]

----------

## sanity

I'm still not getting what auto-swappiness optimizes for, but I don't use it anyway.

On all my machines, I sort of take the opposite approach.  I set swappiness to 100.  This is especially useful on my desktop.  Consider:  My brother runs GNOME.  I run Fluxbox.  We run them on different X servers, so that we can swtich off, leaving programs open and running, without sharing settings / giving him access to my settings (PGP key, etc. etc.)

Obviously, when he's on, he uses a lot of RAM.  Now suppose I want to play ut2004.  You see the problem?

Of course, his stuff can be swapped out.  It's not likely to happen, though, unless I'm actually running out of memory, or unless swappiness is quite high.  And if he's hogging the RAM, I don't get to use it for buffer/cache, which can really help speed sometimes.  (Oops!  Firefox crashed! (buggy flash plugin)  Oh, but it's cached -- 1-2 seconds and I have a browser again.)

This is also helpful against fragmentation.  This may be getting just a bit obsessive, but I use reiser4, and if I'm downloading a 250 meg file and I have 500 megs of RAM, it may very well stay buffered until the download is done, and then be written entirely contiguously.

Basic formula goes like this:  if you think have enough RAM, you might set swappiness=0.  Like, if you've got 3 gigs of RAM on a desktop machine, and you leave it on all night, cron jobs won't take significantly longer, and they won't push your apps out into swap -- thus, you come back and everything's just as you left it, no waiting around.

If you do not have enough RAM, or if you think you might be wasting some RAM, you might set swappiness=100.  Now, all the things you aren't using go to swap, out of the way for things you are using, such as ut2004.  The downside?  After leaving the computer for awhile, it's quite likely that a cron job or a running download or something has kicked your email, your browser, even your terminal emulator out to swap, and thus, it might take 5 seconds for your computer to come back to life, and another 1-2 seconds each time you bring up a window that you haven't seen in awhile.

My last OS was Windows 98.  5 seconds I can live with, to avoid buying enough RAM to run GNOME, two instances of Fluxbox, 3 X servers, and ut2004.

----------

## sploo22

Along these same lines, is it possible to force a program and its assorted libraries to stay in the cache, so e.g. I can have Firefox ready to start up even if I haven't been using it recently? Preferably something more robust than "dd if=/usr/bin/firefox of=/dev/null" every 30 seconds. Does the kernel provide this level of control?

----------

## Clansman

 *sploo22 wrote:*   

> Along these same lines, is it possible to force a program and its assorted libraries to stay in the cache, so e.g. I can have Firefox ready to start up even if I haven't been using it recently? Preferably something more robust than "dd if=/usr/bin/firefox of=/dev/null" every 30 seconds. Does the kernel provide this level of control?

 

i seriously doubt it. and i don't think the kernel is made to act that way. it would be application-dependant and would require personalized configurations, which is not the case on any of today's kernel.

nevertheless it could become a usable idea, in some patch-package...

[]

----------

## dmitrio

I have copied this, with permission of  sapphirecat and bk0, to gentoo-wiki.com 

http://gentoo-wiki.com/Linux_Memory_Management

If you see anything that should be added or changed, feel free to do so. 

Thank you for a great HOWTO.

----------

## solarium_rider

I had a quick question, since nothing is really mentioned (explicetely)on whether it's good or not if the swap file is full.  Currently, when i try to view a mpeg from the web or something (through mplayer) the whole system becomes laggy (mouse pointer skips bad.)  The desktop also seems laggy when I'm running an emerge sync.  I'm using vanilla 2.6.6 kernel.  Also is it normal for kdeinit to use 30% of my ram (according to top). 

Here's what my stuff looks like: 

```

ccase@miles ccase $ free -m

             total       used       free     shared    buffers     cached

Mem:           503        495          7          0          4         34

-/+ buffers/cache:        456         46

Swap:          517        517          0

ccase@miles ccase $ uptime

 01:49:06 up 16 days, 13:40,  1 user,  load average: 0.13, 0.14, 0.17

ccase@miles ccase $ cat /proc/meminfo

MemTotal:       515520 kB

MemFree:          8100 kB

Buffers:          4468 kB

Cached:          35068 kB

SwapCached:       2092 kB

Active:         460344 kB

Inactive:        18756 kB

HighTotal:           0 kB

HighFree:            0 kB

LowTotal:       515520 kB

LowFree:          8100 kB

SwapTotal:      530136 kB

SwapFree:           68 kB

Dirty:               8 kB

Writeback:           0 kB

Mapped:         462644 kB

Slab:            15784 kB

Committed_AS:   994508 kB

PageTables:       3592 kB

VmallocTotal:   516056 kB

VmallocUsed:     39604 kB

VmallocChunk:   475300 kB

ccase@miles ccase $ cat /proc/sys/vm/swappiness

60

```

Any recommendations on whether I should switch kernels or just change the swapiness?  Do you think my lag issues are cpu or mem related?  I was under the impression the 2.6 kernel was suppose to have better scheduler for a desktop system.

Thanks

----------

## Clansman

what's your swapiness level? do you set it manually?

you could compile a 2.6.7 kernel and include con kolivas' autoregulated swapiness patch in it. i use it.

or, from kerneltrap.org kerneltrap.org/node/view/3366 andrew morton's patchset with nick piggins' addicion could also help you out. 

 *Quote:*   

> Nick's patchset, which applies against the 2.6.7-mm3 kernel, provides a different CPU scheduler and additional memory management work. For the interested, Nick advises, "if anyone is having swapping or interactivity problems, please try it out.

 

[]

----------

## rich0

 *solarium_rider wrote:*   

> I had a quick question, since nothing is really mentioned (explicetely)on whether it's good or not if the swap file is full.  Currently, when i try to view a mpeg from the web or something (through mplayer) the whole system becomes laggy (mouse pointer skips bad.)  The desktop also seems laggy when I'm running an emerge sync.  I'm using vanilla 2.6.6 kernel.  Also is it normal for kdeinit to use 30% of my ram (according to top). 
> 
> Here's what my stuff looks like: 
> 
> <snip>
> ...

 

My guess is that swappiness has nothing to do with it - only 34MB of your RAM is used by cache.  It looks to me like some processes is hogging a lot of RAM.  And if kdeinit is using 30% of your RAM with this much RAM in the system, that would seem odd to me...

----------

## sanity

 *Quote:*   

> my HD is NOT quiet and when the CPU is idle it goes cacheing some of the disk, and very annoying because for 15+ min it's being really noisy .

 

For 15+ min?  How much swap space do you have?  Something tells me that you need more RAM on that thing -- my hd is idle when my box is idle, for the most part, and I only have 512 megs.

 *Quote:*   

> Everything I have read about swapspace in linux says that it is more effiecient to have a swap partition even if you have a lot or memory.

 

Depends.  In theory, if you are not using tmpfs and you're on a tiny filesystem (like a floppy or a cd) with much more ram than "hard" space, then swap doesn't add anything.

Only real-world case I can think of is if you can predict that you will never need to have more memory than your total amount of RAM.  But by "needing memory", I include disk cache, and more cache is usually better.

Only real-world scenario I can think of where someone should choose not to have swap is if they need the space and/or they are running a very active server, on which almost none of the programs running will ever be idle.

----------

## Rainmaker

Maybe add this bit:

Your system only reports 28 Megs when you actually have 32 Megs. This is because part of the memory is used for the kernel code, and another part of it is "reserved". Check the ouput of dmesg to see how much this is. The less you "built into" your kernel (i.e. use modules) the smaller your kernel gets.

----------

## jkruer01

Can someone please explain to me what the benefits of swaping out the contents of cache?  The purpose of cache is to store content on a higher speed medium so you can access it faster.  If you move the cach to the harddrive then doesn't it defeat the purpose of the cache?

----------

## rich0

 *jkruer01 wrote:*   

> Can someone please explain to me what the benefits of swaping out the contents of cache?...If you move the cach to the harddrive then doesn't it defeat the purpose of the cache?

 

You don't swap out the cache - I don't think any decent OS does that at all.

What the debate is about is swapping out programs in memory in order to make room for cache.

It is undisputed that if idle software needs more RAM than you have physical memory for, then swapping is necessary to load new more-active programs.

What is disputed is whether the system should go ahead and swap out programs that are idle when it does not need that memory to run other programs.  By swapping this memory out, it frees up RAM to use as a cache.  On the other hand, if you don't swap it out, then if one of those programs wakes up it won't have to be read in.

Neither side is advocating swapping out the cache itself.  Linux simply uses unused memory as the cache/buffers.

----------

## jkruer01

Your right, I'm sorry.  I didn't take enough time to read every word.  Now that I went back and read the original post more slowly it makes much more since to me.

Thanks!

----------

## Stieltje

I guess that gkrellm does not report free mem the same way that 

```
 free -m 
```

 does

```

# free -m

             total       used       free     shared    buffers     cached

Mem:          1518        790        728          0        144        415

-/+ buffers/cache:        229       1288

Swap:         4000          0       4000

```

gkrellm reports that I have 1289 MB free...

Anyway, nice post, I guess I won't have to bother about swappiness though I have the latest ck-sources installed  (2.6.7-ck5) 

/Stieltje

----------

## sapphirecat

 *Stieltje wrote:*   

> 
> 
> ```
> # free -m
> 
> ...

 

Must have been in a different thread that I mentioned this... gkrellm pretty much does the same as free with -/+ buffers/cache; the difference between 1288 and 1289 is probably rounding error. It's also possible to go into gkrellm's memory options and make it show three krells, splitting things into used, buffers, cache, and completely free.

----------

## Nutterpc

after having this problem myself.....I always wondered what I could do to try to counteract it

part of it came by reading available values in sysctl -a

part of it came via research......well........most of it really  :Smile: 

Now yes, I will admit I used to have issues with linux seemingly runnin off with all my RAM, and then going to swap, but with 1Gb DDR400 DualChannel.........it shouldn't need to   :Confused: 

After nearly a weeks worth of research so far, I've come up whit some fairly drastic changes in sysctl.conf, which, on my machine......are producing very noticeable results

Programs take less time to load, they don't hog Proc time (which they used to beforehang  :Shocked:  )

CPU  just blips every now and then, and Disk doesn;t even move...unless I load a program

And for all those wondering what kernel I'm running, its ck-sources latest: 2.6.8-ck3

Nutterpc

PS: if anyone else running this kernel would like to try my sysctl.conf settings out to help me further finetune them, my email addy should be in my profile......just contact me on msn  :Wink: 

----------

## smg

Mh, I'm just asking me, what's faster for my system, 2*256 DDR-Ram @ 400Mhz working as dual  ram modules or making out of this 1GB. Can you recommend me one of this possibilities?

best regards 'ash'

----------

## Nutterpc

all depends really, there's quite a few factors that come into it

a) how much memory do u need

b) how much do u want to spend on memory

c) is the $$ needed to get the extra ram going to make it worthwhile for what *you* do?

----------

## minaguib

To add to the excellent post, I've noticed something peculiar about `top`s output that I thought I'd point out.

At the header near the top, `top` says that I'm using no swap:

```
Swap:   265064k total,       12k used,   265052k free,   112472k cached
```

(well, almost no swap - 12k is negligible)

However, the "SWAP" column in the process listing lists several processes with huge chunks of "swap":

```
  PID USER      PR  VIRT  RES  SHR %CPU %MEM    TIME+  SWAP CODE DATA COMMAND

25274 mina      15  132m  55m  32m  0.0 22.3   1:52.33  76m   68  99m thunderbird-bin

 1960 mina      15 95628  32m  31m  3.4 12.9   0:32.40  61m   68  61m firefox-bin

 6076 root      15 92496  24m  69m  2.1  9.6   4:38.54  66m 1680  18m X

 1277 mina      15 46084 7360  10m  0.2  2.9   0:03.57  37m  984  33m xmms

25060 mina      16 24216 8572  13m  0.6  3.3   0:47.93  15m  796 9320 gkrellm2

 6819 mina      15  9508 6480 5840  0.0  2.5   0:32.25 3028  808 2860 irssi
```

This, of course, is contradictory.  If there's no swap usage, no process could possibly have swapped data.

My personal conclusion (corrections welcome) is that `top` uses a fairly primitive way to calculate swap per process:

```
Swap = Total Usage - Resident Usage
```

It does not take into account mapped memory from non-resident sources, such as mapped files on disk, or memory on a graphics card.

In my example above, it's most likely that the 66 megs of memory assigned to "X" as swap is files mapped to disk and the memory on my el-cheapo graphics card.

And so my conclusion is, the "SWAP" column in the process list is not accurate and, for the most part, should be ignored.

----------

## jecepede

Ola !

WOW ! What a chrystal clear story ! Thank you all for the explanations. I improoved my system with the swappiness-option.

I do however have a small question....

If you look at the first cat proc/meminfo-output from the guy with 512 Mb you will see this :

 *Quote:*   

> 
> 
> ```
> ccase@miles ccase $ cat /proc/meminfo
> 
> ...

 

I get simular results when I do it with my own PC.

But ! When I do it on my old Armada laptop with only 64 Mb I get :

```
bash-2.05b# cat /proc/meminfo

MemTotal:        60424 kB

MemFree:          3712 kB

Buffers:           372 kB

Cached:          19440 kB

SwapCached:      19764 kB

Active:          47240 kB

Inactive:         2428 kB

HighTotal:           0 kB

HighFree:            0 kB

LowTotal:        60424 kB

LowFree:          3712 kB

SwapTotal:      124984 kB

SwapFree:        75124 kB

Dirty:              96 kB

Writeback:           0 kB

Mapped:          43356 kB

Slab:             5224 kB

CommitLimit:    155196 kB

Committed_AS:   105888 kB

PageTables:        964 kB

VmallocTotal:   974772 kB

VmallocUsed:       264 kB

VmallocChunk:   974464 kB

```

The total memory suddenly is oly 60 Mb and not 64 ! Where did my 4 much needed Mb's go to ???

Same thing happened on my normal PC : When I devide the 515520 Kb by 1024 I get only 503 Mb NOT 512 ???

I have see in some other topics they put an "append mem" in the lilo.conf but it doesn't seem to work for me ???

Can anyone explane that ?

Thanks guys and gals !!!

Jessy

----------

## miwalter

The guy with 512mb gets 503,44MB free reported (a "loss" of 8,56 MB). You get 59,01 MB reported (a "loss" of 4,99 MB).

I would guess this is kernel-memory or some "non usable" ram.

----------

## t3gah

Thanks for the explanation.

What annoys me about the 'new' kernel is that it creates a swap file twice the size of the physical memory in the system. So if you have 1.5GB or more there is this HUGE waste of space on the system because the equation the kernel has ratio wise to the amount of physical memory versus what people who have that amount of memory in their system use it for cancels out what the actual user wants or needs.  In essence, the user cannot disable this "feature" just like M$ because of whatever.  

There is of course another issue that comes up with all those older systems and the memory management scheme of the new kernel is there to help make things smoother, but the whole Linux 'world' has become so bloated that you now have to have 128MB minimum according to many distro's for X to even start which is wrong because I've gotten around that.

I wish this explanation was on Linux.org on the main page and on every distro site out there so people can understand the why for's, etc.

Someone from fedoraforum.org gave me this link.  Kudos to everyone.  I have Gentoo on one of my systems.  Sweet O/S.  :Smile: 

----------

## vyzivus

Hello,

thanks for your tutorial, it explained quite a few things. I have a few questions though.

- RES is amount of memory that the process physically occupies. So if I sum up all processes RES I get memory reported as 'used' by 'free' in '-+buffers/cache' section?

- is this the value that gnome-system-monitor shows as 'user memory' ?

- top manual states that RES = CODE + DATA. firefox-bin is obviously ignoring this equation  :Smile: 

```
  PID USER      VIRT  RES  SHR %MEM SWAP CODE DATA COMMAND

 9991 root     80988  54m 7228  5.4  24m 1808  47m X

25597 vyzivus   236m  48m  23m  4.8 187m  112  79m firefox-bin

25557 vyzivus   186m  47m  26m  4.7 139m 1992  50m amarokapp

25387 vyzivus   133m  24m  11m  2.5 108m  292  21m gnome-terminal
```

- Take a look at this:

```
  PID  VIRT  RES  SHR %CPU %MEM    TIME+  SWAP CODE DATA COMMAND

26191 1507m 182m  58m  0.0 18.1   0:40.73 1.3g   52 1.3g java

25597  239m  52m  23m  1.3  5.2   1:56.42 187m  112  83m firefox-bin

25557  187m  46m  25m  4.3  4.6   5:20.10 141m 1992  50m amarokapp
```

Here, java takes 1,3GB of SWAP space?? How can this be, on system with 512mb of ram and 256mb of swap? What the hell of memory does java address? I'm using amd64 version of sun-jdk-1.5.0.02, running as a server JavaVM. The system is amd64 with 64bit 2.6.11 gentoo-sources.

----------

## paulbiz

 *sapphirecat wrote:*   

> The mysterious 880 MB limit on x86

 

Nice, I've just reclaimed around 128MB more ram  :Smile: 

I always thought there was just something I didn't understand about how it measured my RAM, or my video ram or something was causing the number to be lower. I never looked at the "4 GB" option in the kernel config because I didn't have 4 GB of RAM. But, when I read the help, sure enough it said if you have between 1 GB to 4 GB use this option.

Thanks!

----------

## sn99520

My box is up for 1 day and 19:28

OpenCA server was running on it and now I stopped everithig except X, firefox and urxvt to write this post.

I reallly stoped almost everithing, look:

```

 ps -A

  PID TTY          TIME CMD

    1 ?        00:00:00 init

    2 ?        00:00:00 ksoftirqd/0

    3 ?        00:00:00 events/0

    4 ?        00:00:00 khelper

    9 ?        00:00:00 kthread

   19 ?        00:00:00 kacpid

  117 ?        00:00:01 kblockd/0

  188 ?        00:00:00 pdflush

  189 ?        00:00:02 pdflush

  191 ?        00:00:00 aio/0

  190 ?        00:00:01 kswapd0

  193 ?        00:00:00 jfsIO

  194 ?        00:00:00 jfsCommit

  195 ?        00:00:00 jfsSync

  196 ?        00:00:00 xfslogd/0

  197 ?        00:00:00 xfsdatad/0

  198 ?        00:00:00 xfsbufd

  794 ?        00:00:00 kseriod

  865 ?        00:00:00 ata/0

  867 ?        00:00:00 scsi_eh_0

  868 ?        00:00:00 scsi_eh_1

  877 ?        00:00:00 reiserfs/0

  938 ?        00:00:00 udevd

 4571 ?        00:00:00 khubd

 5547 ?        00:00:00 dhcpcd

 6825 ?        00:00:02 syslog-ng

 6879 ?        00:00:00 acpid

 7178 ?        00:00:00 khpsbpkt

 7244 ?        00:00:00 knodemgrd_0

 9344 ?        00:00:00 sshd

 9384 ?        00:00:00 cron

 9563 ?        00:00:00 xfs

 9629 ?        00:00:00 xinetd

 9644 tty1     00:00:00 agetty

 9645 tty2     00:00:00 agetty

 9646 tty3     00:00:00 agetty

 9647 tty4     00:00:00 agetty

 9648 tty5     00:00:00 agetty

 9649 tty6     00:00:00 agetty

 9745 ?        00:00:00 xdm

31419 ?        00:00:28 X

31424 ?        00:00:00 xdm

31456 ?        00:00:00 Xsession

31502 ?        00:00:00 fluxbox

31518 ?        00:00:00 torsmo

31529 ?        00:00:00 mozilla-launche

31540 ?        00:00:26 firefox-bin

31943 ?        00:00:00 urxvt

31954 pts/0    00:00:00 bash

 2304 pts/0    00:00:00 ps
```

And look at my mem usage, this is "top" sorted by mem usage. I don't have problem with 186120k buffers, and 791204k cached.

But there is only 314868k free. I have 2 Gig RAM.

This means RAM Usage is 742 M. what uses so much RAM, if I stoped almost everithing???

```
top - 17:03:14 up 1 day, 19:26,  0 users,  load average: 0.04, 0.01, 0.00

Tasks:  50 total,   1 running,  49 sleeping,   0 stopped,   0 zombie

Cpu(s):  2.3% us,  0.7% sy,  0.0% ni, 96.7% id,  0.0% wa,  0.3% hi,  0.0% si

Mem:   2056044k total,  1741176k used,   314868k free,   186120k buffers

Swap:   506036k total,        0k used,   506036k free,   791204k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                             

31419 root      15   0  126m  90m 2804 S  2.0  4.5   0:21.96 X                                                                                    

31540 steve     16   0 89932  28m  13m S  0.7  1.4   0:19.07 firefox-bin                                                                          

31943 steve     15   0 32584 5316 2408 S  0.0  0.3   0:00.24 urxvt                                                                                

31502 steve     15   0 28272 4468 2920 S  0.0  0.2   0:00.66 fluxbox                                                                              

 9563 xfs       16   0 15508 4084 1032 S  0.0  0.2   0:00.13 xfs                                                                                  

31424 root      16   0 24736 2336 1776 S  0.0  0.1   0:00.01 xdm                                                                                  

 9344 root      16   0 17072 1768 1392 S  0.0  0.1   0:00.07 sshd                                                                                 

31954 steve     15   0  8996 1540 1152 S  0.0  0.1   0:00.00 bash                                                                                 

 9745 root      16   0 17008 1292 1008 S  0.0  0.1   0:00.00 xdm                                                                                  

31529 steve     21   0  4820 1244  908 S  0.0  0.1   0:00.01 mozilla-launche                                                                      

 1559 steve     16   0 10432 1224  924 R  0.3  0.1   0:00.01 top                                                                                  

31456 steve     18   0  4560 1072  896 S  0.0  0.1   0:00.00 Xsession                                                                             

31518 steve     16   0  6652 1016  840 S  0.0  0.0   0:00.10 torsmo                                                                               

 9629 root      18   0 10828  968  776 S  0.0  0.0   0:00.00 xinetd                                                                               

 6825 root      16   0  6040  812  584 S  0.0  0.0   0:02.94 syslog-ng                                                                            

 9384 root      16   0  8952  760  620 S  0.0  0.0   0:00.00 cron                                                                                 

 9644 root      16   0  3620  644  540 S  0.0  0.0   0:00.00 agetty                                                                               

 9645 root      16   0  3620  644  540 S  0.0  0.0   0:00.00 agetty                                                                               

 9646 root      16   0  3620  644  540 S  0.0  0.0   0:00.00 agetty                                                                               

 9647 root      16   0  3620  644  540 S  0.0  0.0   0:00.00 agetty                                                                               

 9648 root      16   0  3620  644  540 S  0.0  0.0   0:00.00 agetty                                                                               

 9649 root      16   0  3620  644  540 S  0.0  0.0   0:00.00 agetty                                                                               

 6879 root      24   0  2680  596  504 S  0.0  0.0   0:00.00 acpid                                                                                

    1 root      16   0  2556  548  460 S  0.0  0.0   0:00.83 init                                                                                 

 5547 root      11  -5  2568  512  428 S  0.0  0.0   0:00.00 dhcpcd                                                                               

  938 root      12  -5  2540  460  352 S  0.0  0.0   0:00.02 udevd                                                                                

    2 root      34  19     0    0    0 S  0.0  0.0   0:00.03 ksoftirqd/0                                                                          

    3 root      10  -5     0    0    0 S  0.0  0.0   0:00.29 events/0                                                                             

    4 root      11  -5     0    0    0 S  0.0  0.0   0:00.00 khelper                                                                              

    9 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 kthread                                                                              

   19 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 kacpid                                                                               

  117 root      10  -5     0    0    0 S  0.0  0.0   0:01.37 kblockd/0                                                                            

  188 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pdflush                                                                              

  189 root      15   0     0    0    0 S  0.0  0.0   0:02.73 pdflush                                                                              

  191 root      19  -5     0    0    0 S  0.0  0.0   0:00.00 aio/0                                                                                

  190 root      15   0     0    0    0 S  0.0  0.0   0:01.69 kswapd0                                                                              

  193 root      25   0     0    0    0 S  0.0  0.0   0:00.00 jfsIO                                                                                

  194 root      25   0     0    0    0 S  0.0  0.0   0:00.00 jfsCommit                                                                            

  195 root      25   0     0    0    0 S  0.0  0.0   0:00.00 jfsSync                                                                              

  196 root      19  -5     0    0    0 S  0.0  0.0   0:00.00 xfslogd/0                                                                            

  197 root      19  -5     0    0    0 S  0.0  0.0   0:00.00 xfsdatad/0                                                                           

  198 root      15   0     0    0    0 S  0.0  0.0   0:00.01 xfsbufd                                                                              

  794 root      17   0     0    0    0 S  0.0  0.0   0:00.00 kseriod                                                                              

  865 root      11  -5     0    0    0 S  0.0  0.0   0:00.00 ata/0                                                                                

  867 root      16   0     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_0                                                                            

  868 root      17   0     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_1                                                                            

  877 root      10  -5     0    0    0 S  0.0  0.0   0:00.03 reiserfs/0                                                                           

 4571 root      15   0     0    0    0 S  0.0  0.0   0:00.11 khubd                                                                                

 7178 root      15   0     0    0    0 S  0.0  0.0   0:00.00 khpsbpkt    

```

I,m sorry if answer to my question was already answered in this thread, but I did not find it.

----------

## vyzivus

I also had problems with memory consumption. They vanished after revdep-rebuild, so my guess is that multiple versions of libraries were used at once by binaries that didn't get recompiled. Don't know the exact reason though.

----------

## LordThunder

 *t3gah wrote:*   

> Thanks for the explanation.
> 
> What annoys me about the 'new' kernel is that it creates a swap file twice the size of the physical memory in the system. So if you have 1.5GB or more there is this HUGE waste of space on the system because the equation the kernel has ratio wise to the amount of physical memory versus what people who have that amount of memory in their system use it for cancels out what the actual user wants or needs.  In essence, the user cannot disable this "feature" just like M$ because of whatever.  
> 
> There is of course another issue that comes up with all those older systems and the memory management scheme of the new kernel is there to help make things smoother, but the whole Linux 'world' has become so bloated that you now have to have 128MB minimum according to many distro's for X to even start which is wrong because I've gotten around that.
> ...

 

Some remarks to this:

1. The size of your swap partition is not at all determined by your kernel. 

The size is whatever you made it when partitioning your harddisk at the install of your system, so if it's 3gigs: blame your crappy distribution, not the kernel. 

In the old days the 'rule of thumb' was swapsize == 2x installed RAM ,but never larger than 128mb. I find that alof of people new to Linux are not aware of the second half of the rule. This made sense when you had computers with <=64 mb RAM installed. On top of that, the kernel didn't even support larger swapsizes than 128mb anyway, even if you made partition that was larger than that (you could only circumvent that by adding more swappartitions). If you have a regular modern system, you basically have to eyeball the swapsize and adjust it to whatever you think you want to have as extra 'breathingspace'. I usally reserve between 128 and 512mb. I would recommend to always have at least some swapspace. Actually, historically some parts of the system had issues with the swapsize being zero, so I guess it's also good way to avoid any of those issues that may linger.

2. The required memory of today's ditributions is mainly governed by the fact you use 'bloated' desktop environments. KDE and Gnome both load alot of resident libraries and services that eat away from your memory like there is no tomorrow; It's the price you pay for the 'integration', ease of use and mostly, the eyecandy.

If you wish to run a lighter desktop, try use other windowmanagers like windowmaker, fvwm2, fluxbox etc., which have a significantly smaller memory footprint.

Kind regards,

-- Lord Thunder (using Linux since 1994)

----------

## spindle

Excellent excellent writeup. Very informative. Cleared up a few things for me.

----------

## trinite

Having lately some memory problems with one of my servers. After running for about a week, all my memory and swap are in use, slowing down performance / killing apps. 

The server is a little bit short on memory (512 M RAM, an 1 G swap) and is running apache, php, courier-imap (pop3, imap, imap-ssl), qmail, spamassassin, clamav, vsftpd and ssh). 

free gives the following output: 

```
# free

             total       used       free     shared    buffers     cached

Mem:        512740     505536       7204          0       4064      10248

-/+ buffers/cache:     491224      21516

Swap:       979956     979956          0
```

top: 

```
top - 14:55:59 up 29 days,  4:34,  1 user,  load average: 4.01, 2.95, 1.80

Tasks:  82 total,   1 running,  81 sleeping,   0 stopped,   0 zombie

Cpu(s):  1.1% us,  0.0% sy,  0.0% ni, 97.8% id,  1.1% wa,  0.0% hi,  0.0% si

Mem:    512740k total,   506040k used,     6700k free,     4624k buffers

Swap:   979956k total,   979956k used,        0k free,    15808k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND

 4942 root      16   0  265m  60m  180 S  0.0 12.1   0:56.15 authdaemond

 4943 root      16   0  261m  60m   64 S  0.0 12.0   0:55.22 authdaemond

 4946 root      16   0  260m  59m  348 S  0.0 11.9   0:55.56 authdaemond

 4944 root      16   0  261m  59m  192 S  0.0 11.9   0:55.58 authdaemond

 4945 root      16   0  259m  58m   72 S  0.0 11.6   0:54.89 authdaemond

32224 root      16   0 61800  57m 1000 S  0.0 11.4   0:48.56 spamd

32225 root      17   0 56284  51m  856 S  0.0 10.4   0:04.11 spamd

32112 root      16   0 53356  49m  996 S  0.0  9.8   0:03.49 spamd

 4815 root      16   0 58536  16m  232 S  0.0  3.4  20:53.05 clamd

 4444 mysql     17   0  140m 4564 1244 S  0.0  0.9   2:56.12 mysqld

 7725 apache    16   0  224m 4068 1184 S  0.0  0.8   0:02.00 apache2

 1583 root      17   0  6884 3668  520 S  0.0  0.7   0:00.15 qmail-scanner-q

 5354 root      16   0  3556 3556 2584 S  0.0  0.7   0:02.16 ntpd

14590 root      16   0  6572 3364  448 S  0.0  0.7   0:03.16 gds_inet_server

 7723 apache    16   0  223m 3332  576 S  0.0  0.6   0:02.30 apache2

13958 root      16   0  6568 3232  456 S  0.0  0.6   0:03.93 gds_inet_server

23925 root      16   0  6764 1836  492 S  0.0  0.4   0:02.26 apache2

 7722 apache    18   0  6392 1740  300 S  0.0  0.3   0:00.00 apache2

  933 root      15   0  6164 1040  660 S  0.0  0.2   0:00.19 sshd

 1804 root      16   0  2080  988  720 R  0.4  0.2   0:00.18 top

  948 admin     16   0  6328  956  548 S  0.0  0.2   0:00.32 sshd

  949 admin     15   0  2796  844  544 S  0.0  0.2   0:00.00 bash

  961 root      17   0  2536  840  540 S  0.0  0.2   0:00.03 bash

  954 root      16   0  2160  760  548 S  0.0  0.1   0:00.00 su

 1582 qmaild    15   0  2764  604  432 S  0.0  0.1   0:00.00 qmail-smtpd

 5827 root      16   0  2100  584  508 S  0.0  0.1   0:02.76 xinetd

 5751 root      16   0  2956  528  488 S  0.0  0.1   0:00.23 vsftpd

 5901 root      16   0  1492  516  512 S  0.0  0.1   0:00.00 agetty

 5902 root      16   0  1492  516  512 S  0.0  0.1   0:00.00 agetty

 5903 root      16   0  1492  516  512 S  0.0  0.1   0:00.00 agetty

 5904 root      16   0  1492  516  512 S  0.0  0.1   0:00.00 agetty

 5905 root      16   0  1492  516  512 S  0.0  0.1   0:00.00 agetty

 5908 root      16   0  1492  516  512 S  0.0  0.1   0:00.00 agetty

 5679 root      17   0  1704  492  452 S  0.0  0.1   0:00.34 cron

32177 qmaild    16   0  1512  492  424 S  0.0  0.1   0:00.10 tcpserver

32181 qmails    16   0  1488  460  380 S  0.0  0.1   0:00.19 qmail-send

  812 root      15   0  1536  436  392 S  0.0  0.1   0:18.07 couriertcpd
```

As you can see, the load averages are also much to high, which might be caused by the memory shortage?

Does anyone know what happens to all my memory? 

Tnx in advance!

Btw, it is a P4 2400.

----------

## phajdan.jr

Read http://gentoo-wiki.com/FAQ_Linux_Memory_Management and post 1155852.

About load average: 15-min load average seems to be ok (1.80).

----------

## nixnut

merged above two posts here.

----------

## desultory

Split off topic "Ubuntu memory management and lag issues".

----------

## Evincar

Excellent explanation!! I was wondering why I always had only about 60MB free. And it kinda puzzled me that the system stayed responsive nevertheless. In XP, whenever my mem usage goes to 800 MB or so, I am toast   :Sad: . 

I would say this should be a sticky or part of the FAQ!

----------

## Auswaschbar

I have another question:

Because i have 1280mb of ram in my notebook, but didn't want the highmem-overhead I set the 2g/2g user/kernel memory split (I use kernel 2.6.20, in older ones there is no such options I think). Now it recognizes and uses all of the ram. I only wonder if there are any disadvantages of this method?!?

----------

## achlice

really did a good job ,i mean ,the editer~~

----------

## x22

 *vyzivus wrote:*   

> Hello,
> 
> - RES is amount of memory that the process physically occupies. So if I sum up all processes RES I get memory reported as 'used' by 'free' in '-+buffers/cache' section?
> 
> 

 

The sum will be different. In memory management everything is more complicated as it looks. (For example, some memory is shared between more processes, some memory is used for kernel and not for any process, and there are millions of other possibilities.)

 *vyzivus wrote:*   

> 
> 
> ```
> 
>   PID  VIRT  RES  SHR %CPU %MEM    TIME+  SWAP CODE DATA COMMAND
> ...

 

Top calculates SWAP as VIRT-RES. This is not the correct way to calculate swap usage of a process. (Once again: In memory management everything is more complicated as it looks.) I don't know the correct way. 

Java is very good at taking large amounts purely virtual memory (memory which is unused and physically stored nowhere). Large part of that 1.5GB doesn't take any space in RAM or on disk.

----------

