默认chrome缓存位置在~/.cache/google-chrome中,磁盘io很多。为减少磁盘io,保护磁盘同时加快chrome速度,可设置缓存使用内存盘,缺点是重启后缓存丢失,所以这里可以使用脚本进行同步。

https://www.cnblogs.com/dylanchu/p/9750494.html

1. 开机自动挂载内存盘 (使用/dev/shm, 无需自己创建内存盘)

  1. sudo mkdir /ramdisk
  2. sudo chmod 777 /ramdisk
  3. sudo vim /etc/fstab, 添加如下内容:
# ramdisk
none /ramdisk tmpfs nodev,nosuid,noatime,mode=1777,size=512M 0 0

可使用 df -h 命令查看 /ramdisk 虚拟分区大小。

2. 缓存同步(打包解包)脚本

首先需要安装 tar 的 lzop 插件,然后建立核心脚本:

mkdir -p /home/dylanchu/scripts/chrome
touch /home/dylanchu/scripts/chrome/chromecache
chmod +x /home/dylanchu/scripts/chrome/chromecache
vim /home/dylanchu/scripts/chrome/chromecache

内容如下:

#!/usr/bin/sh

# invoke this after reboot and before shutdown
# make sure that you already have 'lzop' installed on your system case "$1" in
import)
cd /dev/shm
tar --lzop -pxf /home/dylanchu/.cache/chromecache-backup.tar.lzop
;;
dump)
cd /dev/shm
# delete files larger than 3MB
find ./google-chrome/ -size +3M -exec rm {} \;
tar --lzop -pcf /home/dylanchu/.cache/chromecache-backup.tar.lzop google-chrome/
;;
*)
echo -e "Usage: $(cd `dirname $0`; pwd)/chromecache {import|dump}"
exit 1
;;
esac exit 0

3. 开机导入脚本

开机时设置缓存路径,及从压缩包导入缓存

touch /home/dylanchu/scripts/chrome/onboot.sh
chmod +x /home/dylanchu/scripts/chrome/onboot.sh
vim /home/dylanchu/scripts/chrome/onboot.sh

内容如下:

#!/bin/sh

#for the google chrome cache
/bin/rm ~/.cache/google-chrome -R
/bin/mkdir -p /dev/shm/google-chrome
/bin/ln -sf /dev/shm/google-chrome ~/.cache/google-chrome #for the chromium cache
#/bin/rm ~/.cache/chromium
#/bin/mkdir -p /dev/shm/chromium
#/bin/ln -sf /dev/shm/chromium ~/.cache/chromium # import dumped cache file to ram:
echo [`date +"%Y-%m-%d %H:%M"`] On boot - Importing caches to ram >> /home/dylanchu/chromecache_sync.log
/home/dylanchu/scripts/chrome/chromecache import >> /home/dylanchu/chromecache_sync.log
echo [`date +"%Y-%m-%d %H:%M"`] On boot - Caches imported to ram >> /home/dylanchu/chromecache_sync.log

添加上述 onboot.sh 脚本到开机自启动:

这里用xfce gui的 “会话和启动” (session-settings),点击添加,并设置名称和脚本路径。

(也可使用crontab的@reboot执行)

4. 关机前导出缓存到硬盘

关机前需要执行的脚本

touch /home/dylanchu/scripts/chrome/onshutdown.sh
chmod +x /home/dylanchu/scripts/chrome/onshutdown.sh
vim /home/dylanchu/scripts/chrome/onshutdown.sh

内容如下:

#!/bin/sh

# dump cache files from ram to disk:
echo [`date +"%Y-%m-%d %H:%M"`] On shutdown - Dumping caches to disk >> /home/dylanchu/chromecache_sync.log
/home/dylanchu/scripts/chrome/chromecache dump >> /home/dylanchu/chromecache_sync.log
echo [`date +"%Y-%m-%d %H:%M"`] On shutdown - Caches dumped to disk >> /home/dylanchu/chromecache_sync.log
ping -c 3 127.1 > /dev/null

让 systemd 在关机时自动执行上述脚本

sudo vim /lib/systemd/system/chromedumpcache.service

内容如下:(测试无效)

[Unit]
Description=Dump chrome caches from ram to disk at shutdown.
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target [Service]
Type=simple
RemainAfterExit=true
# when system start
ExecStart=/bin/true
# when system shutdown
ExecStop=/home/dylanchu/scripts/chrome/onshutdown.sh [Install]
WantedBy=multi-user.target halt.target reboot.target shutdown.target

修改:

​ multi-user.target是字符界面,改为graphical.target后正常工作:(测试发现仅关机和重启时工作)

[Unit]
Description=Dump chrome caches to disk
DefaultDependencies=no
Before=umount.target shutdown.target reboot.target halt.target [Service]
Type=simple
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/home/dylanchu/scripts/chrome/onshutdown.sh [Install]
WantedBy=graphical.target

在 systemd 注册之:

sudo systemctl enable chromedumpcache.service
sudo systemctl daemon-reload
sudo systemctl status chromedumpcache.service

systemctl get-default 命令可以查看系统启动默认进入哪个界面

5. 重启生效。


参考:

https://wiki.archlinux.org/index.php/Tmpfs

https://wiki.archlinux.org/index.php/Chromium/Tips_and_tricks#Cache_in_tmpfs

内存盘和硬盘同步:

http://docs.observium.org/persistent_ramdisk/

https://askubuntu.com/questions/416299/execute-command-before-shutdown-reboot(用systemd关机前执行指令)

