Android,谁动了我的内存(1)
16lz
2021-01-23
Android,谁动了我的内存(1) 2011-03-11 00:03:57 标签: 内存泄露 android OutOfMemory Bitmap Cursor 版权声明:原创作品,谢绝转载!否则将追究法律责任。
- 由于我们程序的失误,长期保持某些资源(如Context)的引用,造成内存泄露,资源造成得不到释放。
- 保存了多个耗用内存过大的对象(如Bitmap),造成内存超出限制。
- publicclassClassName{
- privatestaticContextmContext;
- //省略
- }
- privatestaticDrawablesBackground;
- @Override
- protectedvoidonCreate(Bundlestate){
- super.onCreate(state);
- TextViewlabel=newTextView(this);
- label.setText("Leaksarebad");
- if(sBackground==null){
- sBackground=getDrawable(R.drawable.large_bitmap);
- }
- label.setBackgroundDrawable(sBackground);
- setContentView(label);
- }
- publicclassMyActivityextendsActivity{
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- newMyThread().start();
- }
- privateclassMyThreadextendsThread{
- @Override
- publicvoidrun(){
- super.run();
- //dosomthing
- }
- }
- }
Android,谁动了我的内存(2) 2011-03-11 00:06:39 标签: OutOfMemory android 内存泄露 Bitmap Cursor 版权声明:原创作品,谢绝转载!否则将追究法律责任。
- publicabstractclassWeakAsyncTask<Params,Progress,Result,WeakTarget>extends
- AsyncTask<Params,Progress,Result>{
- protectedWeakReference<WeakTarget>mTarget;
- publicWeakAsyncTask(WeakTargettarget){
- mTarget=newWeakReference<WeakTarget>(target);
- }
- /**{@inheritDoc}*/
- @Override
- protectedfinalvoidonPreExecute(){
- finalWeakTargettarget=mTarget.get();
- if(target!=null){
- this.onPreExecute(target);
- }
- }
- /**{@inheritDoc}*/
- @Override
- protectedfinalResultdoInBackground=\'#\'" /span>
- finalWeakTargettarget=mTarget.get();
- if(target!=null){
- returnthis.doInBackground=\'#\'" /span>
- }else{
- returnnull;
- }
- }
- /**{@inheritDoc}*/
- @Override
- protectedfinalvoidonPostExecute(Resultresult){
- finalWeakTargettarget=mTarget.get();
- if(target!=null){
- this.onPostExecute(target,result);
- }
- }
- protectedvoidonPreExecute(WeakTargettarget){
- //Nodefaultaction
- }
- protectedabstractResultdoInBackground(WeakTargettarget,Params...params);
- protectedvoidonPostExecute(WeakTargettarget,Resultresult){
- //Nodefaultaction
- }
- }
- privateImageViewpreview;
- BitmapFactory.Optionsoptions=newBitmapFactory.Options();
- options.inSampleSize=2;//图片宽高都为原来的二分之一,即图片为原来的四分之一
- Bitmapbitmap=BitmapFactory.decodeStream(cr.openInputStream(uri),null,options);
- preview.setImageBitmap(bitmap);
- /**本例子为博主随手一写,来说明用法,并未验证*/
- privateclassMyAdapterextendsBaseAdapter{
- privateArrayList<SoftReference<Bitmap>>mBitmapRefs=newArrayList<SoftReference<Bitmap>>();
- privateArrayList<Value>mValues;
- privateContextmContext;
- privateLayoutInflatermInflater;
- MyAdapter(Contextcontext,ArrayList<Value>values){
- mContext=context;
- mValues=values;
- mInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
- publicintgetCount(){
- returnmValues.size();
- }
- publicObjectgetItem(inti){
- returnmValues.get(i);
- }
- publiclonggetItemId(inti){
- returni;
- }
- publicViewgetView(inti,Viewview,ViewGroupviewGroup){
- ViewnewView=null;
- if(view!=null){
- newView=view;
- }else{
- newView=(View)mInflater.inflate(R.layout.image_view,false);
- }
- Bitmapbitmap=BitmapFactory.decodeFile(mValues.get(i).fileName);
- mBitmapRefs.add(newSoftReference<Bitmap>(bitmap));//此处加入ArrayList
- ((ImageView)newView).setImageBitmap(bitmap);
- returnnewView;
- }
- }
- Cursorcursor=null;
- try{
- cursor=mContext.getContentResolver().query(uri,null,null,null,null);
- if(cursor!=null){
- cursor.moveToFirst();
- //dosomething
- }
- }catch(Exceptione){
- e.printStackTrace();
- }finally{
- if(cursor!=null){
- cursor.close();
- }
- }
- @Override
- protectedvoidonDestroy(){
- if(mAdapter!=null&&mAdapter.getCurosr()!=null){
- mAdapter.getCursor().close();
- }
- super.onDestroy();
- }
更多相关文章
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面 .
- Android中的Parcel机制 实现Bundle传递对象
- Android 面试必备 - 线程
- android 进程与线程 - 开发文档翻译 - 进程
- android 进程与线程 - 开发文档翻译 - 线程
- android内存处理机制
- Android 中三种使用线程的方法