android中的context可以做很多操作,但是最主要的功能是加载和访问资源。
在android中有两种context,一种是 application context,一种是activity context,通常我们在各种类和方法间传递的是activity context。
比如一个activity的onCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mGameView = new GameView(this);
setContentView(mGameView);
}
把activity context传递给view,意味着view拥有一个指向activity的引用,进而引用activity UI占有的资源:view , resource, SensorManager等。
但是这样如果context发生内存泄露的话,就会泄露很多内存,这里泄露的意思是gc没有办法回收activity的内存(当前Activity为活动或finish后还没来得及回收)。
Leaking an entire activity是很容易的一件事。
当屏幕旋转的时候,系统会销毁当前的activity,保存状态信息再创建一个新的。
比如我们写了一个应用程序,需要加载一个很大的图片,我们不希望每次旋转屏幕的时候都销毁这个图片重新加载。
实现这个要求的简单想法就是定义一个静态的Drawable,这样Activity 类创建销毁它始终保存在内存中,访问速度会很快。
实现类似:public class myactivity extends Activity {
private static Drawable sBackground;
protected void onCreate(Bundle state) {
super.onCreate(state);
TextView label = new TextView(this);
label.setText("Leaks are bad");
if (sBackground == null) {
sBackground = getDrawable(R.drawable.large_bitmap);
}
label.setBackgroundDrawable(sBackground);//drawable attached to a view
setContentView(label);
}
}
这段程序看起来很简单,但是却问题很大。当屏幕旋转的时候会有leak,即gc没法销毁activity
我们刚才说过,屏幕旋转的时候系统会销毁当前的activity。但是当drawable和view关联后,drawable保存了view的 reference,即sBackground保存了label的引用,而label保存了activity的引用。既然drawable不能销毁,它所引用和间接引用的都不能销毁,这样系统就没有办法销毁当前的activity,于是造成了内存泄露。gc对这种类型的内存泄露是无能为力的。
避免这种内存泄露的方法是避免activity中的任何对象的生命周期长过activity,避免由于对象对 activity的引用导致activity不能正常被销毁
同时,我们可以使用application context
application context伴随application的一生,与activity的生命周期无关。
application context可以通过Context.getApplicationContext或者Activity.getApplication方法获取。
使用Application,需要在 AndroidManifest.xml 文件注册,即android:name=".GApplication":
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:name=".GApplication">
<activity android:name=".WordSearch"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|keyboard|screenLayout">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
避免context相关的内存泄露,记住以下几点:1. 不要让生命周期长的对象引用activity context,即保证引用activity的对象要与activity本身生命周期是一样的
2. 对于生命周期长的对象,可以使用application context (继承类:public class
GApplication extends Application)
3. 尽量使用静态类(全局),避免非静态的内部类,避免生命周期问题,注意内部类对外部对象引用导致的生命周期变化
分享到:
相关推荐
Android webview 内存泄露的解决方法 最近在activity嵌套webview显示大量图文发现APP内存一直在涨,没法释放内存,查了很多资料,大概是webview的一个BUG,引用了activity导致内存泄漏,所以就尝试传递...
</activity> </application> 为了让大家更容易理解,写了一个简单的Demo.步骤如下: 第1步:新建一个Android工程ApplicationDemo,目录结构如下: 第2步:新建一个工具类ToolsUtil.java,代码如下 package ...
涉及:activity跳转、intent使用、startActivityForResult/onActivityResult,着重讲解通过来Application Context实现共享数据
android检查内存泄露和用法 <!--==============================================================================--> android:name="com.squareup.leakcanary.internal.HeapAnalyzerService" android:enabled...
本文主要介绍Android中Context引起的内存泄露的问题,这里对Context的知识做了详细讲解,说明如何避免内存泄漏的问题,有兴趣的小伙伴可以参考下
简要介绍android context 的用法
android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/...
Adding Logging Support to Your Android Application 83 Adding Some Media Support to Your Application 84 Adding Location-Based Services to Your Application 88 Debugging Your Application on Hardware 90 ...
android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/...
Android Context使用例子.
</activity> <provider android:name=".provider" android:authorities="com.example.tigongzhe.provider" android:multiprocess="true" android:exported="true" android:permission=...
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> android:id="@ id/loadView" android:layout_width="fill_parent" android:layout_height="fill_parent"/> ...
Android Native代码(C/C++)内存泄露分析 Hook内存申请本地函数,在替换函数中获取堆栈,在内存中记录申请的内存大小以及堆栈信息 提供API导出动态库泄露(申请了但是未释放)的内存大小 提供API导出动态库泄露堆栈...
本文实例讲述了Android编程中...第一种中context的生命周期与Application的生命周期相关的,context随着Application的销毁而销毁,伴随application的一生,与activity的生命周期无关。第二种中的context跟Activit
spring application-context xml说明
作者:Hewi ...来源:知乎 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。...首先,Activity的Context和Application的Context肯定不是一个东西,一个是当前活动的 Context,它的生命周期仅限
android Context Menu With Icon
Android全局变量和Context的实现方法
android中一个对象已经不需要了,但是其他对象还持有他的引用,导致他不能回收,导致这个对象暂存在内存中,这样内存泄漏就出现了。 内存泄漏出现多了,会是应用占用过多的没存,当占用的内存超过了系统分配的内存...