我们正在开发一个使用 PDFTron 的应用程序。我们注意到,当打开显示分配内存的 Activity 时,会增加很多。离开活动后,只有一部分内存再次被释放。一些 PDFtron 部件被留下。
问题是,即使我们将活动设置为 NoHistory 和 singleTask,活动似乎也没有完成。我看过一些关于内存管理的课程,但无法弄清楚是什么导致活动保持打开状态。
在我们调用Finish();
活动之前,我们处理 PdfViewCtrl。然后在 OnDestroy 中:
protected override void OnDestroy()
{
base.OnDestroy();
if (_mPdfViewCtrl == null) return;
_mPdfViewCtrl.Destroy();
_mPdfViewCtrl = null;
}
如果应用程序长时间使用,则会导致内存不足错误。
--- End of managed Java.Lang.Error stack trace ---
java.lang.OutOfMemoryError
dalvik.system.NativeStart.run(Native Method):0
或者
--- 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
在模拟会话的打印屏幕下方(Android Studio 监控):
编辑 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();
}