我有这个从服务运行的线程。run 的循环运行大约 15 分钟没有问题。在那之后 - 我的 GC 因内存泄漏而发疯,然后该功能就不能再正常工作了。
我已经使用 DDMS Allocation Tracker 来查找泄漏,并且似乎泄漏在input = in.readLine();
线路内。我真的不知道如何解决它,但这是我的代码、堆栈跟踪和分配跟踪器快照:
代码:
@Override
public void run() {
startKeepAliveTimer();
try {
socket = new Socket(host, port);
if (socket != null)
{
Log.i("ServerConnection", "Server connection opened");
this.app.setConnectedToServer(true);
serverManager.loginToServer();
}
else
app.setConnectedToServer(false);
InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream());
BufferedReader in = new BufferedReader(inputStreamReader);
String input;
while (app.isConnectedToServer())
{
app.getFacebookManager().refreshAccessToken();
input = in.readLine();
if (input != null)
{
lastOnline = System.currentTimeMillis();
input = input.trim();
input = app.decrypt(input);
Log.e("Server Reponse", input);
serverManager.processData(app.convertToJSONObject(input));
}
}
} catch (IOException e) {
Log.e("ServerConnection", e.toString());
}
Log.i("ServerConnection", "Server connection closed");
}
private void startKeepAliveTimer() {
Timer timer = new Timer("keepAlive", true);
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
keepAlive();
}
}, 0, keepAlive);
}
堆栈跟踪
11-12 09:55:38.349: D/dalvikvm(2251): GC_CONCURRENT freed 1164K, 23% free 9987K/12871K, paused 1ms+1ms
11-12 09:55:38.899: D/dalvikvm(2251): GC_CONCURRENT freed 1072K, 23% free 9989K/12871K, paused 1ms+1ms
11-12 09:55:39.439: D/dalvikvm(2251): GC_CONCURRENT freed 1075K, 23% free 9967K/12871K, paused 0ms+1ms
11-12 09:55:39.959: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 0ms+1ms
11-12 09:55:40.499: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9992K/12871K, paused 0ms+0ms
11-12 09:55:41.069: D/dalvikvm(2251): GC_CONCURRENT freed 1079K, 23% free 9983K/12871K, paused 1ms+1ms
11-12 09:55:41.749: D/dalvikvm(2251): GC_CONCURRENT freed 1094K, 23% free 9967K/12871K, paused 2ms+2ms
11-12 09:55:42.379: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 1ms+0ms
11-12 09:55:42.909: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 1ms+1ms
11-12 09:55:43.459: D/dalvikvm(2251): GC_CONCURRENT freed 1061K, 23% free 9967K/12871K, paused 1ms+0ms
11-12 09:55:43.979: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 1ms+0ms
11-12 09:55:44.549: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 1ms+0ms
11-12 09:55:45.079: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 1ms+1ms
11-12 09:55:45.629: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 1ms+1ms
11-12 09:55:46.179: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 1ms+0ms
11-12 09:55:46.709: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 1ms+0ms
11-12 09:55:47.229: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 1ms+0ms
11-12 09:55:47.749: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 1ms+0ms
11-12 09:55:48.299: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 1ms+1ms
11-12 09:55:48.839: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9989K/12871K, paused 1ms+0ms
11-12 09:55:49.379: D/dalvikvm(2251): GC_CONCURRENT freed 1075K, 23% free 9991K/12871K, paused 1ms+0ms
11-12 09:55:49.909: D/dalvikvm(2251): GC_CONCURRENT freed 1078K, 23% free 9967K/12871K, paused 1ms+1ms
11-12 09:55:50.429: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 3ms+1ms
11-12 09:55:50.949: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 1ms+1ms
11-12 09:55:51.499: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 1ms+1ms
11-12 09:55:52.019: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 0ms+0ms
11-12 09:55:52.539: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 1ms+1ms
11-12 09:55:53.069: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 2ms+1ms
11-12 09:55:53.609: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 1ms+0ms
11-12 09:55:54.139: D/dalvikvm(2251): GC_CONCURRENT freed 1045K, 23% free 9967K/12871K, paused 0ms+1ms
分配跟踪器: