Android项目中的WebView内存泄漏

时间:2014-09-10 12:08:29

标签: android memory-leaks

我正在使用java项目和Android项目。 java项目使用Android项目中Activity对象的引用(当我调用java项目中的函数时,我发送Activity引用)。到目前为止还可以,但有时会出现错误。它是内存泄漏:

    09-09 20:54:38.496: E/WindowManager(15895): android.view.WindowLeaked: Activity com.test.android.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{425c0468 V.E..... R.....I. 0,0-720,1280} that was originally added here
09-09 20:54:38.496: E/WindowManager(15895):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:467)
09-09 20:54:38.496: E/WindowManager(15895):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:267)
09-09 20:54:38.496: E/WindowManager(15895):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
09-09 20:54:38.496: E/WindowManager(15895):     at android.app.Dialog.show(Dialog.java:289)
09-09 20:54:38.496: E/WindowManager(15895):     at com.test.AppBase$5.run(AppBase.java:210)
09-09 20:54:38.496: E/WindowManager(15895):     at android.app.Activity.runOnUiThread(Activity.java:4924)
09-09 20:54:38.496: E/WindowManager(15895):     at com.test.AppBase.setVisibility(AppBase.java:197)
09-09 20:54:38.496: E/WindowManager(15895):     at com.test.AppBase.callSmth(AppBase.java:140)
09-09 20:54:38.496: E/WindowManager(15895):     at com.test.AppBase.callSmth(AppBase.java:156)
09-09 20:54:38.496: E/WindowManager(15895):     at com.test.android.MainActivity.onClick(MainActivity.java:47)
09-09 20:54:38.496: E/WindowManager(15895):     at android.view.View.performClick(View.java:4654)
09-09 20:54:38.496: E/WindowManager(15895):     at android.view.View$PerformClick.run(View.java:19438)
09-09 20:54:38.496: E/WindowManager(15895):     at android.os.Handler.handleCallback(Handler.java:733)
09-09 20:54:38.496: E/WindowManager(15895):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-09 20:54:38.496: E/WindowManager(15895):     at android.os.Looper.loop(Looper.java:146)
09-09 20:54:38.496: E/WindowManager(15895):     at android.app.ActivityThread.main(ActivityThread.java:5602)
09-09 20:54:38.496: E/WindowManager(15895):     at java.lang.reflect.Method.invokeNative(Native Method)
09-09 20:54:38.496: E/WindowManager(15895):     at java.lang.reflect.Method.invoke(Method.java:515)
09-09 20:54:38.496: E/WindowManager(15895):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
09-09 20:54:38.496: E/WindowManager(15895):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
09-09 20:54:38.496: E/WindowManager(15895):     at dalvik.system.NativeStart.main(Native Method)

我正在使用MAT并在eclipse中从DMMS获取转储进行分析。它表明我在java项目中有一个类可以获得大部分资源。在那里我使用webview,在mainthread上运行。如果我尝试在非主线上初始化webview,则会出现警告,而在某些设备上则只显示webview。

其中一个网页浏览集成在一个对话框中。从我可以理解的是,当我尝试关闭对话框时,对Activity的引用可能不存在。

有时我也会收到此错误(无论设备是什么,api> 10)

E/ActivityThread(16551): Pause GC
E/ActivityThread(16551): java.lang.reflect.InvocationTargetException
E/ActivityThread(16551):        at java.lang.reflect.Method.invokeNative(Native Method)
E/ActivityThread(16551):        at java.lang.reflect.Method.invoke(Method.java:515)
E/ActivityThread(16551):        at android.app.ActivityThread.pauseGC(ActivityThread.java:5525)
E/ActivityThread(16551):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
E/ActivityThread(16551):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
E/ActivityThread(16551):        at android.app.ActivityThread.access$900(ActivityThread.java:175)
E/ActivityThread(16551):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
E/ActivityThread(16551):        at android.os.Handler.dispatchMessage(Handler.java:102)
E/ActivityThread(16551):        at android.os.Looper.loop(Looper.java:146)
E/ActivityThread(16551):        at android.app.ActivityThread.main(ActivityThread.java:5602)
E/ActivityThread(16551):        at java.lang.reflect.Method.invokeNative(Native Method)
E/ActivityThread(16551):        at java.lang.reflect.Method.invoke(Method.java:515)
E/ActivityThread(16551):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
E/ActivityThread(16551):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
E/ActivityThread(16551):        at dalvik.system.NativeStart.main(Native Method)
E/ActivityThread(16551): Caused by: java.lang.UnsatisfiedLinkError: Native method not found: dalvik.system.VMRuntime.pauseGc:(Ljava/lang/St
ing;)I
E/ActivityThread(16551):        at dalvik.system.VMRuntime.pauseGc(Native Method)
E/ActivityThread(16551):        ... 15 more

我也搜索了这一点,发现这不是开发人员的错误(但我不知道这两个是否相关。堆栈跟踪中的细节不多),但是以上是我的代码。

我不知道现在该尝试什么,我不应该从主线程中显示webview吗?

mActivity.runOnUiThread(new Runnable() {
                public void run() {
                    mWebView = MyWebViewFactory.create(mActivity, "");
                    dialog = MyWebViewFactory.showWebView(mActivity, mWebView);
                }
            });

public static Dialog showWebView(final Activity context, final WebView webView) {
        Dialog callAlert = new Dialog(context,
                android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setLoadsImagesAutomatically(true);

        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        callAlert.requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = callAlert.getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        callAlert.setCanceledOnTouchOutside(true);
        callAlert.setContentView(webView);
        callAlert.getWindow().setBackgroundDrawable(
                new ColorDrawable(android.graphics.Color.TRANSPARENT));
        callAlert.getWindow().setLayout(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        callAlert.getWindow().setFormat(PixelFormat.TRANSLUCENT);
        callAlert.setCancelable(true);
        return callAlert;
    }

感谢。

0 个答案:

没有答案