From bbe25d7ec1ea4df6b35a69bb96076715b4d7b2f5 Mon Sep 17 00:00:00 2001 From: Vasil Trifonov Date: Thu, 17 May 2018 11:55:15 +0300 Subject: [PATCH] keep some variable from early GC to speed up start time (#5823) * keep some variable from early GC to speed up start time * do not clear callbacks, as they shouldn't use much memory --- .../application/application.android.ts | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tns-core-modules/application/application.android.ts b/tns-core-modules/application/application.android.ts index 5416a1867..a3eae29cd 100644 --- a/tns-core-modules/application/application.android.ts +++ b/tns-core-modules/application/application.android.ts @@ -45,6 +45,8 @@ export class AndroidApplication extends Observable implements AndroidApplication public foregroundActivity: android.app.Activity; public startActivity: android.app.Activity; public packageName: string; + // we are using these property to store the callbacks to avoid early GC collection which would trigger MarkReachableObjects + private callbacks: any = {}; public get currentContext(): android.content.Context { return this.foregroundActivity; @@ -63,10 +65,11 @@ export class AndroidApplication extends Observable implements AndroidApplication this.packageName = nativeApp.getPackageName(); this.context = nativeApp.getApplicationContext(); - let lifecycleCallbacks = initLifecycleCallbacks(); - let componentCallbacks = initComponentCallbacks(); - this.nativeApp.registerActivityLifecycleCallbacks(lifecycleCallbacks); - this.nativeApp.registerComponentCallbacks(componentCallbacks); + // we store those callbacks and add a function for clearing them later so that the objects will be eligable for GC + this.callbacks.lifecycleCallbacks = initLifecycleCallbacks(); + this.callbacks.componentCallbacks = initComponentCallbacks(); + this.nativeApp.registerActivityLifecycleCallbacks(this.callbacks.lifecycleCallbacks); + this.nativeApp.registerComponentCallbacks(this.callbacks.componentCallbacks); this._registerPendingReceivers(); } @@ -190,7 +193,7 @@ export function getNativeApplication(): android.app.Application { // the getInstance might return null if com.tns.NativeScriptApplication exists but is not the starting app type if (!nativeApp) { - // TODO: Should we handle the case when a custom application type is provided and the user has not explicitly initialized the application module? + // TODO: Should we handle the case when a custom application type is provided and the user has not explicitly initialized the application module? const clazz = java.lang.Class.forName("android.app.ActivityThread"); if (clazz) { const method = clazz.getMethod("currentApplication", null); @@ -235,14 +238,15 @@ function initLifecycleCallbacks() { const subscribeForGlobalLayout = profile("subscribeForGlobalLayout", function (activity: android.app.Activity) { const rootView = activity.getWindow().getDecorView().getRootView(); - let onGlobalLayoutListener = new android.view.ViewTreeObserver.OnGlobalLayoutListener({ + // store the listener not to trigger GC collection before collecting the method + this.onGlobalLayoutListener = new android.view.ViewTreeObserver.OnGlobalLayoutListener({ onGlobalLayout() { notify({ eventName: displayedEvent, object: androidApp, activity }); let viewTreeObserver = rootView.getViewTreeObserver(); - viewTreeObserver.removeOnGlobalLayoutListener(onGlobalLayoutListener); + viewTreeObserver.removeOnGlobalLayoutListener(this.onGlobalLayoutListener); } }); - rootView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener); + rootView.getViewTreeObserver().addOnGlobalLayoutListener(this.onGlobalLayoutListener); }); const lifecycleCallbacks = new android.app.Application.ActivityLifecycleCallbacks({