11#! /system/bin/sh
22MODDIR=${0%/* }
3+
4+ # Wait for system boot completion before doing ANYTHING
5+ while [ " $( getprop sys.boot_completed) " != " 1" ]; do
6+ sleep 5
7+ done
8+ sleep 15 # Additional wait for system stability
9+
310# Ensure permissions for all scripts
411chmod 755 " $MODDIR " /* .sh 2> /dev/null
512
@@ -13,7 +20,7 @@ touch "$LOG_FILE"
1320# Log function
1421log () {
1522 echo " $( date ' +%Y-%m-%d %H:%M:%S' ) [OOM Adjuster] $* " >> " $LOG_FILE "
16- tail -n 1000 " $LOG_FILE " > " $MODDIR /tmp_log" && mv " $MODDIR /tmp_log" " $LOG_FILE "
23+ tail -n 5000 " $LOG_FILE " > " $MODDIR /tmp_log" && mv " $MODDIR /tmp_log" " $LOG_FILE "
1724}
1825
1926# Load config values
@@ -43,10 +50,10 @@ load_config() {
4350}
4451
4552# Launch phantom fix
46- if [ -f " $MODDIR /phantom_fix.sh" ]; then
47- sh " $MODDIR /phantom_fix.sh" &
48- log " Phantom fix started"
49- fi
53+ # if [ -f "$MODDIR/phantom_fix.sh" ]; then
54+ # sh "$MODDIR/phantom_fix.sh" &
55+ # log "Phantom fix started"
56+ # fi
5057
5158# Load config
5259load_config
@@ -122,17 +129,160 @@ prev_pid_pogo=""
122129 sleep 0.1
123130 done
124131) &
125- # --------------------------------------------------
126- # PolygonX watchdog loop
132+ # ==========================
133+ # PolygonX Watchdog with Dual Cache Clear & PoGo Kill on LMKD Recovery
134+ # ==========================
127135(
136+ APP_PKG=" com.evermorelabs.polygonx"
137+ POGO_PKG=" com.nianticlabs.pokemongo"
138+ CHECK_INTERVAL=35
139+ RESTART_DELAY=15
140+
141+ log " PolygonX watchdog started with dual cache clear & PoGo kill on LMKD recovery."
142+
128143 while true ; do
129- if ! pidof com.evermorelabs.polygonx > /dev/null; then
130- log " PolygonX not running - attempting to restart service."
131- am startservice --user 0 com.evermorelabs.polygonx/.services.PolygonXService 2>> " $MODDIR /oom_adjuster.log"
144+ if ! pidof " $APP_PKG " > /dev/null; then
145+ log " PolygonX not running - waiting ${RESTART_DELAY} s and cleaning up..."
146+ sleep " $RESTART_DELAY "
147+
148+ # Store whether PoGo was running before PolygonX died
149+ was_pogo_running=$( pidof " $POGO_PKG " )
150+
151+ # KILL PoGo if it was running when PolygonX died (LMKD scenario)
152+ if [ -n " $was_pogo_running " ]; then
153+ log " LMKD kill detected - force killing PoGo with kill -9..."
154+
155+ # Aggressive kill of all PoGo processes
156+ pgrep -f " $POGO_PKG " | while read -r pid; do
157+ kill -9 " $pid " 2> /dev/null
158+ log " Killed -9 PoGo process: $pid "
159+ done
160+
161+ # Force stop any remnants
162+ am force-stop " $POGO_PKG " 2> /dev/null
163+
164+ log " PoGo force kill completed"
165+
166+ # CLEAR CACHE FOR BOTH APPS (not data)
167+ log " Clearing cache for both PolygonX and Pokémon Go..."
168+
169+ # Clear PolygonX cache
170+ cmd package trim-caches com.evermorelabs.polygonx >> " $LOG_FILE " 2>&1
171+ pm clear-com.evermorelabs.polygonx.CACHE >> " $LOG_FILE " 2>&1
172+ rm -rf /data/data/com.evermorelabs.polygonx/cache/* 2> /dev/null
173+ log " PolygonX cache cleared"
174+
175+ # Clear Pokémon Go cache
176+ cmd package trim-caches com.nianticlabs.pokemongo >> " $LOG_FILE " 2>&1
177+ pm clear-com.nianticlabs.pokemongo.CACHE >> " $LOG_FILE " 2>&1
178+ rm -rf /data/data/com.nianticlabs.pokemongo/cache/* 2> /dev/null
179+ log " Pokémon Go cache cleared"
180+
181+ log " Dual cache clearing completed (both apps)"
182+ fi
183+
184+ # Free memory before restarting PolygonX
185+ log " Pre-restart memory cleanup..."
186+ echo 3 > /proc/sys/vm/drop_caches
187+ sync
188+ sleep 8
189+
190+ # Restart PolygonX using monkey (the working method)
191+ log " Restarting PolygonX via monkey..."
192+ timeout 10 /system/bin/monkey -p com.evermorelabs.polygonx -c android.intent.category.LAUNCHER 1 >> " $LOG_FILE " 2>&1
193+
194+ sleep 8
195+ if pidof " $APP_PKG " > /dev/null; then
196+ log " SUCCESS: PolygonX restarted via monkey after dual cache clear"
197+
198+ # Log the restart cycle completion
199+ if [ -n " $was_pogo_running " ]; then
200+ log " LMKD recovery cycle completed: Both apps killed → Both caches cleared → PolygonX restarted"
201+ fi
202+ else
203+ log " FAILED: PolygonX restart failed after cache clear"
204+ fi
205+
206+ # Extended wait after restart attempt
207+ sleep 30
132208 fi
209+ sleep " $CHECK_INTERVAL "
210+ done
211+ ) &
212+
213+ # ==========================
214+ # Swap Space Monitor & Protector
215+ # ==========================
216+ (
217+ while true ; do
218+ # Check swap usage to prevent LMKD kills
219+ if [ -f /proc/swaps ] && [ -f /proc/meminfo ]; then
220+ swap_total=$( grep SwapTotal /proc/meminfo | awk ' {print $2}' )
221+ swap_free=$( grep SwapFree /proc/meminfo | awk ' {print $2}' )
222+
223+ if [ " $swap_total " -gt 1000 ]; then # Only if swap is configured
224+ swap_used=$(( swap_total - swap_free))
225+ swap_usage_percent=$(( (swap_used * 100 ) / swap_total ))
226+
227+ # If swap is critically low, LMKD will kill aggressively
228+ if [ " $swap_usage_percent " -ge 85 ]; then
229+ log " CRITICAL: High swap usage (${swap_usage_percent} %) - clearing to prevent LMKD kills"
230+
231+ # Emergency memory recovery
232+ echo 3 > /proc/sys/vm/drop_caches
233+ sync
234+
235+ # Kill some user apps to free swap
236+ pm list packages -3 | cut -d: -f2 | head -3 | while read -r pkg; do
237+ if [ " $pkg " != " com.evermorelabs.polygonx" ] && [ " $pkg " != " com.nianticlabs.pokemongo" ]; then
238+ am force-stop " $pkg " 2> /dev/null && \
239+ log " Freed swap by stopping: $pkg "
240+ fi
241+ done
242+
243+ # Compact memory to reduce swap usage
244+ cmd activity compact -m full com.nianticlabs.pokemongo 2> /dev/null
245+ cmd activity compact -m full com.evermorelabs.polygonx 2> /dev/null
246+ fi
247+ fi
248+ fi
249+
133250 sleep 30
134251 done
135252) &
253+
254+ # ==========================
255+ # PolygonX Anti-Kill Protection
256+ # ==========================
257+ (
258+ while true ; do
259+ polygonx_pid=$( pidof com.evermorelabs.polygonx)
260+
261+ if [ -n " $polygonx_pid " ]; then
262+ # Maximum protection against LMKD
263+ echo -1000 > /proc/$polygonx_pid /oom_score_adj 2> /dev/null
264+ echo -1000 > /proc/$polygonx_pid /oom_adj 2> /dev/null
265+
266+ # Prevent swap pressure kills
267+ if [ -f /proc/$polygonx_pid /oom_score ]; then
268+ echo 0 > /proc/$polygonx_pid /oom_score 2> /dev/null
269+ fi
270+
271+ # Keep in foreground cgroups
272+ echo $polygonx_pid > /dev/cpuset/foreground/tasks 2> /dev/null
273+ echo $polygonx_pid > /dev/stune/foreground/tasks 2> /dev/null
274+
275+ # Log protection status occasionally
276+ if [ $(( RANDOM % 10 )) -eq 0 ]; then
277+ oom_score=$( cat /proc/$polygonx_pid /oom_score_adj 2> /dev/null || echo " unknown" )
278+ log " PolygonX protection active (PID: $polygonx_pid , oom_score_adj: $oom_score )"
279+ fi
280+ fi
281+
282+ sleep 20
283+ done
284+ ) &
285+
136286# --------------------------------------------------
137287# drop_caches loop (every 30s if RAM usage > 80%)
138288(
@@ -142,7 +292,7 @@ prev_pid_pogo=""
142292 mem_used_kb=$(( mem_total_kb - mem_avail_kb))
143293 mem_usage_percent=$(( (mem_used_kb * 100 ) / mem_total_kb ))
144294
145- if [ " $mem_usage_percent " -ge 80 ]; then
295+ if [ " $mem_usage_percent " -ge 70 ]; then
146296 echo 3 > /proc/sys/vm/drop_caches
147297 log " Dropped caches due to high memory usage (${mem_usage_percent} %)"
148298 sleep_interval=10
@@ -163,7 +313,7 @@ prev_pid_pogo=""
163313 mem_used_kb=$(( mem_total_kb - mem_avail_kb))
164314 mem_usage_percent=$(( (mem_used_kb * 100 ) / mem_total_kb ))
165315
166- if [ " $mem_usage_percent " -ge 80 ]; then
316+ if [ " $mem_usage_percent " -ge 70 ]; then
167317 log " Memory usage at ${mem_usage_percent} %. Compacting app memory..."
168318 cmd activity compact -m some com.nianticlabs.pokemongo 2> /dev/null && \
169319 log " Compacted PoGo memory"
@@ -185,7 +335,7 @@ prev_pid_pogo=""
185335 mem_used_kb=$(( mem_total_kb - mem_avail_kb))
186336 mem_usage_percent=$(( (mem_used_kb * 100 ) / mem_total_kb ))
187337# --------------------------------------------------
188- if [ " $mem_usage_percent " -ge 80 ]; then
338+ if [ " $mem_usage_percent " -ge 75 ]; then
189339 log " Memory usage at ${mem_usage_percent} %. Running LRU deprioritization."
190340# --------------------------------------------------
191341 dumpsys activity lru | grep -E ' Proc #[0-9]+:' | while read -r line; do
@@ -213,6 +363,55 @@ prev_pid_pogo=""
213363 done
214364) &
215365# --------------------------------------------------
216- # --------------------------------------------------
366+ # ==========================
367+ # Pokémon GO Memory Killer
368+ # ==========================
369+ (
370+ POGO_PKG=" com.nianticlabs.pokemongo"
371+ SYSTEM_MEMORY_THRESHOLD=95 # Kill at 95% system RAM
372+ POGO_MEMORY_THRESHOLD=2800 # Kill at 2800MB PoGo usage
373+ CHECK_INTERVAL=10
374+
375+ log " PoGo memory killer started (System: ${SYSTEM_MEMORY_THRESHOLD} %, PoGo: ${POGO_MEMORY_THRESHOLD} MB)"
376+
377+ while true ; do
378+ pogo_pid=$( pidof " $POGO_PKG " )
379+ if [ -n " $pogo_pid " ]; then
380+ # Calculate system memory usage percentage
381+ mem_total_kb=$( grep MemTotal /proc/meminfo | awk ' {print $2}' )
382+ mem_avail_kb=$( grep MemAvailable /proc/meminfo | awk ' {print $2}' )
383+ mem_used_kb=$(( mem_total_kb - mem_avail_kb))
384+ mem_usage_percent=$(( (mem_used_kb * 100 ) / mem_total_kb ))
385+
386+ # Get PoGo specific memory usage (in MB)
387+ pogo_memory_mb=$( cat /proc/$pogo_pid /status 2> /dev/null | grep VmRSS | awk ' {print $2}' )
388+ pogo_memory_mb=$(( pogo_memory_mb / 1024 ))
389+
390+ log " Memory: Total=${mem_usage_percent} %, PoGo=${pogo_memory_mb} MB"
391+
392+ # Kill if system memory > 95% OR PoGo using > 2800MB
393+ if [ " $mem_usage_percent " -ge " $SYSTEM_MEMORY_THRESHOLD " ] || [ " $pogo_memory_mb " -ge " $POGO_MEMORY_THRESHOLD " ]; then
394+ log " KILLING PoGo - System RAM: ${mem_usage_percent} %, PoGo RAM: ${pogo_memory_mb} MB"
395+
396+ # Clear PoGo cache before killing
397+ cmd package trim-caches com.nianticlabs.pokemongo >> " $LOG_FILE " 2>&1
398+
399+ # Kill all PoGo processes aggressively
400+ pgrep -f " $POGO_PKG " | while read -r pid; do
401+ kill -9 " $pid " 2> /dev/null
402+ done
403+
404+ # Force stop any remnants
405+ am force-stop " $POGO_PKG " 2> /dev/null
406+
407+ # Free system memory
408+ echo 3 > /proc/sys/vm/drop_caches
409+
410+ log " SUCCESS: PoGo killed and memory freed - PolygonX will restart it"
411+ fi
412+ fi
413+ sleep " $CHECK_INTERVAL "
414+ done
415+ ) &
217416log " OOM adjustment, cache cleaner, and watchdog started in background."
218417exit 0
0 commit comments