下邊是記憶體清理指令,把這個寫到一個linux shell的free.sh檔案中。要清理時運作一下就可以了。
#!/bin/bash
free -m
sync
echo 1 > /proc/sys/vm/drop_caches
echo 2 > /proc/sys/vm/drop_caches
echo 3 > /proc/sys/vm/drop_caches
free -m
對于想要自動清理記憶體的,可以寫一個python腳本,使用背景運作這個腳本,定時調用上邊的清理記憶體shell就可以了。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import os
while True:
10) #3小時=10800秒
'sh freem.sh').readlines()
for s in
print
上邊的是python調用系統指令的free.py檔案,把這個檔案和清理shell檔案放在同一個目錄。然後背景運作這個python腳本就可以了,上邊的代碼會每3小時清理一次記憶體,清理後的結果會輸出到标準輸出。
背景運作腳本的方法: