Skip to main content

PAGECACHE、DENTRIES、INODES

Kiga-Hub
Linux Linux
KIGA
Author
KIGA
This is a personal blog, intended for sharing.
Table of Contents

/proc
#

/proc 是一个虚拟文件系统,通过对它的读写操作做为与Kernel实体间进行通信的一种手段 通过修改/proc中的文件,来对当前kernel的行为作出调整

  • 调整/proc/sys/vm/drop_caches 释放内存

Linux 内存使用
#

Linux 在内存管理方面做的非常的好,系统在运行一段时间后,会将暂时用不到的内存转为 buff/cache,这样在程序使用到这一部分数据时,能够很快的取出,从而提高系统的运行效率。如果你看到服务器内存剩余的非常少,不必担心,真正剩余的内存是 free+buff/cache的和才是实际可用的内存。如果应用程序需要内存空间时,Linux 会将缓存让出给程序使用,从而使内存达到最大化的利用率

当大量的缓存占用内存空间时,应用程序就会使用到sawp交换分区,这样会使得系统的运行变慢,从而影响整体运行效率

  • Swap 是指交换分区,当可用内存少于额定值的时候,就会开会进行交换 所以,这个时候我们需要手动去释放内存,释放内存的时候

sync
#

sync命令运行sync子例程,如果必须停止系统,则运行sync命令以确保文件系统的完整性 sync将所有未写的系统缓存写到磁盘中,包含已修改的inode、已延迟的块I/O和读写映射文件

PAGECACHE/DENTRIES/INODES
#

inode 文件的数据结构 dentries 目录的数据结构 pagecache 页面缓存

  • It appears you are working with memory caching of directory structures
  • An inode in your context is a data structure that represents a file. A dentries is a data structure that represents a directory
  • hese structures could be used to build a memory cache that represents the file structure on a disk. To get a directly listing, the OS could go to the dentries–if the directory is there–list its contents (a series of inodes). If not there, go to the disk and read it into memory so that it can be used again。这些结构可用于构建表示磁盘上的文件结构的内存高速缓存。为了直接获得列表,操作系统可以去dentries那里(如果目录在那里的话)列出其内容(一系列inode)。如果没有,则会去磁盘上将其读入内存,以便它可以再次使用
  • The page cache could contain any memory mappings to blocks on disk. That could conceivably be buffered I/O, memory mapped files, paged areas of executables–anything that the OS could hold in memory from a file。页面缓存(page cache)可以包含磁盘块的任何内存映射。这可以是缓冲I/O,内存映射文件,可执行文件的分页区域——操作系统可以从文件保存在内存中的任何内容

简单分析内存使用情况
#

项目中如果有内存不足、应用获取不到可用内存、OOM错误问题,从swap的使用情况快速判断,应用在系统上稳定运行,free值也会保持在一个稳定值的 通过快速清空buffer,强制腾出free大小,暂时把问题从系统层面脱离,更有效地去分析应用层面的原因(应用使用内存的情况)

#To free pagecache:
echo 1 > /proc/sys/vm/drop_caches
#To free dentries and inodes:
echo 2 > /proc/sys/vm/drop_caches
#To free pagecache, dentries and inodes:
echo 3 > /proc/sys/vm/drop_caches

手动释放PAGECACHE/DENTRIES/INODES

执行echo 1 > /proc/sys/vm/drop_caches

free -m

              total        used        free      shared  buff/cache   available
Mem:          10531        2499        7318          60         713        8170
Swap:          2047           0        2047

可以看到free值 及buff/cache值的明显变化

系统Mem额定信息
#

cat /proc/meminfo

MemTotal:       10784492 kB
MemFree:         5035376 kB
MemAvailable:    8370408 kB
Buffers:          331192 kB
Cached:          2554148 kB
SwapCached:            0 kB
Active:          2995952 kB
Inactive:        1990264 kB
Active(anon):    1586000 kB
Inactive(anon):    59392 kB
Active(file):    1409952 kB
Inactive(file):  1930872 kB
Unevictable:       14260 kB
Mlocked:           14260 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Dirty:               168 kB
Writeback:             0 kB
AnonPages:       2115184 kB
Mapped:           463988 kB
Shmem:             61888 kB
Slab:             467136 kB
SReclaimable:     308684 kB
SUnreclaim:       158452 kB
KernelStack:       15616 kB
PageTables:        52888 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     7489392 kB
Committed_AS:    8503784 kB
VmallocTotal:   34359738367 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:      333632 kB
DirectMap2M:     8640512 kB
DirectMap1G:     2097152 kB