在我的应用程序中,我使用了 ListView 和列表适配器。当单击 ListView 中的某个子项时,会出现一个可单击的 TextView ,它将多个位图加载到 ScrollView 中 - 并且此 ScrollView 显示在警报对话框中。
所有这一切都发生在一个扩展 BaseExpandableListAdapter 的类中,当单击此文本链接时 - 将调用一个静态内部类,它负责加载所有这些 (9) 位图。这个内部类扩展了 asynctask。
在将这些位图加载到 ScrollView 之前 - 这个内部类的两个静态方法被调用,将位图缩小到适合屏幕的大小。这里我使用 Bitmapfactory.decoderesource 和 Bitmap.scaledownBitmap。
所有这一切都工作得很好,..但是程序存在内存泄漏。这个泄漏之前相当大,因为这个内部类是非静态的。因此,通过将此内部类设为静态来减少泄漏。是的——减少了,但没有消除。
我也对几个对象进行了弱引用,但没有成功。例如 - 我对引用内部类的对象进行了弱引用。我对传递给内部类的上下文进行了弱引用。我什至对位图做了 w weak reference。根本没有成功。
我的 Samsung Galazy s3 的堆大小是 64 MB。当 ListView 及其所有子项首次加载时,使用的堆大约为 17 MB。然后,当加载 9 个位图时,它会增加到大约 42 MB。如果我然后单击另一个子项目,使用的图像堆是相同的 - 但在继续单击并加载位图后,堆突然变大到 47 MB ... 然后相同的场景...... 静止一段时间 - 然后高达 52 MB .... 56 MB。所以我几乎必须单击并加载位图才能使内存不足。比如说 15 - 20 分钟的密集使用。
结论:使内部类静态化有助于我减少内存泄漏。但是尽管对几个对象(尤其是上下文)进行了弱引用,但我无法进一步减少泄漏。
有什么建议吗?
下面的代码有点乱....
static class BitmapWorkerTask extends AsyncTask <Integer, Void, Bitmap[]> {
private int[] data;
private int[] width, height;
private int nmbrOfImages;
private String[] scrollText;
private ImageView mImage;
private View view;
private LayoutInflater factory;
private AlertDialog.Builder alertadd;
private Context context;
private WeakReference <Context> sc;
private WeakReference <ImageView> mImageV;
private WeakReference <Bitmap[]> bitmapV;
public BitmapWorkerTask(int[] width, int[] height, int nmbrOfImages, String[] scrollText, Context context) {
this.width = width;
this.height = height;
this.nmbrOfImages = nmbrOfImages;
this.scrollText = scrollText;
this.context = context;
mImage = null;
view = null;
factory = null;
alertadd = null;
System.gc();
sc = new WeakReference <Context> (context);
try {
for (int i = 0; i < scaledBitmap.length; i++) {
scaledBitmap[i].recycle();
scaledBitmap[i] = null;
}
} catch (NullPointerException ne) {
System.out.println("nullpointerexception ... gick inte recycla bitmapbilder");
}
switch (nmbrOfImages) {
case 0:
data = new int[1];
break;
case 1:
data = new int[3];
break;
case 2:
data = new int[5];
break;
case 3:
data = new int[9];
break;
}
}
@Override
protected Bitmap[] doInBackground(Integer ... params) {
switch (nmbrOfImages) {
case 0:
data[0] = params[0];
break;
case 1:
data[0] = params[0];
data[1] = params[1];
data[2] = params[2];
break;
case 2:
data[0] = params[0];
data[1] = params[1];
data[2] = params[2];
data[3] = params[3];
data[4] = params[4];
break;
case 3:
data[0] = params[0];
data[1] = params[1];
data[2] = params[2];
data[3] = params[3];
data[4] = params[4];
data[5] = params[5];
data[6] = params[6];
data[7] = params[7];
data[8] = params[8];
break;
}
alertadd = new AlertDialog.Builder(sc.get());
factory = LayoutInflater.from(sc.get());
Bitmap[] bm = decodeSampledBitmapFromResource(sc.get().getResources(), data, width, height);
bitmapV = new WeakReference <Bitmap[]> (bm);
return bitmapV.get();
}
protected void onPostExecute(Bitmap[] bitmap) {
switch (nmbrOfImages) {
case 0:
if (view == null) {
view = factory.inflate(R.layout.alertviews, null);
}
mImage = (ImageView) view.findViewById(R.id.extra_img);
mImage.setImageBitmap(bitmap[0]);
alertadd.setView(view);
alertadd.setNeutralButton("Here!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}
});
alertadd.show();
break;
case 1:
if (view == null) {
view = factory.inflate(R.layout.alertviews2, null);
}
mImage = (ImageView) view.findViewById(R.id.img1);
mImage.setImageBitmap(bitmap[0]);
mImage = (ImageView) view.findViewById(R.id.img2);
mImage.setImageBitmap(bitmap[1]);
mImage = (ImageView) view.findViewById(R.id.img3);
mImage.setImageBitmap(bitmap[2]);
try {
TextView mText2 = (TextView) view.findViewById(R.id.text_img1_scrollview);
mText2.setText(scrollText[0]);
mText2 = (TextView) view.findViewById(R.id.text_img2_scrollview);
mText2.setText(scrollText[1]);
mText2 = (TextView) view.findViewById(R.id.text_img3_scrollview);
mText2.setText(scrollText[2]);
} catch (NullPointerException ne) {
System.out.println("nullpointerexception ... TextView i metoden onPostExecute");
}
alertadd.setView(view);
alertadd.setNeutralButton("Here!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}
});
alertadd.show();
break;
case 2:
if (view == null) {
view = factory.inflate(R.layout.alertviews3, null);
}
mImage = (ImageView) view.findViewById(R.id.img1);
mImage.setImageBitmap(bitmap[0]);
mImage = (ImageView) view.findViewById(R.id.img2);
mImage.setImageBitmap(bitmap[1]);
mImage = (ImageView) view.findViewById(R.id.img3);
mImage.setImageBitmap(bitmap[2]);
mImage = (ImageView) view.findViewById(R.id.img4);
mImage.setImageBitmap(bitmap[3]);
mImage = (ImageView) view.findViewById(R.id.img5);
mImage.setImageBitmap(bitmap[4]);
try {
TextView mText3 = (TextView) view.findViewById(R.id.text_img1_scrollview);
mText3.setText(scrollText[0]);
mText3 = (TextView) view.findViewById(R.id.text_img2_scrollview);
mText3.setText(scrollText[1]);
mText3 = (TextView) view.findViewById(R.id.text_img3_scrollview);
mText3.setText(scrollText[2]);
mText3 = (TextView) view.findViewById(R.id.text_img4_scrollview);
mText3.setText(scrollText[3]);
mText3 = (TextView) view.findViewById(R.id.text_img5_scrollview);
mText3.setText(scrollText[4]);
} catch (NullPointerException ne) {
System.out.println("nullpointerexception ... TextView i metoden onPostExecute");
}
alertadd.setView(view);
alertadd.setNeutralButton("Here!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}
});
alertadd.show();
break;
case 3:
if (view == null) {
view = factory.inflate(R.layout.alertviews4, null);
}
AlertDialog.Builder alertadd = new AlertDialog.Builder(context);
mImage = (ImageView) view.findViewById(R.id.img1);
mImage.setImageBitmap(bitmap[0]);
mImage = (ImageView) view.findViewById(R.id.img2);
mImage.setImageBitmap(bitmap[1]);
mImage = (ImageView) view.findViewById(R.id.img3);
mImage.setImageBitmap(bitmap[2]);
mImage = (ImageView) view.findViewById(R.id.img4);
mImage.setImageBitmap(bitmap[3]);
mImage = (ImageView) view.findViewById(R.id.img5);
mImage.setImageBitmap(bitmap[4]);
mImage = (ImageView) view.findViewById(R.id.img6);
mImage.setImageBitmap(bitmap[5]);
mImage = (ImageView) view.findViewById(R.id.img7);
mImage.setImageBitmap(bitmap[6]);
mImage = (ImageView) view.findViewById(R.id.img8);
mImage.setImageBitmap(bitmap[7]);
mImage = (ImageView) view.findViewById(R.id.img9);
mImage.setImageBitmap(bitmap[8]);
try {
TextView mText4 = (TextView) view.findViewById(R.id.text_img1_scrollview);
mText4.setText(scrollText[0]);
mText4 = (TextView) view.findViewById(R.id.text_img2_scrollview);
mText4.setText(scrollText[1]);
mText4 = (TextView) view.findViewById(R.id.text_img3_scrollview);
mText4.setText(scrollText[2]);
mText4 = (TextView) view.findViewById(R.id.text_img4_scrollview);
mText4.setText(scrollText[3]);
mText4 = (TextView) view.findViewById(R.id.text_img5_scrollview);
mText4.setText(scrollText[4]);
mText4 = (TextView) view.findViewById(R.id.text_img6_scrollview);
mText4.setText(scrollText[5]);
mText4 = (TextView) view.findViewById(R.id.text_img7_scrollview);
mText4.setText(scrollText[6]);
mText4 = (TextView) view.findViewById(R.id.text_img8_scrollview);
mText4.setText(scrollText[7]);
mText4 = (TextView) view.findViewById(R.id.text_img9_scrollview);
mText4.setText(scrollText[8]);
} catch (NullPointerException ne) {
System.out.println("nullpointerexception ... TextView i metoden onPostExecute");
}
alertadd.setView(view);
alertadd.setNeutralButton("Here!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}
});
alertadd.show();
break;
}
}
/**
*
* @param options
* @param reqW
* @param reqH
* @return
*/
public static int calculateInSampleSize(BitmapFactory.Options options, int reqW, int reqH) {
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
int inSampleSize = 1;
if (imageHeight > reqH || imageWidth > reqW) {
int heightRatio = Math.round((float) imageHeight / (float) reqH);
int widthRatio = Math.round((float) imageWidth / (float) reqW);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
System.out.println("i if-satsen!");
System.out.println("height-ratio: " + heightRatio + "\nwidth-ratio: " + widthRatio);
}
System.out.println("samplesize: " + inSampleSize);
inSampleSize = inSampleSize;
return inSampleSize;
}
@SuppressLint("NewApi")
/**
*
* @param res
* @param resId
* @param reqW
* @param reqH
* @return
*/
public static Bitmap[] decodeSampledBitmapFromResource(Resources res, int[] resId, int[] reqW, int[] reqH) {
scaledBitmap = new Bitmap[resId.length];
BitmapFactory.Options options;
for (int i = 0; i < resId.length; i++) {
options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bm =BitmapFactory.decodeResource(res, resId[i], options);
System.out.println("ursprunglig bild: h = " + options.outHeight + " w = " + options.outWidth);
options.inSampleSize = calculateInSampleSize(options, reqW[i], reqH[i]);
while (options.outHeight < reqH[i] || options.outWidth < reqW[i]) {
options.inSampleSize--;
System.out.println("räknar nu ner insampleseize\ninSamleSize =" + options.inSampleSize);
}
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeResource(res, resId[i], options);
System.out.println("innan omskalning: h = " + options.outHeight + " w = " + options.outWidth);
System.out.println("antalet bytes: " + bm.getByteCount());
System.out.println("native free size: " + Debug.getNativeHeapFreeSize() );
scaledBitmap[i] = Bitmap.createScaledBitmap(bm, reqW[i], reqH[i], true);
bm.recycle();
bm = null;
}
System.gc();
WeakReference <Bitmap[] > sc = new WeakReference <Bitmap[]> (scaledBitmap);
return sc.get();
}
}
最佳答案
您保持对位图的双重引用 曾经很弱,曾经是硬拷贝 你的问题代码是:
Bitmap[] bm = decodeSampledBitmapFromResource(sc.get().getResources(), data, width, height);
bitmapV = new WeakReference <Bitmap[]> (bm);
你没有去掉 bm 的内容
而且你不止一次这样做
scaledBitmap[i] = Bitmap.createScaledBitmap(bm, reqW[i], reqH[i], true);
bm.recycle();
bm = null;
}
System.gc();
WeakReference <Bitmap[] > sc = new WeakReference <Bitmap[]> (scaledBitmap);
你应该这样做:
ArrayList<WeakReference<Bitmap>>scaledBitmaps= new ArrayList<WeakReference<Bitmap>>();
scaledBitmaps.add(new WeakReference<Bitmap>(Bitmap.createScaledBitmap...));
and do not use Bitmap[] array
关于android - 尽管使用弱引用内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17272474/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h