Monday, March 3, 2014

Android how to solve it, but use a picture memory leak problem?


Internet search a bit, Android assigned to the picture, but the memory is only 8M, so when a lot of pictures to use but when designed into the memory leak problem, online simply but said the need to recycle () it, it was in use Bitmap time, then If you use the picture when it comes to Bitmap does not it?
Here is a program that can run, but if the picture more memory leak occurs, ask how to solve it
 
package com.loulijun.imageswitcher;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class ImageSwitcherActivity extends Activity implements ViewFactory {
private ImageSwitcher is;
private Gallery gallery;
/ / Image Resources
private Integer [] thumbs = {
R.drawable.yuanyuan01,
R.drawable.yuanyuan02,
R.drawable.yuanyuan03,
R.drawable.yuanyuan04,
R.drawable.yuanyuan05,
R.drawable.yuanyuan06,
R.drawable.yuanyuan07,
R.drawable.yuanyuan08
};

private Integer [] imgIds = {
R.drawable.yuanyuan01,
R.drawable.yuanyuan02,
R.drawable.yuanyuan03,
R.drawable.yuanyuan04,
R.drawable.yuanyuan05,
R.drawable.yuanyuan06,
R.drawable.yuanyuan07,
R.drawable.yuanyuan08
};
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
/ / Set to full-screen mode
requestWindowFeature (Window.FEATURE_NO_TITLE);
setContentView (R.layout.main);

is = (ImageSwitcher) findViewById (R.id.switcher);
is.setFactory (this);
/ / Fade
is.setInAnimation (AnimationUtils.loadAnimation (this, android.R.anim.fade_in));
is.setOutAnimation (AnimationUtils.loadAnimation (this, android.R.anim.fade_out));

gallery = (Gallery) findViewById (R.id.gallery);
gallery.setAdapter (new ImageAdapter (this));
gallery.setOnItemSelectedListener (new Gallery.OnItemSelectedListener ()
{

public void onItemSelected (AdapterView arg0, View arg1,
int arg2, long arg3) {
is.setImageResource (imgIds [arg2]);

}

public void onNothingSelected (AdapterView arg0) {
/ / TODO Auto-generated method stub

}

});
}
public View makeView () {
ImageView i = new ImageView (this);
i.setBackgroundColor (0xFF000000);
i.setScaleType (ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams (new ImageSwitcher.LayoutParams (
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return i;
}
public class ImageAdapter extends BaseAdapter
{
/ / Constructor
rewriteprivate Context context;
public ImageAdapter (Context c)
{
context = c;
}

/ / Returns the number of pictures
public int getCount () {
/ / TODO Auto-generated method stub
return thumbs.length;
}

public Object getItem (int position) {
/ / TODO Auto-generated method stub
return position;
}

public long getItemId (int position) {
/ / TODO Auto-generated method stub
return position;
}

public View getView (int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView (context);
iv.setImageResource (thumbs [position]);
iv.setAdjustViewBounds (true);
iv.setLayoutParams (new Gallery.LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

return iv;
}

}

}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Good question
Non-Bitmap image is too large memory leaks caused by excessive solution solicitation

Reply:
Concerned about the Citie .........
Reply:
This code can not see where you have a memory leak is found in the operation of the memory has been increased until OOM error, or a start on the reported OOM mistake.
Reply:
Look. . . . .
Reply:
Virtual memory is automatically managed, did not timely recovery, and memory requirements increase. . . Casual. . .
Reply:
reference to the third floor fontlose reply:
this code can not see where you are found to have a memory leak has been increasing in memory until OOM error in the operation of, or a start, they reported OOM mistake.

When I started running is no problem, but in the Gallery, click a few pictures after the wrong
Error is:
dalvikvm.3775488-byte external allocation too large for this process
Graphics VM won't let us allocate 3775488 bytes

I removed a couple of the big picture, it can barely run, but in practice this situation is not enough ah
Reply:
In the blog encountered a big garden of God, but just tell me
can use the following method write a global resource management class, with Map and soft references to save these pictures resources, rather than a direct reference to the resource file
But honestly, I do not understand, you who understand that the next
Reply:
Gorgeous Bangding
Reply:
Memory tracking clicks when changing System.out.println (Runtime.getRuntime () totalMemory ().); This value change operation that does not have much memory there is no leak.

The only time you need to click on the picture to turn resources into less memory bitmap display the application, 8M bit smaller turn a large image into a bitmap are several M debug time can be set dalvik / system / build.prop this document. vm.heapsize located bigger.

Reply:
Gorgeous Bangding ~ ~ ~
Reply:
Concerned about the Citie, learn together ^ ^ ^ ^
Reply:
Image compression is handled a little bit?
Reply:
Professionals to understand it

Reply:
Replies can get 10 points a day available points! Tips:
Reply:
Also have this problem!!!
Reply:
Learn
Reply:
Learning.
Reply:
Come to learn
Reply:
The reply was deleted at the moderator 2011-11-24 11:31:21

Reply:
Explain the good
Reply:
Take 10 minutes a day
Reply:
We look Solution
Reply:
reference to the second floor duaozhuan0 reply:
attention Citie .........

Concerned about the Citie
Reply:
The reply was deleted at 2012-08-28 09:13:21 moderator

Reply:
Last encountered this problem, but also did not get


The final solution is to compress the time the art picture quality, and can not be used because JPG is a compressed format JPG itself, will be used to decode it.


There is, when direct references, in fact, is the background
bitmap decoding through




I am limited, rough guess who the man of God said, meaning that such references do not write directly R.drawable.xxx, write a class directly through bitmap loaded, and then manage their own reading and release resources. Give the program "a reference."

But the last time we tried this, picture resources is large enough, here is say a single image is too large, or too many pictures exist, or will this problem is still not resolved
Reply:
Attention and learning.
Reply:
The top one, learn
Reply:
Sustained attention in
Reply:
Continue to look at!
Reply:
Thank you. Good stuff. Are learning helpful
Reply:
This is useful, powerful!
Reply:
Oh, do not give ah of resource
Reply:
My next concern is generally the use recyle more
Reply:
Java soft references refer to the SoftReference, android source code, there are many applications where the use of soft, you can refer to.
Reply:
Learning learn
Reply:
Or playing cool
CManage their own memory, somehow leak does not exist, then everything is in their control.
Reply:
cited 36 floor artstealer reply:
or playing cool
CManage their own memory, somehow leak does not exist, then everything is in their control.

Because Android is introduced in C, which leads to memory leaks. JAVA virtual machine automatically reclaims memory the set
Reply:
Learn. Increase their knowledge of the
Reply:
Learn!!!!!
Reply:
Bangding, learning a ~ haha
Reply:
To solve this problem is not very good ....
Reply:
cited 37 floor hualangdao reply:
cited 36 floor artstealer reply:

Or playing cool
CManage their own memory, somehow leak does not exist, then everything is in their control.

Because Android is introduced in C, which leads to memory leaks. JAVA set of virtual machines automatically reclaims memory

Android also has a virtual machine, and automatic recovery, and C does not matter. Mobile resources are already strained, no problem to get hold of the virtual machine, but also put forward a memory can not be controlled.
The so-called garbage collection, and WC almost had pulled a punch line after using the toilet, and automatic recovery of it, do your own advantage is rushed, there are experts to help you looked after. In fact, could have been justified, in China, a large public places toilets are dirty, clean nobody really can not. But if every family to help you arrange for a full-flush toilets, Java father ostentatious, not afraid of trouble, asked him once every few seconds: Shangwancesuo No, he loved it. But the phone does not like it, you do not trouble trouble Ah, what the broken toilet, I look to you vent.
Reply:
Well, I come and collect points.
Reply:
Attention. . . .
Reply:
Attention. . . . . . .
Reply:
reference to the 34th floor fwgreat reply:
soft references refer to the java SoftReference, android source code, there are many applications where the use of soft, you can refer to.


Well, thank you ha
Reply:
Can not read but want to learn under
Reply:
lz code a little problem when pass Context to pass ApplicationContext, but do not pass go directly to the Activity. So easily cause the entire Activity leaks.
Recommend a look at Android SDK documentation lz resources \ articles \ avoiding-memory-leaks.html

Sometimes OOM is not necessarily a memory leak, the picture may be too large, the decoding time, the need for down-sampling.
Reply:
Look. . . .
Reply:
reference to the 6th floor hualangdao reply:
reference to the third floor fontlose reply:

This code can not see where you have a memory leak is found in the operation of the memory has been increased until OOM error, or a start on the reported OOM mistake.

When I started running is no problem, but in the Gallery, click a few pictures after the wrong
Error is:
dalvikvm.3775488-byte external allocation too large for this process
Grap ......


A picture 3.7M, lz using a picture of how much ah. . .
Do not use R. drawable, use Bitmap.
Decoding must be down-sampling, and color space set to RGB565. Must promptly recycle after use.

Some say the Soft / Weak Reference has some effect, but this is the system control, no control when releasing their own good.

No comments:

Post a Comment