亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標題: 推薦一篇文章,希望對大家有幫助 [打印本頁]

作者: snow_insky    時間: 2006-01-11 16:45
標題: 推薦一篇文章,希望對大家有幫助
該文章引用自http://forums.gentoo.org/viewtopic.php?p=1155852

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:
Code:
free -m

The -m option stands for megabytes, and the output will look something like this:
Code:
             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:
Code:
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:
Code:
# sysctl -w vm.swappiness=30
# echo 30 >/proc/sys/vm/swappiness

The default when Gentoo boots can also be set in /etc/sysctl.conf:
Code:
# 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.
作者: albcamus    時間: 2006-01-12 09:21
偶也轉一個(轉自kernelnewbies郵件列表)

On 10/17/05, Roy Smith <misterdabolina@xxxxxxxxx> wrote:
> Hi all,
>
> I want to refine a previous question of mine
> (and thank everybody who helped me!)
> about the virtual memory of the kenel itself.
>
> Where does the kernel keeps its own page tables ?
> are they all swapped-in all the time ?

The memory in the Kernel/Kernel Module will never be swapped-out !!!!

The kernel keeps the Virtual Address range from 3G (PAGE_OFFSET) to 4G
for it-self, in which it creates 1 to 1 mapping of physical memory
like 3G of Virtual Address points to 0 Physical Address and so on ....
till the 896MB Physical RAM or less than 896MB MAX avaialble RAM in
the System ... and for (3G + 896MB) to 4G is called as
VMALLOC_RESERVE, which is used in tempoarary mappings of the physical
memory above 896MB (HighMemory) .... and let say if system has 256MB
RAM then 3G to (3G + 256MB) Virtual Address will have direct mappings
and remaining 4G - (3G + 256MB) Virtual Address range is
VMALLOC_RESERVE .....

So the page table exists for 0 to 896MB of Physical RAM and for
accessing more than 896MB it temporary creates mappings with-in its
virtual address range already reserved for Highmemory mappings .....

> isn't it a lot of mostly-unused memory ?
>

No its not unused memory because in modern operating systems and in
Linux too the approach is not keep free memory there ... rather use
all memory for caching to speed-up the things and when some one needs
memory it simply fullfills the requirement from the memory already in
use for caching .... Although page structures and page tables uses and
required memory but that memory we can't avoid as those have to be
kept some where in memory for fast accessing .... and 0 to 896MB only
Physical Memory mapping/page tables are created to use the memory as
much less as possible for them .... because if the system has 32GB of
RAM and kernel create all the page-tables then u can think that more
than 32-times of memory required for keeping page-tables as compare to
keep them only for less than 1GB RAM ....

(I might not be so clear to explain and might be missing something, so
others plzz do make them correct)
作者: snow_insky    時間: 2006-01-12 12:40
dfsf
作者: snow_insky    時間: 2006-01-12 12:42
這兩天,我怎么都不能發(fā)貼,不知怎么搞的!唉...

現(xiàn)在許多人對linux的內存使用量感到迷惑,老是覺得自己的機子內存無緣無故的被用完了,我想看了上面的東西,你們應該明白這是怎么回事。
作者: Solaris12    時間: 2006-01-13 15:28
原帖由 snow_insky 于 2006-1-12 12:42 發(fā)表
這兩天,我怎么都不能發(fā)貼,不知怎么搞的!唉...

現(xiàn)在許多人對linux的內存使用量感到迷惑,老是覺得自己的機子內存無緣無故的被用完了,我想看了上面的東西,你們應該明白這是怎么回事。



Solaris也有類似的機制,幾乎所有現(xiàn)代系統(tǒng)都有。
作者: hjxisking    時間: 2006-01-13 15:59
原來內存被用的越多,意味著你的機器在處理上可以跑的更快一點?
作者: albcamus    時間: 2006-01-13 16:03
原帖由 hjxisking 于 2006-1-13 15:59 發(fā)表
原來內存被用的越多,意味著你的機器在處理上可以跑的更快一點?

--編輯了半天, 本著厚道的原則, 自己刪掉了。

學習一點BIO的知識吧, 懂個大概再來批評, 好嗎?

[ 本帖最后由 albcamus 于 2006-1-13 16:06 編輯 ]
作者: snow_insky    時間: 2006-01-14 14:33
原帖由 hjxisking 于 2006-1-13 15:59 發(fā)表
原來內存被用的越多,意味著你的機器在處理上可以跑的更快一點。


我想我們也應該聽到這種反對的聲音,這樣才能討論嘛,謝謝!

是這樣的,查看linux系統(tǒng)中處于free狀態(tài)的內存有兩個角度,一個是從內核的角度來看,一個是從應用層的角度來看的。

1.從內核的角度來看free的內存,就是內核目前可以直接分配到,不需要額外的操作,這個free值是不包括系統(tǒng)中處于buffer和cache狀態(tài)的內存;但是在內核需要時,或在系統(tǒng)運行逐步推進時,buffer和cache狀態(tài)的內存可以變?yōu)閒ree狀態(tài)的內存。

