zoukankan      html  css  js  c++  java
  • linux内存清理步骤与脚本

    linux 内存优化

     

    linux 内存优化:

    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。

    手动释放linux内存cache和脚本定时释放:freemem.sh

    #!/bin/bash
    
    used=`free -m | awk 'NR==2' | awk '{print $3}'`
    free=`free -m | awk 'NR==2' | awk '{print $4}'`
    
    echo "===========================" >> /var/log/mem.log
    date >> /var/log/mem.log
    echo "Memory usage | [Use:${used}MB][Free:${free}MB]" >> /var/log/mem.log
    
    if [ $free -le 100 ] ; then
                    sync && echo 1 > /proc/sys/vm/drop_caches
                    sync && echo 2 > /proc/sys/vm/drop_caches
                    sync && echo 3 > /proc/sys/vm/drop_caches
                    echo "OK" >> /var/log/mem.log
    else
                    echo "Not required" >> /var/log/mem.log

    将脚本添加到crond任务,定时执行。

    脚本说明:sync命令是把脏数据写到磁盘,也就是把内存中的数据写入磁盘,避免丢失。

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

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

    echo "*/30 * * * * root /root/satools/freemem.sh" >> /etc/crondtab

  • 相关阅读:
    Linux中grep命令的12个实践例子
    进程调度函数schedule()分析
    spin_lock、spin_lock_irq、spin_lock_irqsave区别
    耳机接电话挂断功能
    英文月份
    Linux 学习之路:认识shell和bash
    Solution:Cannot pull with rebase: You have unstaged changes in Github
    Selenium Webdriver——操作隐藏的元素(四)
    Selenium Webdriver——操作隐藏的元素(三)switchTo().frame()
    selenium webdriver处理HTML5 的视频播放
  • 原文地址:https://www.cnblogs.com/zcyy/p/15668527.html
Copyright © 2011-2022 走看看