手动释放Linux服务器内存

系统 Linux
在服务器运行过程中,使用free -m查看服务器内存时,经常会发现free值很小,有些同学就会很紧张,总想采取一些措施,使得free值看起来高一点,心里就比较爽一点。其实,个人觉得这完全是图一时之快,没什么实质性用途。

在服务器运行过程中,使用free -m查看服务器内存时,经常会发现free值很小,有些同学就会很紧张,总想采取一些措施,使得free值看起来高一点,心里就比较爽一点。其实,个人觉得这完全是图一时之快,没什么实质性用途。

一、大众释放内存方法

1. 首先使用free -m查看剩余内存

view plaincopy to clipboardprint?

linux-8v2i:~ # free -m

total used free shared buffers cached

Mem: 3952 2773 178 0 130 1097

-/+ buffers/cache: 1545 2406

Swap: 2055 0 2055

linux-8v2i:~ # free -m

total used free shared buffers cached

Mem: 3952 2773 178 0 130 1097

-/+ buffers/cache: 1545 2406

Swap: 2055 0 2055

2. 执行sync命令

使用sync命令以确保文件系统的完整性,sync 命令运行 sync 子例程,将所有未写的系统缓冲区写到磁盘中,包含已修改的 i-node、已延迟的块 I/O 和读写映射文件。

view plaincopy to clipboardprint?

linux-8v2i:~ # sync

linux-8v2i:~ # sync

3. 修改/proc/sys/vm/drop_caches

view plaincopy to clipboardprint?

echo 3 > /proc/sys/vm/drop_caches

echo 3 > /proc/sys/vm/drop_caches

说明:

1>. /proc是一个虚拟文件系统,我们可以通过对它的读写操作作为与kernel实体间进行通信的一种手段。也就是说可以通过修改/proc中的文件,来对当前kernel的行为做出调整。也就是说我们可以通过调整/proc/sys/vm/drop_caches来释放内存。

2>. 关于drop_caches的官方说明如下:

Writing to this file causes the kernel to drop clean caches,dentries and inodes from memory, causing that memory to becomefree.

To free pagecache, use echo 1 > /proc/sys/vm/drop_caches;

to free dentries and inodes, use echo 2 > /proc/sys/vm/drop_caches;

to free pagecache, dentries and inodes, use echo 3 >/proc/sys/vm/drop_caches.

Because this is a non-destructive operation and dirty objects are not freeable, the user should run sync first.

3>. Linux内核会将它最近访问过的文件页面缓存在内存中一段时间,这个文件缓存被称为pagecache。

4.再使用free -m查看剩余内存,情况如下:

view plaincopy to clipboardprint?

linux-8v2i:~ # free -m

total used free shared buffers cached

Mem: 3952 1773 2325 0 0 80

-/+ buffers/cache: 1545 2406

Swap: 2055 0 2055

linux-8v2i:~ # free -m

total used free shared buffers cached

Mem: 3952 1773 2325 0 0 80

-/+ buffers/cache: 1545 2406

Swap: 2055 0 2055

二、Linux内存分析

1. 首先对free -m查看结果进行分析

view plaincopy to clipboardprint?

linux-8v2i:~ # free -m

total used free shared buffers cached

Mem: 3952 2773 178 0 130 1097

-/+ buffers/cache: 1545 2406

Swap: 2055 0 2055

linux-8v2i:~ # free -m

total used free shared buffers cached

Mem: 3952 2773 178 0 130 1097

-/+ buffers/cache: 1545 2406

Swap: 2055 0 2055

各参数含义:

total:总物理内存

used:已使用内存

free:完全未被使用的内存

shared:应用程序共享内存

buffers:缓存,主要用于目录方面,inode值等

cached:缓存,用于已打开的文件

-buffers/cache:应用程序使用的内存大小,used减去缓存值

+buffers/cache:所有可供应用程序使用的内存大小,free加上缓存值

其中:

total = used + free

-buffers/cache=used-buffers-cached,这个是应用程序真实使用的内存大小

+buffers/cache=free+buffers+cached,这个是服务器真实还可利用的内存大小

2. Linux的内存分配方式

大家都知道,Linux服务器为了提高效率,会提前申请内存,即使这些内存没有被具体应用使用,Linux也会提前申请这些内存,然后利用这些内存做缓存用,即将刚打开的文件系统存入cache中,这样对应的服务器free值会越来越少,buffers和cached会越来越大,因此给大家表象就是内存越来越少了,大家就紧张了;其实,大家完全不用紧张,Linux服务器在发现内存不足时,会自动清理cached区域,释放内存,然后继续增大cache,free继续减少。因此,那样手动降低内存使用率的方法,其实就是图一时之快,呵呵。

【编辑推荐】

  1. 如何保持Linux服务器间的文件同步
  2. 攻防有道 剖析对Linux服务器的攻击
  3. Linux服务器被黑 知识点全面讲解
责任编辑:赵宁宁 来源: 天极网
相关推荐

2010-03-05 13:59:16

Linux服务器内存

2020-06-22 08:30:42

Linux内存手动释放

2010-03-05 14:10:39

Linux服务器内存

2009-04-14 18:41:21

Nehalem服务器金士顿

2012-09-13 15:37:21

linux内存

2009-10-28 10:31:13

2013-07-04 11:24:55

内存监控内存转储

2020-08-13 11:35:52

Linuxswapbuffer

2010-09-07 14:31:37

服务器升级硬件篇

2013-12-06 11:00:16

Linux服务器Unix服务器内存

2015-05-29 13:22:10

Linux挂载运维

2020-02-07 15:42:52

服务器内存显存

2018-08-17 08:44:37

服务器内存排查

2010-10-22 13:56:41

SQL Server服

2022-05-05 09:27:31

Linux服务器优化

2014-08-06 11:25:00

LinuxSVN服务器

2023-10-18 13:31:00

Linux内存

2017-04-17 14:40:11

虚拟化服务器内存

2017-03-28 15:30:51

服务器虚拟化内存

2024-02-29 09:28:19

点赞
收藏

51CTO技术栈公众号