4

这个问题是 我昨天发布的这个问题的扩展。我发现了可能的内存泄漏,并以正确的方式处理了这个问题。由非静态内部类引起的内存泄漏之一,更准确地说是非静态的Handler

但是我今天面临一个新的内存泄漏问题。我读过这篇文章,本质是基于ContextActivity 或 Application Context。 在此处输入图像描述

这是我从Eclipse Memory Analyzer

我已经看到Remainder这个饼图的变化,在修复问题后Handler,剩余部分增加了 10.6 MB。

但是到了上下文问题,这Problem Suspect 2给了我一个提示在哪里看

35 instances of "android.widget.FrameLayout", loaded by "<system class loader>" occupy 39 218 464 (39,94%) bytes. 

Biggest instances:

•android.widget.FrameLayout @ 0x425aa258 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x425d9b20 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x42609258 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x4260a248 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x42925960 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x429808e0 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x429a4350 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x429d9b20 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x429e5710 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x42a28c98 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x42a51b80 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x46a8caf0 - 3 266 728 (3,33%) bytes. 

它告诉我内存泄漏发生在我使用FrameLayout. 我使用 FrameLayout 的唯一地方是在我的Splash Screen.

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    utils = new AppUtils(getApplicationContext());
    pd = (ProgressBar) findViewById(R.id.pd);
    progress = (TextView) findViewById(R.id.progress);
    version = (TextView)findViewById(R.id.version);
    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    SharedPreferences.Editor prefEditor = prefs.edit();
    prefEditor.putString("firstpw", "first");
    prefEditor.commit();

    folder = new CreateApplicationFolder(getApplicationContext(), true); 
    boolean isSDPresent = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);

    if (!isSDPresent) {
        ErrorDialog dialog = new ErrorDialog(getApplicationContext(), getResources().getString(R.string.sdCardTitle),
                getResources().getString(R.string.sdCardContent),
                getResources().getString(R.string.ok), 2);
        Log.i("SDCARD: ", "Is Not Present");
        dialog.canCancelDialog(false);

    } else {
        Log.i("SDCARD: ", "Is Present");

        version.setText("Version: " + utils.getAppVersion()); 

        try {
            new ReadConfig(Splash.this, pd, progress).execute("");
        } catch (ParserConfigurationException e) {
            Log.e("Parser: ", e.getMessage());
        } catch (SAXException e) {
            Log.e("Sax: ", e.getMessage());
        } catch (IOException e) {
            Log.e("IO: ", e.getMessage());
        }
    }
}

我怀疑当我将 Activity 上下文发送到 AsyncTask 时会发生内存泄漏ReadConfig,如下所示:

try {
            new ReadConfig(Splash.this, pd, progress).execute("");
        } catch (ParserConfigurationException e) {
            Log.e("Parser: ", e.getMessage());
        } catch (SAXException e) {
            Log.e("Sax: ", e.getMessage());
        } catch (IOException e) {
            Log.e("IO: ", e.getMessage());
        }

在 ReadConfig 类中,我还使用此上下文Activity在方法中启动一个新onPostExecute方法,这将使此上下文保持活动状态。

protected void onPostExecute(String result) {

        if (result.equals("noConfig")) {
            Toast.makeText(context, context.getResources().getString(R.string.configNotFound),
                    Toast.LENGTH_LONG).show();
            Intent mainIntent = new Intent(context, MainClass.class);
            context.startActivity(mainIntent);
        }
        else if(result.equals("pathnotFound")) {

            new ErrorDialog(context, context.getResources().getString(R.string.noBTFolderTitle), context.getResources().getString(R.string.noBtFolderContent), context.getResources().getString(R.string.exit), 2);

        } else {
            Intent mainIntent = new Intent(context, MainClass.class);
            context.startActivity(mainIntent);
        }
    }

我需要上下文来启动这个新的活动,从资源中获取字符串等等。有什么可能的方法可以让我以不同的方式来避免内存泄漏?感谢您阅读这篇大文章,如果您需要更多代码或其他任何内容,只需添加评论即可。

提前致谢。

4

1 回答 1

3

您可能应该尽可能尝试使用 Application Context 而不是 Activity 上下文(这并不总是可能的)。

要从任何地方访问您的应用程序上下文,您可以定义自己的应用程序类

class MyApp extends Application
{
private static MyApp mInstance;

@Override
public void onCreate()
{
   mInstance = this;
}

public static Context context()
{
   return mInstance.getApplicationContext();
}

}

然后你在你的清单中声明这个类

<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" android:name="com.myapp.MyAppClass">

并在适用的情况下使用 MyApp.context()

于 2012-11-22T09:12:28.750 回答