From 9d1d9bbedbba1bc702e1f1482d745de04ce27b07 Mon Sep 17 00:00:00 2001
From: Sergey Volkov <s.volkov@netris.ru>
Date: Mon, 7 Aug 2017 14:32:26 +0300
Subject: [PATCH] v8 LowMemoryNotification()

---
 .../src/java/org/appcelerator/kroll/KrollRuntime.java | 19 +++++++++++++++++++
 .../org/appcelerator/kroll/runtime/v8/V8Runtime.java  |  7 +++++++
 android/runtime/v8/src/native/V8Runtime.cpp           |  5 +++++
 .../java/org/appcelerator/titanium/TiApplication.java |  2 ++
 4 files changed, 33 insertions(+)

diff --git a/android/runtime/common/src/java/org/appcelerator/kroll/KrollRuntime.java b/android/runtime/common/src/java/org/appcelerator/kroll/KrollRuntime.java
index c64f567..3e0254c 100644
--- a/android/runtime/common/src/java/org/appcelerator/kroll/KrollRuntime.java
+++ b/android/runtime/common/src/java/org/appcelerator/kroll/KrollRuntime.java
@@ -39,6 +39,7 @@ public abstract class KrollRuntime implements Handler.Callback
 	private static final int MSG_DISPOSE = 101;
 	private static final int MSG_RUN_MODULE = 102;
 	private static final int MSG_EVAL_STRING = 103;
+	private static final int MSG_LOW_MEMORY = 104;
 
 	private static final String PROPERTY_FILENAME = "filename";
 	private static final String PROPERTY_SOURCE = "source";
@@ -237,6 +238,14 @@ public abstract class KrollRuntime implements Handler.Callback
 		}
 	}
 
+	public void onLowMemory() {
+		if (isRuntimeThread()) {
+			handleLowMemory();
+		} else {
+			handler.sendEmptyMessage(MSG_LOW_MEMORY);
+		}
+	}
+
 	public void runModule(String source, String filename, KrollProxySupport activityProxy)
 	{
 		if (isRuntimeThread()) {
@@ -329,6 +338,11 @@ public abstract class KrollRuntime implements Handler.Callback
 				doEvalString(source, filename);
 				return true;
 			}
+
+			case MSG_LOW_MEMORY: {
+				handleLowMemory();
+				return true;
+			}
 		}
 
 		return false;
@@ -465,6 +479,11 @@ public abstract class KrollRuntime implements Handler.Callback
 		// No-op V8 should override.
 	}
 
+	public void handleLowMemory()
+	{
+		// No-op V8 should override.
+	}
+
 	public State getRuntimeState()
 	{
 		return runtimeState;
diff --git a/android/runtime/v8/src/java/org/appcelerator/kroll/runtime/v8/V8Runtime.java b/android/runtime/v8/src/java/org/appcelerator/kroll/runtime/v8/V8Runtime.java
index 2113f87..008c9b1 100644
--- a/android/runtime/v8/src/java/org/appcelerator/kroll/runtime/v8/V8Runtime.java
+++ b/android/runtime/v8/src/java/org/appcelerator/kroll/runtime/v8/V8Runtime.java
@@ -231,6 +231,12 @@ public final class V8Runtime extends KrollRuntime implements Handler.Callback
 	}
 
 	@Override
+	public void handleLowMemory() {
+		nativeLowMemory();
+		shouldGC.set(false);
+	}
+
+	@Override
 	public void setGCFlag()
 	{
 		shouldGC.set(true);
@@ -242,5 +248,6 @@ public final class V8Runtime extends KrollRuntime implements Handler.Callback
 	private native Object nativeEvalString(String source, String filename);
 	private native boolean nativeIdle();
 	private native void nativeDispose();
+	private native void nativeLowMemory();
 	private native void nativeAddExternalCommonJsModule(String moduleName, KrollSourceCodeProvider sourceProvider);
 }
diff --git a/android/runtime/v8/src/native/V8Runtime.cpp b/android/runtime/v8/src/native/V8Runtime.cpp
index 85b56ac..d8d3ca0 100644
--- a/android/runtime/v8/src/native/V8Runtime.cpp
+++ b/android/runtime/v8/src/native/V8Runtime.cpp
@@ -322,6 +322,11 @@ JNIEXPORT jboolean JNICALL Java_org_appcelerator_kroll_runtime_v8_V8Runtime_nati
 	return V8Runtime::v8_isolate->IdleNotificationDeadline(deadline_in_s);
 }
 
+JNIEXPORT void JNICALL Java_org_appcelerator_kroll_runtime_v8_V8Runtime_nativeLowMemory(JNIEnv *env, jobject self)
+{
+    V8Runtime::v8_isolate->LowMemoryNotification();
+}
+
 /*
  * Called by V8Runtime.java, this passes a KrollSourceCodeProvider java class instance
  * to KrollBindings, where it's stored and later used to retrieve an external CommonJS module's
diff --git a/android/titanium/src/java/org/appcelerator/titanium/TiApplication.java b/android/titanium/src/java/org/appcelerator/titanium/TiApplication.java
index de3a1a3..d7ae0eb 100644
--- a/android/titanium/src/java/org/appcelerator/titanium/TiApplication.java
+++ b/android/titanium/src/java/org/appcelerator/titanium/TiApplication.java
@@ -401,6 +401,7 @@ public abstract class TiApplication extends Application implements KrollApplicat
 		// Release all the cached images
 		TiBlobLruCache.getInstance().evictAll();
 		TiImageLruCache.getInstance().evictAll();
+		KrollRuntime.getInstance().onLowMemory();
 		super.onLowMemory();
 	}
 
@@ -412,6 +413,7 @@ public abstract class TiApplication extends Application implements KrollApplicat
 			// Release all the cached images
 			TiBlobLruCache.getInstance().evictAll();
 			TiImageLruCache.getInstance().evictAll();
+			KrollRuntime.getInstance().onLowMemory();
 		}
 		super.onTrimMemory(level);
 	}
-- 
2.7.4

