在我的安卓应用程序中,我有一个使用AsyncTask填充的表(在现实生活中,它所做的不仅仅是几个文本视图,但是这个简化的代码也可以看出问题):
private class RowPainter extends AsyncTask<Statistics, Void, Void>
{
private final WeakReference<TableRow> rowReference;
public RowPainter(TableRow row)
{
rowReference = new WeakReference<>(row);
}
@Override
protected Void doInBackground(Statistics... statistics)
{
fillDetailsRow(statistics[0]);
return null;
}
@Override
protected void onPostExecute(Void aVoid)
{
rowReference.clear();
}
private void fillDetailsRow(Statistics stats)
{
fillText(R.id.name, stats.name, rowReference);
fillText(R.id.level, printableValue(stats.level), rowReference);
fillText(R.id.total_count, printableValue(stats.TotalCount), rowReference);
fillText(R.id.track_score, printableValue(stats.calculateScore()), rowReference);
}
private void fillText(int viewId, String text, WeakReference<TableRow> reference)
{
TableRow row = reference.get();
if (row == null)
return;
TextView textView = (TextView) row.findViewById(viewId);
textView.setText(text);
}
private String printableValue(int value)
{
return value == 0 ? "" : String.format("%,d", value);
}
}这段代码在我使用Android4.4的三星Galaxy手机上工作得很好,但在我使用Android5.0.2的新三星Galaxy边缘上,它会在我将设备旋转3-4次后产生内存错误(我可以在没有问题的情况下旋转我的SIII几十次)。当我在Android上运行SIII时,我的内存始终保持在64 my以下,但是S6边缘.-它从64 at开始,每次我旋转设备时,它增加了大约30-40兆字节,从来没有下降过。我不知道我在这里错过了什么。
发布于 2015-07-19 03:11:53
我想,我知道我的问题的答案。我用Android5在不同的设备上测试了我的应用程序,其中一些运行良好,而另一些很快就会出现OutOfMemory错误。我已经将代码从使用AsyncTasks转换为使用Runnable --一切都恢复了正常。没有更奇怪的内存错误,runnable做它的工作,并释放内存,这是它应该做的。
https://stackoverflow.com/questions/31364370
复制相似问题