├── .editorconfig ├── .gitignore ├── .idea ├── .gitignore ├── BGAppRetention.iml ├── codeStyles │ ├── Project.xml │ └── codeStyleConfig.xml ├── inspectionProfiles │ └── profiles_settings.xml ├── modules.xml └── vcs.xml ├── AppRetention.apk ├── HChen.json ├── META-INF └── com │ └── google │ └── android │ ├── update-binary │ └── updater-script ├── README-en.md ├── README.md ├── Update.md ├── customize.sh ├── memory.sh ├── module.prop ├── service.sh ├── swap.ini └── system.prop /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | indent_size = 4 7 | indent_style = space 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.xml] 12 | insert_final_newline = false 13 | 14 | [*.yml] 15 | indent_size = 2 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /.idea/.* -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/BGAppRetention.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 116 | 117 | -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /AppRetention.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HChenX/MemoryOpt/737916100a96b0151705adb3c94f8cc31bdf7e27/AppRetention.apk -------------------------------------------------------------------------------- /HChen.json: -------------------------------------------------------------------------------- 1 | { 2 | "versionCode": 2025022800, 3 | "version": "2025.02.28", 4 | "zipUrl": "https://github.com/HChenX/MemoryOpt/releases/download/v.2025.02.28/MemoryOpt.zip", 5 | "changelog": "https://raw.githubusercontent.com/HChenX/MemoryOpt/main/Update.md" 6 | } 7 | -------------------------------------------------------------------------------- /META-INF/com/google/android/update-binary: -------------------------------------------------------------------------------- 1 | #!/sbin/sh 2 | 3 | ################# 4 | # Initialization 5 | ################# 6 | 7 | umask 022 8 | 9 | # echo before loading util_functions 10 | ui_print() { echo "$1"; } 11 | 12 | require_new_magisk() { 13 | ui_print "*******************************" 14 | ui_print " Please install Magisk v20.4+! " 15 | ui_print "*******************************" 16 | exit 1 17 | } 18 | 19 | ######################### 20 | # Load util_functions.sh 21 | ######################### 22 | 23 | OUTFD=$2 24 | ZIPFILE=$3 25 | 26 | mount /data 2>/dev/null 27 | 28 | [ -f /data/adb/magisk/util_functions.sh ] || require_new_magisk 29 | . /data/adb/magisk/util_functions.sh 30 | [ $MAGISK_VER_CODE -lt 20400 ] && require_new_magisk 31 | 32 | install_module 33 | exit 0 34 | -------------------------------------------------------------------------------- /META-INF/com/google/android/updater-script: -------------------------------------------------------------------------------- 1 | #MAGISK 2 | -------------------------------------------------------------------------------- /README-en.md: -------------------------------------------------------------------------------- 1 |
2 |

MemoryOpt

