说明
安装应用过多,使用过程中会导致内存不足,杀掉系统进程,可以通过free -h命令判断当前剩余内存,杀掉部分进程来解决内存紧张问题。
脚本实现
#!/bin/sh
while true ;do
switch_value=`getprop persist.mem`
echo -e "flag:$switch_value"
if [ $switch_value -eq 1 ]; then
total=`free -m | sed -n '2p' | awk '{print $2}'`
used=`free -m | sed -n '2p' | awk '{print $3}'`
free=`free -m | sed -n '2p' | awk '{print $4}'`
shared=`free -m | sed -n '2p' | awk '{print $5}'`
buffers=`free -m | sed -n '2p' | awk '{print $6}'`
cacheused=`free -m | sed -n '3p' | awk '{print $3}'`
cachefree=`free -m | sed -n '3p' | awk '{print $4}'`
totalcache=`free -m | sed -n '3p' | awk '{print $3+$4}'`
ratecache=`free -m | sed -n '3p' | awk '{print $3/($3+$4)*100}'`
echo -e "total\tused\tfree\tshared\tbuffers\tcacheused\tcachefree\tratecache"
echo -e "$total\t$used\t$free\t$shared\t$buffers\t$cacheused\t\t$cachefree\t\t$ratecache"
rate=`echo ${ratecache%.*}`
echo -e "rate:$rate"
if [ $rate -ge 90 ];then
echo "Memory Warn"
kill -9 $(pidof com.oushang.screen)
kill -9 $(pidof com.oushang.qqmusic)
kill -9 $(pidof com.oushang.changba)
kill -9 $(pidof com.oushang.kugouchangchang)
kill -9 $(pidof com.oushang.radio)
kill -9 $(pidof com.oushang.iqiyi)
kill -9 $(pidof com.oushang.weather)
kill -9 $(pidof com.oushang.uploadservice)
kill -9 $(pidof cn.loopon.app.babykgo)
kill -9 $(pidof cn.loopon.app.server)
kill -9 $(pidof cn.loopon.app.refuel)
kill -9 $(pidof cn.loopon.app.travel)
kill -9 $(pidof com.changan.os.wechatsendcar)
kill -9 $(pidof cn.com.os.changan.appstore)
kill -9 $(pidof com.incall.apps.softmanager)
kill -9 $(pidof com.os.airconditioner)
kill -9 $(pidof com.Oshan.Fitness)
kill -9 $(pidof com.changan.oushangCos1)
kill -9 $(pidof com.chinatsp.smartlights)
kill -9 $(pidof com.chinatsp.smartlights:lights_server)
kill -9 $(pidof com.chinatsp.drivercenter)
kill -9 $(pidof com.chinatsp.threescreenmiddleservice)
kill -9 $(pidof com.chinatsp.outsidecarvoice)
kill -9 $(pidof com.chinatsp.filemanager)
kill -9 $(pidof com.chinatsp.update)
kill -9 $(pidof com.chinatsp.dlna)
kill -9 $(pidof com.chinatsp.dashboard)
kill -9 $(pidof com.chinatsp.face)
kill -9 $(pidof com.chinatsp.gesturerecognition)
kill -9 $(pidof com.chinatsp.eaglepilot)
kill -9 $(pidof com.chinatsp.voicecamera)
kill -9 $(pidof com.chinatsp.servicemall)
kill -9 $(pidof com.chinatsp.music.kgsupport)
kill -9 $(pidof com.chinatsp.music)
kill -9 $(pidof com.chinatsp.onecall)
kill -9 $(pidof com.chinatsp.dvrcamera)
kill -9 $(pidof com.chinatsp.phone)
kill -9 $(pidof com.chinatsp.settings)
echo 3 >/proc/sys/vm/drop_caches
sleep 1m
elif [ $rate -ge 95 ];then
kill -9 $(pidof com.android.car)
kill -9 $(pidof com.tencent.wecarnavi)
kill -9 $(pidof com.chinatsp.ifly)
kill -9 $(pidof video.evs.camera:renderservice)
kill -9 $(pidof video.evs.camera)
kill -9 $(pidof evs_app)
kill -9 $(pidof evsrenderengine)
echo 3 >/proc/sys/vm/drop_caches
sleep 1m
else
echo 'It is OK'
fi
else
echo 'persis.mem is 0'
fi
sleep 1
done
创建服务和启动服务
在init.rc中实现如下代码:
service mem /system/bin/mem.sh
user shell
class core
disabled
oneshot
group root shell
seclabel u:r:shell:s0
#启动服务
on property:sys.boot_completed=1
bootchart stop
start mem
编译文件:
/ device/mediateksample/spm8666p1_64/full_spm8666p1_64.mk
PRODUCT_COPY_FILES += \
device/mediateksample/spm8666p1_64/sepolicy/delete_gd_data.sh:system/bin/delete_gd_data.sh \
device/mediateksample/spm8666p1_64/sepolicy/delete_data_update.sh:system/bin/delete_data_update.sh \
device/mediateksample/spm8666p1_64/sepolicy/cat_rt_data.sh:system/bin/cat_rt_data.sh \
device/mediatek/mt6771/mem.sh:system/bin/mem.sh
/ device/mediateksample/spm8666p1_64_car/full_spm8666p1_64_car.mk
PRODUCT_COPY_FILES += \
device/mediateksample/spm8666p1_64_car/sepolicy/delete_gd_data.sh:system/bin/delete_gd_data.sh \
device/mediateksample/spm8666p1_64_car/sepolicy/delete_data_update.sh:system/bin/delete_data_update.sh \
device/mediateksample/spm8666p1_64_car/sepolicy/cat_rt_data.sh:system/bin/cat_rt_data.sh \
device/mediateksample/spm8666p1_64_car/sepolicy/bluetoothkey.sh:system/bin/bluetoothkey.sh \
device/mediatek/mt6771/mem.sh:system/bin/mem.sh