Free

free - Display amount of free and used memory in the system

free displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel.

The shared memory column should be ignored; it is obsolete.

free [-b|-k|-m|-g] [-c count] [-l] [-o] [-t] [-s delay] [-V]

Options

-V 显示版本号码

procps 软件包包含了vmstat, free等一些工具:

justin@Justin:~$ free -V
procps version 3.2.8

[-b|-k|-m|-g] 控制显示单位

默认是 -k

-s -c 重复显示

no options

justin@Justin:~$ free
             total       used       free     shared    buffers     cached
Mem:       1010128     995372      14756          0      45612     126860
-/+ buffers/cache:     822900     187228
Swap:       979956      49072     930884

Low Free Memory is not always bad thing:

It can be discouraging to see very little free memory in a system because of the cache and buffer usage, but this is not necessarily a bad thing.

By default, Linux tries to use as much of your memory as possible. This is good. If Linux detects any free memory, it caches applications and data in the free memory to speed up future accesses. Because it is usually a few orders of magnitude faster to access things from memory rather than disk, this can dramatically improve overall performance. When the system needs the cache memory for more important things, the cache memory is erased and given to the system. Subsequent access to the object that was previously cached has to go out to disk to be filled.

total = used + free

can_be_used = free + buffer + cache

第2行: -/+ buffers/cache的意思相当于: -buffers/cache 的内存数:1397032 (等于第1行的 used - buffers - cached) +buffers/cache 的内存数: 2752124 (等于第1行的 free + buffers + cached)

Metrics

can_be_used / total mem (physical) > 70% (enough)

can_be_used / total mem (physical) < 20% (lack of memory)

can_be_used / total mem (physical) > 20% && < 70% (不影响系统性能)

-l

             total       used       free     shared    buffers     cached
Mem:       1010128     997720      12408          0      43040     117964
Low:        891400     879096      12304
High:       118728     118624        104
-/+ buffers/cache:     836716     173412
Swap:       979956      51520     928436

Low+High=Mem

Low vs High Memory

Low The total amount of low memory or memory directly accessible by the kernel.

High The total amount of high memory or memory not directly accessible by the kernel.

High Versus Low Memory ===========================

For 64bit processor it does not matter because they can directly address additional memory that is available in current system

For 32-bit processors (for example, IA32) with 1 GB or more of physical of memory, Linux must manage the physical memory as high and low memory. The high memory is not directly accessible by the Linux kernel and must be mapped into the low-memory range before it can be used.

15.1.3. High and Low Memory

The difference between logical and kernel virtual addresses is highlighted on 32-bit systems that are equipped with large amounts of memory. With 32 bits, it is possible to address 4 GB of memory. Linux on 32-bit systems has, until recently, been limited to substantially less memory than that, however, because of the way it sets up the virtual address space.

The kernel (on the x86 architecture, in the default configuration) splits the 4-GB virtual address space between user-space and the kernel; the same set of mappings is used in both contexts. A typical split dedicates 3 GB to user space, and 1 GB for kernel space.1 The kernel's code and data structures must fit into that space, but the biggest consumer of kernel address space is virtual mappings for physical memory. The kernel cannot directly manipulate memory that is not mapped into the kernel's address space. The kernel, in other words, needs its own virtual address for any memory it must touch directly. Thus, for many years, the maximum amount of physical memory that could be handled by the kernel was the amount that could be mapped into the kernel's portion of the virtual address space, minus the space needed for the kernel code itself. As a result, x86-based Linux systems could work with a maximum of a little under 1 GB of physical memory.

1 Many non-x86 architectures are able to efficiently do without the kernel/user-space split described here, so they can work with up to a 4-GB kernel address space on 32-bit systems. The constraints described in this section still apply to such systems when more than 4 GB of memory are installed, however.

In response to commercial pressure to support more memory while not breaking 32-bit application and the system's compatibility, the processor manufacturers have added "address extension" features to their products. The result is that, in many cases, even 32-bit processors can address more than 4 GB of physical memory. The limitation on how much memory can be directly mapped with logical addresses remains, however. Only the lowest portion of memory (up to 1 or 2 GB, depending on the hardware and the kernel configuration) has logical addresses;2 the rest (high memory) does not. Before accessing a specific high-memory page, the kernel must set up an explicit virtual mapping to make that page available in the kernel's address space. Thus, many kernel data structures must be placed in low memory; high memory tends to be reserved for user-space process pages.

2 The 2.6 kernel (with an added patch) can support a "4G/4G" mode on x86 hardware, which enables larger kernel and user virtual address spaces at a mild performance cost.

The term "high memory" can be confusing to some, especially since it has other meanings in the PC world. So, to make things clear, we'll define the terms here:

Low memory

Memory for which logical addresses exist in kernel space. On almost every system you will likely encounter, all memory is low memory. High memory

Memory for which logical addresses do not exist, because it is beyond the address range set aside for kernel virtual addresses.

On i386 systems, the boundary between low and high memory is usually set at just under 1 GB, although that boundary can be changed at kernel configuration time. This boundary is not related in any way to the old 640 KB limit found on the original PC, and its placement is not dictated by the hardware. It is, instead, a limit set by the kernel itself as it splits the 32-bit address space between kernel and user space.

We will point out limitations on the use of high memory as we come to them in this chapter.

-o

justin@Justin:~$ free -o
             total       used       free     shared    buffers     cached
Mem:       1010128     993748      16380          0      45012     122336
Swap:       979956      50008     929948

-t

             total       used       free     shared    buffers     cached
Mem:       1010128     993252      16876          0      44960     122336
-/+ buffers/cache:     825956     184172
Swap:       979956      50008     929948
Total:     1990084    1043260     946824

如果感兴趣可以进一步参考文件/proc/meminfo,free命令就是根据它 的信息生成的。free命令的源码可从procps-xxx-.src.rpm获取,xxx 为版本号,比如procps-3.2.3-5.3.src.rpm。

FILES

/proc/meminfo memory information

Wiki comments powered by Disqus