From 5324723c2bcab7062f393a5057e51733a1715788 Mon Sep 17 00:00:00 2001 From: Po Lu Date: Fri, 10 Nov 2023 14:57:24 +0800 Subject: [PATCH] Clear image caches in reaction to system VM warnings * java/org/gnu/emacs/EmacsNative.java (onLowMemory): * java/org/gnu/emacs/EmacsService.java (onLowMemory): New function. * src/android.c (android_on_low_memory, onLowMemory): New functions called when a VM caution is registered. Clear the image cache and run garbage collection. --- java/org/gnu/emacs/EmacsNative.java | 3 +++ java/org/gnu/emacs/EmacsService.java | 12 ++++++++++++ src/android.c | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/java/org/gnu/emacs/EmacsNative.java b/java/org/gnu/emacs/EmacsNative.java index 946a38f7f84..78176dd0e47 100644 --- a/java/org/gnu/emacs/EmacsNative.java +++ b/java/org/gnu/emacs/EmacsNative.java @@ -96,6 +96,9 @@ public final class EmacsNative thread, then return. */ public static native void shutDownEmacs (); + /* Garbage collect and clear each frame's image cache. */ + public static native void onLowMemory (); + /* Abort and generate a native core dump. */ public static native void emacsAbort (); diff --git a/java/org/gnu/emacs/EmacsService.java b/java/org/gnu/emacs/EmacsService.java index ab6d57b9c4f..1aac1a6c4dd 100644 --- a/java/org/gnu/emacs/EmacsService.java +++ b/java/org/gnu/emacs/EmacsService.java @@ -321,6 +321,10 @@ public final class EmacsService extends Service } } + /* The native functions the subsequent two functions call do nothing + in the infrequent case the Emacs thread is awaiting a response + for the main thread. Caveat emptor! */ + @Override public void onDestroy () @@ -333,6 +337,14 @@ public final class EmacsService extends Service super.onDestroy (); } + @Override + public void + onLowMemory () + { + EmacsNative.onLowMemory (); + super.onLowMemory (); + } + /* Functions from here on must only be called from the Emacs diff --git a/src/android.c b/src/android.c index f5af742b422..7a670cb507f 100644 --- a/src/android.c +++ b/src/android.c @@ -1957,6 +1957,26 @@ NATIVE_NAME (shutDownEmacs) (JNIEnv *env, jobject object) android_run_in_emacs_thread (android_shut_down_emacs, NULL); } +/* Carry out garbage collection and clear all image caches on the + Android terminal. Called when the system has depleted most of its + memory and desires that background processes release unused + core. */ + +static void +android_on_low_memory (void *closure) +{ + Fclear_image_cache (Qt, Qt); + garbage_collect (); +} + +JNIEXPORT void JNICALL +NATIVE_NAME (onLowMemory) (JNIEnv *env, jobject object) +{ + JNI_STACK_ALIGNMENT_PROLOGUE; + + android_run_in_emacs_thread (android_on_low_memory, NULL); +} + JNIEXPORT jlong JNICALL NATIVE_NAME (sendConfigureNotify) (JNIEnv *env, jobject object, jshort window, jlong time, -- 2.39.2