我有 android 应用程序,它从 telnet 客户端获取巨大的字符串对象。后来我只使用大字符串的一小部分。我用
new String(Part of old string);
将新字符串字符数组与旧字符串字符数组分开。所以旧的字符串应该被垃圾收集,但令人惊讶的是新字符串仍然有对旧对象的引用。我可以用“Eclipse Memory Analyzer”看到它。这很快就会溢出我的 16Meg 应用程序内存。
如何避免这种情况?
private WifiChannel parse1(String channLine){
//scanning with "iwlist wlan0 scanning" the getChans1 method
String[] input = channLine.split(System.getProperty("line.separator"));
if (input.length < 4);
String segment[];
String segment2[];
WifiChannel chan = new WifiChannel();
try {
if (input.length > 5){
chan.setMacAddress(new String(input[0]));
segment = input[1].split(":");
chan.setChannel(Integer.parseInt(segment[1].trim()));
segment = input[3].split(" ");
segment2 = segment[20].split("=");
chan.setQuality(new String(segment2[1]));
segment2 = segment2[1].split("/");
chan.setSignalStrength((Integer.parseInt(segment2[0].trim())*100)/Integer.parseInt(segment2[1].trim())+"%");
segment2 = segment[23].split("=");
try{chan.setSignalLevel(Integer.parseInt(segment2[1].trim()));}catch(Exception e){chan.setSignalLevel(0);}
segment = input[5].split(":");
chan.setName(new String(segment[1].replaceAll("^\"|\"$", "")));
for (int i = 6;i<input.length;i++)
if (input[i].contains("Mode"))
segment = input[i].split(":");
chan.setChannelMode(new String(segment[1]));
String band = "";
if(channLine.contains("5.5 Mb/s"))band = band +"b";
if(channLine.contains("12 Mb/s") )band = band +"g";
chan.setBand(new String(band));
}
}catch (Exception e){Log.e("","",e);}
return chan;
}
方法输入也是更大字符串的一部分。