Skip to content

Commit

Permalink
[TIMOB-26242] More memory management changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary Mathews committed Jul 31, 2018
1 parent d7edd8a commit 0fa2ad3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static KrollRuntime getInstance()
public static void suggestGC()
{
if (instance != null) {
instance.setGCFlag();
instance.doIdle();
}
}

Expand Down Expand Up @@ -466,7 +466,7 @@ public void setEvaluator(KrollEvaluator eval)
evaluator = eval;
}

public void setGCFlag()
public void doIdle()
{
// No-op V8 should override.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class V8Runtime extends KrollRuntime implements Handler.Callback
{
private static final String TAG = "KrollV8Runtime";
private static final String NAME = "v8";
private static final int MAX_V8_IDLE_INTERVAL = 30 * 1000; // ms
private static final int MAX_V8_IDLE_INTERVAL = 10 * 1000; // ms

private boolean libLoaded = false;

Expand All @@ -42,8 +42,7 @@ public final class V8Runtime extends KrollRuntime implements Handler.Callback
new HashMap<String, KrollSourceCodeProvider>();

private ArrayList<String> loadedLibs = new ArrayList<String>();
private AtomicBoolean shouldGC = new AtomicBoolean(false);
private long lastV8Idle;
private long lastV8Idle = 0;

public static boolean isEmulator()
{
Expand Down Expand Up @@ -113,20 +112,12 @@ public void initRuntime()
@Override
public boolean queueIdle()
{
boolean willGC = shouldGC.getAndSet(false);
if (!willGC) {
// This means we haven't specifically been told to do
// a V8 GC (which is just a call to nativeIdle()), but nevertheless
// if more than the recommended time has passed since the last
// call to nativeIdle(), we'll want to do it anyways.
willGC = ((System.currentTimeMillis() - lastV8Idle) > MAX_V8_IDLE_INTERVAL);
}
if (willGC) {
boolean gcWantsMore = !nativeIdle();
if ((System.currentTimeMillis() - lastV8Idle) > MAX_V8_IDLE_INTERVAL) {
lastV8Idle = System.currentTimeMillis();
if (gcWantsMore) {
shouldGC.set(true);
}

// although we setup an interval to initiate GC
// V8 will ultimately decide when to GC under its own accord
nativeIdle();
}
return true;
}
Expand Down Expand Up @@ -219,9 +210,9 @@ public static void addExternalCommonJsModule(String id, Class<? extends KrollSou
}

@Override
public void setGCFlag()
public void doIdle()
{
shouldGC.set(true);
nativeIdle();
}

// JNI method prototypes
Expand Down
7 changes: 5 additions & 2 deletions android/runtime/v8/src/native/V8Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,11 @@ JNIEXPORT jboolean JNICALL Java_org_appcelerator_kroll_runtime_v8_V8Runtime_nati
// while (v8::platform::PumpMessageLoop(V8Runtime::platform, V8Runtime:v8_isolate)) continue;
// v8::platform::RunIdleTasks(g_platform, isolate, 50.0 / base::Time::kMillisecondsPerSecond);

// FIXME What is a good value to use here? We're basically giving it 100 ms to run right now
double deadline_in_s = V8Runtime::platform->MonotonicallyIncreasingTime() + 0.1;
// notify V8 of low memory to suggest a GC
V8Runtime::v8_isolate->LowMemoryNotification();

// give GC time to perform cleanup (1 second)
double deadline_in_s = V8Runtime::platform->MonotonicallyIncreasingTime() + 1;
return V8Runtime::v8_isolate->IdleNotificationDeadline(deadline_in_s);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ public void releaseKroll()
// NOTE: there is an issue where proxies created in another context (such as an event listener callback)
// may not be destroyed when the context has ended. This workaround allows the GC to free these objects
// if memory is constraint.
if (referenceKey != -1) {
/*if (referenceKey != -1) {
try {
final Class referenceTableClass = Class.forName("org.appcelerator.kroll.runtime.v8.ReferenceTable");
final Method getReferenceMethod = referenceTableClass.getDeclaredMethod("getReference", long.class);
Expand All @@ -1440,6 +1440,9 @@ public void releaseKroll()
if (krollObject != null) {
krollObject.release();
}
}*/
if (krollObject != null) {
krollObject.release();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,6 @@ public void releaseViews()
mainHandler = null;
}
setModelListener(null);

releaseKroll();
KrollRuntime.suggestGC();
}

@Override
Expand Down

0 comments on commit 0fa2ad3

Please sign in to comment.