2.從應用層的角度來看系統(tǒng)處于free狀態(tài)的內存,這個值是包括處于buffer和cache的,所以應用層分配內存時,可以直接從buffer和cache中拿。

linux系統(tǒng)之所以提高這種機制,是因為把內存都置為free狀態(tài),還不如把最近使用過的內存緩存起來(如從磁盤中讀取的數(shù)據(jù)),這樣再次需要這些數(shù)據(jù)時可以直接從內存中取,而不需要有一個漫長的磁盤操作,這樣可以提高系統(tǒng)的整體性能。因為free狀態(tài)的內存中的內容是不可用的,與其閑置還不如發(fā)揮它們的作用。而在系統(tǒng)需要時,又可以快速的從這些可釋放的內存中分配,我想這種機制是非常好的,老兄您認為呢?一個普通人都知道...呵呵

下面我們來看看free命令的結果:

             total       used       free     shared    buffers     cached
Mem:           500        355        145          0         67        249
-/+ buffers/cache:         38        462
Swap:          996          1        994

在這個結果中的第一行是從內核角度來看系統(tǒng)內存使用狀態(tài)的,可以看到free的內存只有145M;
第二行是從應用層的角度來看系統(tǒng)內存的使用狀況,可以看到free的內存有462M;

你有沒有看到462這個值的妙處呢????462 := 145 + 249 + 67
作者: kewell11    時間: 2006-01-18 13:41
好啊,雖然本來就知道。
其實windows下也有disk cache使用了ram,不過沒有這么明顯而且(使用得沒有那么充分),linux下是盡可能地是用空閑地ram來cache。
作者: lvscar    時間: 2006-02-04 14:12
好文。!   
     
在我的筆電上 vm.swappiness = 40 時

系統(tǒng)反應速度有明顯上升
作者: 李某人    時間: 2006-02-10 23:57
E文不太好,要是能翻譯下就好了!
作者: linux_ha    時間: 2006-02-15 12:00
我發(fā)了兩個貼為什么沒有顯示呢?
作者: sunday999    時間: 2006-09-13 17:45
哇!
全英文的呀。!
作者: blue_stone    時間: 2006-09-16 09:35
如何查看系統(tǒng)真實內存的大小?
作者: zonyonq    時間: 2006-09-16 10:01
Free
作者: ngzyl    時間: 2006-09-17 17:15
其實這樣的機制 都是為服務器所優(yōu)先考慮的, 服務器處理大量數(shù)據(jù)很多有相同性,在個人電腦上,特別是運行X 時候 就顯的 有些緩慢了,因為畢竟每個窗口和程序都會是意外的新的,我感覺這種機制還需要有改進的地方和研究的意義.....

不知道你們贊同不贊同......個人意見
作者: snow_insky    時間: 2006-09-18 11:41
我不是太明白你說的這段話,但是我對你認為這種機制只對服務器有效表示不贊同.
還有為什么你說運行X的時候,整個系統(tǒng)就會變慢呢?雖然它會把內存用于緩存,但是只要其它的地方需要,這些緩存就會被釋放,因為它們的優(yōu)先級是比較低的,一旦系統(tǒng)有內存需求時,這些緩存就會被釋放,在目前的CPU中,它們不會對系統(tǒng)的運行速度造成任何可以感知的影響.
作者: ngzyl    時間: 2006-09-18 13:19
原帖由 snow_insky 于 2006-9-18 11:41 發(fā)表
我不是太明白你說的這段話,但是我對你認為這種機制只對服務器有效表示不贊同.
還有為什么你說運行X的時候,整個系統(tǒng)就會變慢呢?雖然它會把內存用于緩存,但是只要其它的地方需要,這些緩存就會被釋放,因為它們的優(yōu)先 ...




呵呵,我不是說這個機制不好阿~~,也沒有說認為只在服務器有效,我想表達的是 這種機制還有挖掘的潛力,還有很多深入的技術和方法可以讓我們去研究,而不是只停留現(xiàn)狀,因為畢竟還是有不足的地方的。。。這樣說可能容易理解些吧!艺Z文水平有限  多包含。。
作者: blue_stone    時間: 2006-09-18 13:49
原帖由 zonyonq 于 2006-9-16 10:01 發(fā)表
Free

free查到的是系統(tǒng)可用內存的大小, 不是系統(tǒng)物理內存的大小
作者: 思一克    時間: 2006-09-18 16:40
對于desktop的快速響應來說,內存用于緩存太多了(幾乎可用內存都用完了)未必好。

釋放是要時間的。而且許多時候釋放還要寫磁盤(寫會內存中的DIRT PAGE),還需要swapd的參與。
因此會使響應在一開始時候變慢。

原帖由 snow_insky 于 2006-9-18 11:41 發(fā)表
我不是太明白你說的這段話,但是我對你認為這種機制只對服務器有效表示不贊同.
還有為什么你說運行X的時候,整個系統(tǒng)就會變慢呢?雖然它會把內存用于緩存,但是只要其它的地方需要,這些緩存就會被釋放,因為它們的優(yōu)先 ...





歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2