http://blog.csdn.net/kai165416/article/details/79449638 (删除大于固定大小的文件)

https://askubuntu.com/questions/794290/create-ramdisk-on-16-04-for-chrome

https://www.omgubuntu.co.uk/2010/11/move-google-chrome-cache-to-ramdisk

http://www.bubuko.com/infodetail-1900178.html

http://blog.51cto.com/10237569/1871723

https://superuser.com/questions/1016827/how-do-i-run-a-script-before-everything-else-on-shutdown-with-systemd

http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html

https://www.freedesktop.org/software/systemd/man/bootup.html#System Manager Bootup (systemd 启动顺序)

Linux设置chrome缓存至内存,及开关机同步的更多相关文章

  1. linux下的缓存机制及清理buffer/cache/swap的方法梳理 (转)

    一.缓存机制介绍 在Linux系统中,为了提高文件系统性能,内核利用一部分物理内存分配出缓冲区,用于缓存系统操作和数据文件,当内核收到读写的请求时,内核先去缓存区找是否有请求的数据,有就直接返回,如果 ...

  2. linux下的缓存机制buffer、cache、swap - 运维总结 ["Cannot allocate memory"问题]

    一.缓存机制介绍 在Linux系统中,为了提高文件系统性能,内核利用一部分物理内存分配出缓冲区,用于缓存系统操作和数据文件,当内核收到读写的请求时,内核先去缓存区找是否有请求的数据,有就直接返回,如果 ...

  3. linux下的缓存机制buffer、cache、swap

    一.缓存机制介绍 在Linux系统中,为了提高文件系统性能,内核利用一部分物理内存分配出缓冲区,用于缓存系统操作和数据文件,当内核收到读写的请求时,内核先去缓存区找是否有请求的数据,有就直接返回,如果 ...

  4. Linux高级调试与优化——内存管理

    1.物理地址和虚拟地址 Linux采用页表机制管理内存,32位系统中页大小一般为4KB,物理内存被划分为连续的页,每一个页都有一个唯一的页号. 为了程序的的可移植性,进程往往需要运行在flat mem ...

  5. 真香!Linux 原来是这么管理内存的

    Linux 内存管理模型非常直接明了,因为 Linux 的这种机制使其具有可移植性并且能够在内存管理单元相差不大的机器下实现 Linux,下面我们就来认识一下 Linux 内存管理是如何实现的. 基本 ...

  6. Linux页快速缓存与回写机制分析

    參考 <Linux内核设计与实现> ******************************************* 页快速缓存是linux内核实现的一种主要磁盘缓存,它主要用来降低 ...

  7. linux下的CPU、内存、IO、网络的压力测试

    linux下的CPU.内存.IO.网络的压力测试  要远程测试其实很简单了,把结果放到一个微服务里直接在web里查看就可以了,或者同步到其他服务器上 一.对CPU进行简单测试: 1.通过bc命令计算特 ...

  8. Linux内核入门到放弃-内存管理-《深入Linux内核架构》笔记

    概述 内存管理的实现涵盖了许多领域: 内存中的物理内存页管理 分配大块内存的伙伴系统 分配较小内存块的slab.slub和slob分配器 分配非连续内存块的vmalloc机制 进程的地址空间 在IA- ...

  9. [转帖]linux下的CPU、内存、IO、网络的压力测试

    linux下的CPU.内存.IO.网络的压力测试 https://www.cnblogs.com/zhuochong/p/10185881.html 一.对CPU进行简单测试: 1.通过bc命令计算特 ...

随机推荐

  1. redux和react-redux

    redux和react-redux的关系: redux是react的状态管理工具,却不仅仅只是为了react而生的,所以在使用中会存在痛点.而react-redux是专门为了react定制,目的是为了 ...

  2. LeetCode Array Easy 414. Third Maximum Number

    Description Given a non-empty array of integers, return the third maximum number in this array. If i ...

  3. React 全新的 Context API

    Context API 可以说是 React 中最有趣的一个特性了.一方面很多流行的框架(例如react-redux.mobx-react.react-router等)都在使用它:另一方面官方文档中却 ...

  4. numpy 中的broadcast 机制

    https://www.cnblogs.com/jiaxin359/p/9021726.html

  5. 转载:Spring中各个JAR包的作用

    (1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工 ...

  6. 【leetcode】993. Cousins in Binary Tree

    题目如下: In a binary tree, the root node is at depth 0, and children of each depth k node are at depth  ...

  7. shell变量,字符串,数组

    一.shell变量定义: 命名只能使用英文字母,数字和下划线,首个字符不能以数字开头: 中间不能有空格,可以使用下划线: 不能使用标点符号: 不能使用bash里的关键字. 变量使用: 使用一个定义过的 ...

  8. java中循环删除list中元素的方法

    重点哈 印象中循环删除list中的元素使用for循环的方式是有问题的,但是可以使用增强的for循环,然后今天在使用时发现报错了,然后去科普了一下,再然后发现这是一个误区.下面就来讲一讲..伸手党可直接 ...

  9. Base64和3DES算法

    Base64加密算法 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,可用于在HTTP环境下传递较长的标识信息.它的优点是算法效率高,编码出来的结果比较简短,同时也具有不可读性. ...

  10. Android 模糊搜索rawquery bind or column index out of range: handle 0x2fb180 报错

    做模糊搜索时,出现了  bind or column index out of range: handle 0x2fb180 报错 public Cursor getTitle(String word ...