3 | 4 | ![stars](https://img.shields.io/github/stars/HChenX/BGAppRetention?style=flat) 5 | ![downloads](https://img.shields.io/github/downloads/HChenX/BGAppRetention/total) 6 | ![Github repo size](https://img.shields.io/github/repo-size/HChenX/BGAppRetention) 7 | [![GitHub release (latest by date)](https://img.shields.io/github/v/release/HChenX/BGAppRetention)](https://github.com/HChenX/BGAppRetention/releases) 8 | [![GitHub Release Date](https://img.shields.io/github/release-date/HChenX/BGAppRetention)](https://github.com/HChenX/BGAppRetention/releases) 9 | ![last commit](https://img.shields.io/github/last-commit/HChenX/BGAppRetention?style=flat) 10 | ![language](https://img.shields.io/badge/language-shell-purple) 11 | 12 |

简体中文 | English

13 |

Optimize and adjust memory parameters

14 |
15 | 16 | # ✨introduction to memory parameter module: 17 | 18 | ### 💡Module source: 19 | 20 | - Module author: `焕晨HChen`。 21 | 22 | ### 🌟Module principle: 23 | 24 | - Modules achieve more efficient memory usage by adjusting memory parameters. 25 | - Use shell commands and other methods to modify zarm size, vm parameters, lmk parameters, etc. 26 | 27 | ### 💫The module contains content: 28 | 29 | - Memory parameter modification and optimization. 30 | - Additional Lsp module `AppRetentionHook`。 31 | - #### Note: Additional Module Author: 32 | `焕晨HChen`,For detailed module content, please refer to: [AppRetentionHook](https://github.com/HChenX/AppRetentionHook)。 33 | 34 | ### 🔥Module effect: 35 | 36 | - Optimize and improve memory usage efficiency. 37 | - It is recommended to cooperate with the Lsp module `AppRetention` to achieve better results. 38 | 39 | # 👑Module author information: 40 | 41 | ### ⭐Module author: 42 | 43 | | Module author | Module author name | 44 | |:------------------------:|:------------------:| 45 | | Module author | 焕晨HChen | 46 | | Additional Module Author | 焕晨HChen | 47 | 48 | ## 🎉Conclusion: 49 | 50 | ### 💕Acknowledgement Statement: 51 | 52 | - Thank you all for your support. Without your support module, we would not have been able to get to 53 | where we are now. Thank you very much! 54 | - This module code may have borrowed some code from other modules. If there is any infringement, 55 | please contact me to delete it. 56 | 57 | ### 🎵Friendship link: 58 | 59 | - Github link for AppRetantionBook module: 60 | - [AppRetentionHook](https://github.com/HChenX/AppRetentionHook) 61 | 62 | ### 📢焕晨's communication group: 63 | 64 | - Telegram Communication Group: [Telegram](https://t.me/HChenX_Chat) 65 | - Telegram module distribution group: [Telegram](https://t.me/HChenX_Module) 66 | 67 | ### 💣Disclaimers: 68 | 69 | - Module flashing represents willingness to bear all consequences on one's own. 70 | - Please make your own judgment on whether to install the brush in! 71 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |

内存优化

3 | 4 | ![stars](https://img.shields.io/github/stars/HChenX/BGAppRetention?style=flat) 5 | ![downloads](https://img.shields.io/github/downloads/HChenX/BGAppRetention/total) 6 | ![Github repo size](https://img.shields.io/github/repo-size/HChenX/BGAppRetention) 7 | [![GitHub release (latest by date)](https://img.shields.io/github/v/release/HChenX/BGAppRetention)](https://github.com/HChenX/BGAppRetention/releases) 8 | [![GitHub Release Date](https://img.shields.io/github/release-date/HChenX/BGAppRetention)](https://github.com/HChenX/BGAppRetention/releases) 9 | ![last commit](https://img.shields.io/github/last-commit/HChenX/BGAppRetention?style=flat) 10 | ![language](https://img.shields.io/badge/language-shell-purple) 11 | 12 |

简体中文 | English

13 |

优化调整内存参数

14 |
15 | 16 | # ✨内存参数模块简介: 17 | 18 | ### 💡模块来源: 19 | 20 | - 模块作者:`焕晨HChen`。 21 | 22 | ### 🌟模块原理: 23 | 24 | - 模块通过调整内存参数来实现更高效率的内存使用。 25 | - 使用 Shell 命令等方式修改 zarm 大小、vm 参数、lmk 参数等。 26 | 27 | ### 💫模块包含内容: 28 | 29 | - 内存参数修改优化。 30 | - 附加 Lsp 模块 `AppRetentionHook`。 31 | - #### 注:附加模块作者: 32 | `焕晨HChen`,模块详细内容请查看:[AppRetentionHook](https://github.com/HChenX/AppRetentionHook)。 33 | 34 | ### 🔥模块效果: 35 | 36 | - 优化提升内存使用效率。 37 | - 建议配合 Lsp 模块 `AppRetention`,使其能够发挥更好的效果。 38 | 39 | # 👑模块作者信息: 40 | 41 | ### ⭐模块作者: 42 | 43 | | 模块作者 | 模块作者名 | 44 | |:------:|:-------:| 45 | | 模块作者 | 焕晨HChen | 46 | | 附加模块作者 | 焕晨HChen | 47 | 48 | ## 🎉结语: 49 | 50 | ### 💕致谢声明: 51 | 52 | - 感谢各位的支持,没有你们的支持模块不可能走到现在,非常感谢! 53 | - 本模块代码可能借鉴了部分其他模块的代码,所有若有侵权请联系我删除。 54 | 55 | ### 🎵友情链接: 56 | 57 | - AppRetentionHook 模块 Github 链接: 58 | - [AppRetentionHook](https://github.com/HChenX/AppRetentionHook) 59 | 60 | ### 📢焕晨的交流群: 61 | 62 | - 电报交流群: [Telegram](https://t.me/HChenX_Chat) 63 | - 电报模块分发群: [Telegram](https://t.me/HChenX_Module) 64 | 65 | ### 💣免责声明: 66 | 67 | - 模块刷入即代表愿意自行承担一切后果。 68 | - 请自行判断是否安装刷入! 69 | -------------------------------------------------------------------------------- /Update.md: -------------------------------------------------------------------------------- 1 | ## 更新日志: 2 | 3 | ### 版本号:内存优化 2025-02-28 4 | 5 | - 自动设置 ZRAM 大小为物理内存 +1。 6 | - Author: 焕晨HChen 7 | 8 | ## Update Info: 9 | 10 | ### Version: MemoryOpt v.2025-02-28 11 | 12 | - Automatically set ZRAM size to physical memory +1. 13 | - Author: 焕晨HChen 14 | -------------------------------------------------------------------------------- /customize.sh: -------------------------------------------------------------------------------- 1 | # Author: 焕晨HChen 2 | mShouldRemoveModuleNames=" 3 | zram_huanchen 4 | HChen_Zram 5 | MiniHChen 6 | swap_controller 7 | scene_swap_controller 8 | " 9 | 10 | main() { 11 | print "- [i]: 欢迎使用本模块 (///ω///)" 12 | print "- [i]: 正在安装本模块!< (ˉ^ˉ)> " 13 | print "--------------------------------------------" 14 | print "--------------------------------------------" 15 | 16 | removeModuleIfNeed 17 | installAppRetentionIfNeed 18 | initConfig 19 | initPermissions 20 | 21 | printLog "- [i]: 作者: 焕晨HChen" 22 | printLog "- [i]: 安装完成!All Down!" 23 | } 24 | 25 | # 删除冲突文件 26 | removeModuleIfNeed() { 27 | printLog "- [i]: 正在删除冲突文件!" 28 | 29 | for name in $mShouldRemoveModuleNames; do 30 | modulePath=$(findPath "$name") 31 | if [[ -n $modulePath && -d $modulePath ]]; then 32 | touch "$modulePath"/remove 33 | touch "$modulePath"/disable 34 | 35 | printLog "- [i]: 已卸载: #modulePath" 36 | fi 37 | done 38 | } 39 | 40 | # AppRetention 模块安装 41 | installAppRetentionIfNeed() { 42 | printLog "- [i]: AppRetention 模块,版本 v.5.2.2" 43 | printLog "- [i]: 模块作用: 通过 Hook 系统 kill 逻辑实现后台保活" 44 | printLog "- [i]: 模块作者: 焕晨HChen" 45 | # versionName=$(dumpsys package com.hchen.appretention | grep versionName | cut -f2 -d '=') 46 | versionCode=$(dumpsys package com.hchen.appretention | grep versionCode | cut -f2 -d '=' | cut -f1 -d ' ') 47 | # if [[ $versionName == "5.2.2" && $versionCode == "2025021500" ]]; then 48 | if [[ $versionCode -ge "2025021500" ]]; then 49 | printLog "- [i]: AppRetention 模块已经安装且最新!" 50 | rm -rf "$MODPATH"/AppRetention.apk 51 | else 52 | printLog "- [i]: 按音量上确认安装,按音量下取消安装!" 53 | if [[ $(volumeKeyListener) == 0 ]]; then 54 | unzip -o "$ZIPFILE" 'AppRetention.apk' -d /data/local/tmp/ &>/dev/null 55 | if [[ ! -f /data/local/tmp/AppRetention.apk ]]; then 56 | printLog "- [!]: 解压附加模块失败!无法进行安装!" 57 | printLog "- [!]: 将会跳过安装过程,您可手动解压模块安装!" 58 | return 59 | fi 60 | pm install -r /data/local/tmp/AppRetention.apk &>/dev/null 61 | rm -rf /data/local/tmp/AppRetention.apk 62 | rm -rf "$MODPATH"/AppRetention.apk 63 | printLog "- [i]: AppRetention 模块安装成功!" 64 | else 65 | printLog "- [!]: AppRetention 模块已取消安装!" 66 | rm -rf "$MODPATH"/AppRetention.apk 67 | fi 68 | fi 69 | } 70 | 71 | # 设置压缩模式 72 | initConfig() { 73 | compAlgorithm=$(getConfigValue algorithm) 74 | persist=$(getConfigValue persist) 75 | 76 | printLog "- [i]: 正在检测可用压缩模式!" 77 | if [[ $persist == "true" ]]; then 78 | algorithm=$compAlgorithm 79 | sed -i '6d' "$MODPATH/swap.ini" 80 | sed -i '5a '"persist=true"'' "$MODPATH/swap.ini" 81 | else 82 | result=$(cat /sys/block/zram0/comp_algorithm) 83 | zramModes=$(echo "$result" | sed 's/\[//g' | sed 's/]//g' | sed 's/ /\n/g') 84 | # 优先选择 lz4,如果不可用则选择其他模式 85 | if echo "$zramModes" | grep -q lz4; then 86 | algorithm=lz4 87 | elif echo "$zramModes" | grep -q zstd; then 88 | algorithm=zstd 89 | elif echo "$zramModes" | grep -q lzo-rle; then 90 | algorithm=lzo-rle 91 | else 92 | algorithm=lzo 93 | fi 94 | fi 95 | if [[ -z $algorithm ]]; then 96 | printLog "- [!]: 获取 zram 压缩模式失败!" 97 | exit 1 98 | else 99 | sed -i '2a '"algorithm=$algorithm"'' "$MODPATH/swap.ini" 100 | printLog "- [i]: 设置 zram 压缩算法为: $algorithm" 101 | fi 102 | 103 | # Changed: 放弃修改!! 104 | # printLog "- [i]: 正在检查手机品牌!" 105 | # printLog "- [i]: 你的手机品牌是: $(getprop ro.product.brand)" 106 | # if [[ $(getprop ro.product.brand) == "samsung" ]]; then 107 | # echo -n " 108 | #persist.sys.minfree_12g=1,1,1,1,1,1 109 | #persist.sys.minfree_6g=1,1,1,1,1,1 110 | #persist.sys.minfree_8g=1,1,1,1,1,1 111 | #persist.sys.minfree_def=1,1,1,1,1,1 112 | #ro.slmk.2nd.dha_cached_max=2147483647 113 | #ro.slmk.dha_cached_max=2147483647 114 | #ro.slmk.dha_empty_max=2147483647 115 | #ro.slmk.2nd.dha_lmk_scale=-1 116 | #ro.slmk.dha_lmk_scale=-1 117 | #ro.slmk.dha_lmk_scale=-1 118 | #ro.slmk.2nd.swap_free_low_percentage=0 119 | #ro.slmk.swap_free_low_percentage=0 120 | #ro.slmk.cam_dha_ver=0 121 | #ro.slmk.chimera_quota_enable=false 122 | #ro.slmk.dha_2ndprop_thMB=1 123 | #ro.slmk.enable_upgrade_criad=false 124 | #ro.slmk.genai_reclaim_mode=false 125 | #ro.sys.kernelmemory.gmr.enabled=false 126 | #ro.sys.kernelmemory.umr.enabled=false 127 | #ro.sys.kernelmemory.umr.mem_free_low_threshold_kb=1 128 | #ro.sys.kernelmemory.umr.proactive_reclaim_battery_threshold=0 129 | #ro.sys.kernelmemory.umr.reclaimer.damon.enabled=false 130 | #ro.sys.kernelmemory.umr.reclaimer.onTrim.enabled=false" >>"$MODPATH"/system.prop 131 | # printLog "- [i]:已为三星添加专属PROP修改!" 132 | # fi 133 | } 134 | 135 | initPermissions() { 136 | set_perm_recursive "$MODPATH" 0 0 0777 0777 137 | } 138 | 139 | printLog() { 140 | echo "$@" 141 | sleep "$(echo "scale=3; $RANDOM/32768*0.2" | bc -l)" 142 | } 143 | 144 | findPath() { 145 | if [[ -d /data/adb/modules/ ]]; then 146 | find /data/adb/modules/ -maxdepth 1 -name "$1" 147 | elif [[ -d /data/adb/ksu/modules/ ]]; then 148 | find /data/adb/ksu/modules/ -maxdepth 1 -name "$1" 149 | fi 150 | } 151 | 152 | getConfigValue() { 153 | if [[ -f "/data/adb/modules/MemoryOpt/swap.ini" ]]; then 154 | grep -v '^#' <"/data/adb/modules/MemoryOpt/swap.ini" | grep "^$1=" | cut -f2 -d '=' 155 | fi 156 | } 157 | 158 | volumeKeyListener() { 159 | local choose 160 | local branch 161 | while :; do 162 | choose="$(getevent -qlc 1 | awk '{ print $3 }')" 163 | case "$choose" in 164 | KEY_VOLUMEUP) branch="0" ;; 165 | KEY_VOLUMEDOWN) branch="1" ;; 166 | *) continue ;; 167 | esac 168 | echo "$branch" 169 | break 170 | done 171 | } 172 | 173 | main 174 | -------------------------------------------------------------------------------- /memory.sh: -------------------------------------------------------------------------------- 1 | # Author: 焕晨HChen 2 | MODDIR=${0%/*} 3 | mSwapConfig="$MODDIR/swap.ini" 4 | 5 | main() { 6 | { 7 | echo "###############################################" 8 | time=$(date "+%Y年%m月%d日_%H时%M分%S秒") 9 | echo "手机品牌: $(getprop ro.product.brand)" 10 | echo "手机型号: $(getprop ro.product.device)" 11 | echo "上市名称: $(getprop ro.product.marketname)" 12 | echo "安卓版本: $(getprop ro.build.version.release)" 13 | echo "内存大小: $(expr $(expr $(grep 'MemTotal' /dev/null 105 | 106 | echo "- [i]: 启动 ZRAM!" 107 | swapon /dev/block/zram0 &>/dev/null 108 | } 109 | 110 | initVm() { 111 | # 设置 vm 参数 112 | echo "- [i]: 设置 vm 参数优化!" 113 | 114 | echo "130" >"/proc/sys/vm/swappiness" 115 | if [[ $? -eq 1 ]]; then swappiness=80; else swappiness=130; fi 116 | setValue $swappiness /proc/sys/vm/swappiness 117 | 118 | setValue 5 /proc/sys/vm/dirty_background_ratio # k80p 10 119 | setValue 10 /proc/sys/vm/dirty_ratio # k80p 20 120 | setValue 1000 /proc/sys/vm/dirty_expire_centisecs # k80p 3000 121 | setValue 100 /proc/sys/vm/dirty_writeback_centisecs # k80p 500 122 | setValue 125 /proc/sys/vm/vfs_cache_pressure # k80p 100 123 | 124 | # 杀死触发oom的那个进程, k80p 0 125 | setValue 0 /proc/sys/vm/oom_kill_allocating_task 126 | # 是否打印 oom日志, k80p 1 127 | setValue 0 /proc/sys/vm/oom_dump_tasks 128 | # 是否要允许压缩匿名页 129 | setValue 1 /proc/sys/vm/compact_unevictable_allowed 130 | # io调试开关 131 | setValue 0 /proc/sys/vm/block_dump 132 | # vm 状态更新频率, k80p 1 133 | setValue 3 /proc/sys/vm/stat_interval 134 | # 是否允许过量使用运存 135 | setValue 1 /proc/sys/vm/overcommit_memory 136 | # 触发oom后怎么抛异常 137 | setValue 0 /proc/sys/vm/panic_on_oom 138 | # 此参数决定了内核在后台应该压缩内存的力度。参数取 [0, 100] 范围内的值, k80p 0 139 | setValue 25 /proc/sys/vm/compaction_proactiveness 140 | 141 | if [[ $algorithm == "zstd" ]]; then 142 | setValue 0 /proc/sys/vm/page-cluster 143 | else 144 | # k80p 3 145 | setValue 1 /proc/sys/vm/page-cluster 146 | fi 147 | } 148 | 149 | main 150 | -------------------------------------------------------------------------------- /module.prop: -------------------------------------------------------------------------------- 1 | id=MemoryOpt 2 | name=内存优化 3 | version=2025.02.28 4 | versionCode=2025022800 5 | author=焕晨HChen 6 | description=内存参数调整,以实现更佳的内存使用效率。 7 | updateJson=https://raw.githubusercontent.com/HChenX/MemoryOpt/main/HChen.json 8 | -------------------------------------------------------------------------------- /service.sh: -------------------------------------------------------------------------------- 1 | # Author: 焕晨HChen 2 | MODDIR=${0%/*} 3 | 4 | waitSystemBootCompleted() { 5 | while [[ "$(getprop sys.boot_completed)" != "1" ]]; do 6 | sleep 1 7 | done 8 | while [[ ! -d "/sdcard/Android" ]]; do 9 | sleep 1 10 | done 11 | } 12 | 13 | main() { 14 | waitSystemBootCompleted 15 | 16 | echo -n "" >"$MODDIR"/log.txt # 重置日志 17 | if [[ -f /system/bin/sh ]]; then 18 | /system/bin/sh "$MODDIR/memory.sh" 19 | elif [[ -f /vendor/bin/sh ]]; then 20 | /vendor/bin/sh "$MODDIR/memory.sh" 21 | else 22 | echo "- [!]: 不存在 sh 文件,无法正常执行" >"$MODDIR"/log.txt 23 | exit 1 24 | fi 25 | } 26 | 27 | main 28 | -------------------------------------------------------------------------------- /swap.ini: -------------------------------------------------------------------------------- 1 | # 配置 zram 压缩算法 2 | # 自动检测你无需干扰 3 | 4 | # 是否保留自定义参数 5 | # 目前只支持保留 "zram压缩算法" 6 | persist=false -------------------------------------------------------------------------------- /system.prop: -------------------------------------------------------------------------------- 1 | ro.lmk.low=1001 2 | ro.lmk.medium=1001 3 | ro.lmk.critical=1001 4 | ro.lmk.critical_upgrade=false 5 | ro.lmk.upgrade_pressure=100 6 | ro.lmk.downgrade_pressure=100 7 | ro.lmk.swap_util_max=100 8 | ro.lmk.thrashing_limit=100 9 | ro.lmk.thrashing_limit_decay=20 10 | ro.lmk.psi_partial_stall_ms=200 11 | ro.lmk.psi_complete_stall_ms=700 12 | ro.lmk.swap_free_low_percentage=1 13 | ro.lmk.kill_heaviest_task=false 14 | ro.lmk.swap_is_low_kill_enable=0 --------------------------------------------------------------------------------