编辑:我认为解析 xml 的代码中存在逻辑错误,因此如果编写正确,则不应有任何泄漏。
所以我在我的项目中添加了一个 AsyncTask 类,这是我前段时间为了快速测试目的而编写的,但它会导致内存泄漏错误。
我确信它是负责任的,因为当我移除它时,泄漏问题就消失了。
这是堆转储文件(已转换)http://www7.zippyshare.com/v/83628026/file.html
package com.example.xmldl;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.os.AsyncTask;
import android.util.Xml;
public class Dlxml extends AsyncTask<String, Void, Void> {
//adding or removing Override didnt matter just to let you know
InputStream istream = null;
protected Void doInBackground(String... url){
try {
downloadXml(url[0]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private void downloadXml(String url) throws IOException {
try {
URL mUrl = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) mUrl.openConnection();
istream = urlConnection.getInputStream();
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(istream, null);
parser.nextTag();
parser.require(XmlPullParser.START_TAG, null, "resources");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() == XmlPullParser.TEXT) {
parser.nextTag();
}
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
istream.close();
}
}
}