Florian Schaal Florian Schaal - 3 days ago 4
C# Question

PDFViewCtrl not disposed

We are working on an app using PDFTron. We have noticed that when opening the Activity that displays the allocated memory increases by a lot. After navigating away from the activity only a part of the memory is freed up again. Some PDFtron parts are left behind.

enter image description here

The problem is that the activity does not seem to finish even though that we have set the activity to NoHistory and singleTask. I have watched some courses on memory management but can't figure out what is cause the activity to remain open.

Before we call

Finish();
on the activity we dispose of the PdfViewCtrl. Then in the OnDestroy:

protected override void OnDestroy()
{
base.OnDestroy();
if (_mPdfViewCtrl == null) return;
_mPdfViewCtrl.Destroy();
_mPdfViewCtrl = null;
}


If the app is used for a long periode of time this results in a out-of-memory error.

--- End of managed Java.Lang.Error stack trace ---
java.lang.OutOfMemoryError
dalvik.system.NativeStart.run(Native Method):0


or

--- End of managed Java.Lang.Error stack trace ---
java.lang.OutOfMemoryError
android.graphics.Bitmap.nativeCreate(Native Method):0
android.graphics.Bitmap.createBitmap(Bitmap.java:726):0
android.graphics.Bitmap.createBitmap(Bitmap.java:703):0
android.graphics.Bitmap.createBitmap(Bitmap.java:670):0
pdftron.PDF.Utils.SignaturePickerDialog$SignatureView.onSizeChanged(SignaturePickerDialog.java:244):0
android.view.View.sizeChange(View.java:15326):0
android.view.View.setFrame(View.java:15290):0
android.view.View.layout(View.java:15201):0
android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076):0
android.view.View.layout(View.java:15204):0
android.view.ViewGroup.layout(ViewGroup.java:4793):0
android.widget.FrameLayout.onLayout(FrameLayout.java:448):0
android.view.View.layout(View.java:15204):0
android.view.ViewGroup.layout(ViewGroup.java:4793):0
android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076):0
android.view.View.layout(View.java:15204):0
android.view.ViewGroup.layout(ViewGroup.java:4793):0
android.widget.FrameLayout.onLayout(FrameLayout.java:448):0
android.view.View.layout(View.java:15204):0
android.view.ViewGroup.layout(ViewGroup.java:4793):0
android.widget.FrameLayout.onLayout(FrameLayout.java:448):0
android.view.View.layout(View.java:15204):0
android.view.ViewGroup.layout(ViewGroup.java:4793):0
android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677):0
android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531):0
android.widget.LinearLayout.onLayout(LinearLayout.java:1440):0
android.view.View.layout(View.java:15204):0
android.view.ViewGroup.layout(ViewGroup.java:4793):0
android.widget.FrameLayout.onLayout(FrameLayout.java:448):0
android.view.View.layout(View.java:15204):0
android.view.ViewGroup.layout(ViewGroup.java:4793):0
android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677):0
android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531):0
android.widget.LinearLayout.onLayout(LinearLayout.java:1440):0
android.view.View.layout(View.java:15204):0
android.view.ViewGroup.layout(ViewGroup.java:4793):0
android.widget.FrameLayout.onLayout(FrameLayout.java:448):0
android.view.View.layout(View.java:15204):0
android.view.ViewGroup.layout(ViewGroup.java:4793):0
android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2263):0
android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2009):0
android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1251):0
android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6379):0
android.view.Choreographer$CallbackRecord.run(Choreographer.java:791):0
android.view.Choreographer.doCallbacks(Choreographer.java:591):0
android.view.Choreographer.doFrame(Choreographer.java:561):0
android.view.Choreographer$FrameHandler.handleMessage(Choreographer.java:693):0
android.os.Handler.dispatchMessage(Handler.java:99):0
android.os.Looper.loop(Looper.java:137):0
android.app.ActivityThread.main(ActivityThread.java:5493):0
java.lang.reflect.Method.invokeNative(Native Method):0
java.lang.reflect.Method.invoke(Method.java:525):0
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209):0
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025):0
dalvik.system.NativeStart.main(Native Method):0


Below a print screen from a simulated session (Android studio monitoring):
enter image description here

Edited OnDestroy:

var old_tool = _mPdfViewCtrl.ToolManager;
var old_doc = _mPdfViewCtrl.Doc;
_mPdfViewCtrl?.CloseDoc();
_mPdfViewCtrl?.CloseTool();
_mPdfViewCtrl?.Destroy();
_mPdfViewCtrl = null;
_mPdfViewCtrl?.Dispose();
if (old_doc != null)
{
old_doc.Dispose();
}

if (old_tool != null)
{
old_tool.Dispose();
}


enter image description here

Answer

If your Activity also has a reference to a PDFDoc object, then you should also dispose of that. This is the recommended way.

protected override void OnDestroy()
{
    base.OnDestroy();
    if (_mPdfViewCtrl == null) return;
    _mPdfViewCtrl.Destroy();
    _mPdfViewCtrl = null;
    if(_mPdfDoc != null)
    {
        _mPdfDoc.Dispose();
    }
}

or more generally

protected override void OnDestroy()
{
    base.OnDestroy();
    if (_mPdfViewCtrl == null) return;
    PDFDoc old_doc = _mPdfViewCtrl.Doc;
    _mPdfViewCtrl.Destroy();
    _mPdfViewCtrl = null;
    if(old_doc != null)
    {
        old_doc.Dispose();
    }
}
Comments