我的应用程序有一个Service with Thread,它在后台运行并侦听来自远程服务器的通信。 当线程正在运行时,我在我的LogCat中看到了很多dalvikvm动作。 好吗?或者它可能是一些内存泄漏:
这是我的Thread run():
@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);
while (app.isConnectedToServer())
{
app.getFacebookManager().refreshAccessToken();
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String input;
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");
}
这是我的StackTrace:
paused 8ms
11-09 15:42:36.862: D/dalvikvm(3806): GC_CONCURRENT freed 1367K, 33% free 10661K/15815K, paused 1ms+2ms
11-09 15:42:36.892: D/dalvikvm(3806): GC_CONCURRENT freed 1390K, 33% free 10661K/15815K, paused 1ms+1ms
11-09 15:42:36.902: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 9ms
11-09 15:42:36.932: D/dalvikvm(3806): GC_CONCURRENT freed 1366K, 33% free 10661K/15815K, paused 1ms+1ms
11-09 15:42:36.952: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 8ms
11-09 15:42:36.972: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:36.992: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 12ms
11-09 15:42:37.022: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 2ms+1ms
11-09 15:42:37.032: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 8ms
11-09 15:42:37.052: D/dalvikvm(3806): GC_CONCURRENT freed 8K, 33% free 10644K/15815K, paused 1ms+1ms
11-09 15:42:37.072: D/dalvikvm(3806): GC_FOR_ALLOC freed 1406K, 33% free 10629K/15815K, paused 9ms
11-09 15:42:37.092: D/dalvikvm(3806): GC_CONCURRENT freed 1245K, 33% free 10652K/15815K, paused 1ms+0ms
11-09 15:42:37.112: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 8ms
11-09 15:42:37.132: D/dalvikvm(3806): GC_CONCURRENT freed 8K, 33% free 10644K/15815K, paused 1ms+1ms
11-09 15:42:37.152: D/dalvikvm(3806): GC_FOR_ALLOC freed 1406K, 33% free 10629K/15815K, paused 9ms
11-09 15:42:37.182: D/dalvikvm(3806): GC_CONCURRENT freed 1245K, 25% free 12018K/15815K, paused 1ms+1ms
11-09 15:42:37.212: D/dalvikvm(3806): GC_CONCURRENT freed 3208K, 33% free 10664K/15815K, paused 1ms+0ms
11-09 15:42:37.232: D/dalvikvm(3806): GC_FOR_ALLOC freed 1426K, 33% free 10637K/15815K, paused 11ms
11-09 15:42:37.252: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+0ms
11-09 15:42:37.272: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 9ms
11-09 15:42:37.302: D/dalvikvm(3806): GC_CONCURRENT freed 1366K, 33% free 10661K/15815K, paused 3ms+0ms
11-09 15:42:37.312: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 9ms
11-09 15:42:37.332: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+0ms
11-09 15:42:37.362: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 7ms
11-09 15:42:37.402: D/dalvikvm(3806): GC_CONCURRENT freed 1360K, 33% free 10667K/15815K, paused 1ms+1ms
11-09 15:42:37.432: D/dalvikvm(3806): GC_CONCURRENT freed 1299K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:37.452: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 13ms
11-09 15:42:37.472: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:37.492: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 12ms
11-09 15:42:37.512: D/dalvikvm(3806): GC_CONCURRENT freed 1366K, 33% free 10661K/15815K, paused 1ms+0ms
11-09 15:42:37.532: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 8ms
11-09 15:42:37.552: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:37.572: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 8ms
11-09 15:42:37.622: D/dalvikvm(3806): GC_CONCURRENT freed 1266K, 32% free 10761K/15815K, paused 1ms+1ms
11-09 15:42:37.642: D/dalvikvm(3806): GC_FOR_ALLOC freed 1562K, 33% free 10629K/15815K, paused 13ms
11-09 15:42:37.672: D/dalvikvm(3806): GC_CONCURRENT freed 1367K, 33% free 10653K/15815K, paused 1ms+1ms
11-09 15:42:37.692: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 10ms
11-09 15:42:37.712: D/dalvikvm(3806): GC_CONCURRENT freed 1366K, 33% free 10661K/15815K, paused 1ms+1ms
11-09 15:42:37.722: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 8ms
11-09 15:42:37.752: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 32% free 10758K/15815K, paused 2ms+1ms
11-09 15:42:37.762: D/dalvikvm(3806): GC_FOR_ALLOC freed 1559K, 33% free 10629K/15815K, paused 11ms
11-09 15:42:37.792: D/dalvikvm(3806): GC_CONCURRENT freed 1367K, 33% free 10653K/15815K, paused 2ms+1ms
11-09 15:42:37.812: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 10ms
11-09 15:42:37.832: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:37.852: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 10ms
11-09 15:42:37.872: D/dalvikvm(3806): GC_CONCURRENT freed 8K, 33% free 10644K/15815K, paused 1ms+1ms
11-09 15:42:37.882: D/dalvikvm(3806): GC_FOR_ALLOC freed 1406K, 33% free 10629K/15815K, paused 9ms
11-09 15:42:37.912: D/dalvikvm(3806): GC_CONCURRENT freed 1245K, 33% free 10652K/15815K, paused 1ms+0ms
11-09 15:42:37.932: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 8ms
11-09 15:42:37.962: D/dalvikvm(3806): GC_CONCURRENT freed 1366K, 33% free 10661K/15815K, paused 0ms+1ms
11-09 15:42:37.972: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 10ms
11-09 15:42:38.002: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 32% free 10758K/15815K, paused 1ms+1ms
11-09 15:42:38.012: D/dalvikvm(3806): GC_FOR_ALLOC freed 1559K, 33% free 10629K/15815K, paused 12ms
11-09 15:42:38.042: D/dalvikvm(3806): GC_CONCURRENT freed 1367K, 33% free 10653K/15815K, paused 1ms+1ms
11-09 15:42:38.062: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 8ms
11-09 15:42:38.092: D/dalvikvm(3806): GC_CONCURRENT freed 1366K, 33% free 10661K/15815K, paused 1ms+1ms
11-09 15:42:38.112: D/dalvikvm(3806): GC_CONCURRENT freed 1293K, 33% free 10652K/15815K, paused 1ms+0ms
11-09 15:42:38.132: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 13ms
11-09 15:42:38.162: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 2ms+1ms
11-09 15:42:38.182: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 11ms
11-09 15:42:38.202: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+0ms
11-09 15:42:38.222: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 8ms
11-09 15:42:38.242: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:38.262: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 7ms
11-09 15:42:38.272: D/dalvikvm(3806): GC_CONCURRENT freed 8K, 33% free 10644K/15815K, paused 1ms+1ms
11-09 15:42:38.292: D/dalvikvm(3806): GC_FOR_ALLOC freed 1406K, 33% free 10629K/15815K, paused 8ms
11-09 15:42:38.322: D/dalvikvm(3806): GC_CONCURRENT freed 1366K, 33% free 10653K/15815K, paused 1ms+1ms
11-09 15:42:38.342: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 7ms
11-09 15:42:38.362: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:38.382: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 8ms
11-09 15:42:38.402: D/dalvikvm(3806): GC_CONCURRENT freed 8K, 33% free 10644K/15815K, paused 1ms+0ms
11-09 15:42:38.412: D/dalvikvm(3806): GC_FOR_ALLOC freed 1406K, 33% free 10629K/15815K, paused 9ms
11-09 15:42:38.442: D/dalvikvm(3806): GC_CONCURRENT freed 1245K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:38.462: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 9ms
11-09 15:42:38.492: D/dalvikvm(3806): GC_CONCURRENT freed 8K, 25% free 12019K/15815K, paused 1ms+2ms
11-09 15:42:38.522: D/dalvikvm(3806): GC_CONCURRENT freed 3103K, 32% free 10769K/15815K, paused 1ms+2ms
11-09 15:42:38.532: D/dalvikvm(3806): GC_FOR_ALLOC freed 1570K, 33% free 10637K/15815K, paused 9ms
11-09 15:42:38.552: D/dalvikvm(3806): GC_CONCURRENT freed 1270K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:38.572: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 7ms
11-09 15:42:38.592: D/dalvikvm(3806): GC_CONCURRENT freed 1366K, 33% free 10661K/15815K, paused 1ms+1ms
11-09 15:42:38.622: D/dalvikvm(3806): GC_CONCURRENT freed 1293K, 32% free 10758K/15815K, paused 1ms+1ms
11-09 15:42:38.632: D/dalvikvm(3806): GC_FOR_ALLOC freed 1559K, 33% free 10629K/15815K, paused 11ms
11-09 15:42:38.662: D/dalvikvm(3806): GC_CONCURRENT freed 1246K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:38.682: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 13ms
11-09 15:42:38.702: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:38.722: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 11ms
11-09 15:42:38.742: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:38.762: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 10ms
11-09 15:42:38.782: D/dalvikvm(3806): GC_CONCURRENT freed 8K, 33% free 10644K/15815K, paused 1ms+1ms
11-09 15:42:38.802: D/dalvikvm(3806): GC_FOR_ALLOC freed 1406K, 33% free 10629K/15815K, paused 10ms
11-09 15:42:38.822: D/dalvikvm(3806): GC_CONCURRENT freed 1245K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:38.842: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 9ms
11-09 15:42:38.872: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 24% free 12027K/15815K, paused 1ms+1ms
11-09 15:42:38.902: D/dalvikvm(3806): GC_CONCURRENT freed 3208K, 33% free 10672K/15815K, paused 1ms+0ms
11-09 15:42:38.912: D/dalvikvm(3806): GC_FOR_ALLOC freed 1450K, 33% free 10629K/15815K, paused 9ms
11-09 15:42:38.942: D/dalvikvm(3806): GC_CONCURRENT freed 1366K, 33% free 10653K/15815K, paused 1ms+1ms
11-09 15:42:38.962: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 7ms
11-09 15:42:38.982: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:39.002: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 10ms
11-09 15:42:39.022: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:39.042: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 8ms
11-09 15:42:39.072: D/dalvikvm(3806): GC_CONCURRENT freed 1366K, 33% free 10661K/15815K, paused 1ms+1ms
11-09 15:42:39.092: D/dalvikvm(3806): GC_CONCURRENT freed 1390K, 33% free 10661K/15815K, paused 1ms+1ms
11-09 15:42:39.112: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 9ms
11-09 15:42:39.132: D/dalvikvm(3806): GC_CONCURRENT freed 1366K, 33% free 10661K/15815K, paused 1ms+1ms
11-09 15:42:39.152: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 8ms
11-09 15:42:39.172: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+0ms
11-09 15:42:39.192: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 11ms
11-09 15:42:39.212: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:39.232: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 9ms
11-09 15:42:39.252: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:39.272: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 10ms
11-09 15:42:39.302: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 2ms+1ms
11-09 15:42:39.322: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 11ms
11-09 15:42:39.342: D/dalvikvm(3806): GC_CONCURRENT freed 1366K, 33% free 10661K/15815K, paused 2ms+1ms
11-09 15:42:39.362: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 11ms
11-09 15:42:39.382: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:39.402: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 11ms
11-09 15:42:39.432: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 32% free 10758K/15815K, paused 1ms+1ms
11-09 15:42:39.442: D/dalvikvm(3806): GC_FOR_ALLOC freed 1559K, 33% free 10629K/15815K, paused 11ms
11-09 15:42:39.472: D/dalvikvm(3806): GC_CONCURRENT freed 1246K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:39.492: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 10ms
11-09 15:42:39.522: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:39.532: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 8ms
11-09 15:42:39.562: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 2ms+1ms
11-09 15:42:39.572: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 9ms
11-09 15:42:39.602: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:39.612: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 12ms
11-09 15:42:39.642: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:39.652: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 9ms
11-09 15:42:39.682: D/dalvikvm(3806): GC_CONCURRENT freed 1269K, 32% free 10758K/15815K, paused 1ms+2ms
11-09 15:42:39.692: D/dalvikvm(3806): GC_FOR_ALLOC freed 1559K, 33% free 10629K/15815K, paused 10ms
11-09 15:42:39.722: D/dalvikvm(3806): GC_CONCURRENT freed 1246K, 33% free 10652K/15815K, paused 1ms+1ms
11-09 15:42:39.742: D/dalvikvm(3806): GC_FOR_ALLOC freed 1414K, 33% free 10637K/15815K, paused 12ms
11-09 15:42:39.762: D/dalvikvm(3806): GC_CONCURRENT freed 1366K, 33% free 10661K/15815K, paused 1ms+1ms
的更新: 的 从循环内部删除所有“新”元素之后,我使用了DDMS分配跟踪器,发现我在input = in.readLine()中有很多分配;在InputStream对象里面有一些wrap()函数......怎么会这样?以及该怎么做
答案 0 :(得分:5)
这不应该是一个很大的问题 - 毕竟,这不是一个错误,而是GC在完成它的工作。
但是,在这种情况下,每秒都会释放大量内存(并且由于堆的状态保持不变(Allocated - Free),这也意味着每秒都会分配这个内存。这将加剧电池消费并会导致你的应用程序滞后。
与其他建议一样,尝试实例化while
之外的对象,而不是在每个循环中创建和删除它们。例如,将循环更改为:
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
while (app.isConnectedToServer())
{
app.getFacebookManager().refreshAccessToken();
String input;
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));
}
}
答案 1 :(得分:0)
当您拥有已创建且不再使用的对象时,垃圾收集器将激活。
这意味着,new
上的任何run()
关键字(包括在运行中调用的也有此关键字的方法)将在以后进行垃圾收集。
有些东西必须一直重新创建(例如,Streams),但有些东西可能会保留在内存中(例如,套接字,如果它的生命周期得到妥善处理)。你可以挖掘startKeepAliveTimer();
; serverManager.processData(app.convertToJSONObject(input));
和其他方法,以检查他们是否每次都不会创建可以在服务期限内保留的内容。
但尽管如此......在我看来,GC正在做的工作很好,并且它总是收集并且内存使用相当稳定,我不会太担心。
答案 2 :(得分:0)
我同意Budius的看法。 GC留给了android os。如果您在应用程序中遇到过多的内存消耗,请尝试修改它。使用MAT分析器查找内存泄漏。 该视频是关于内存管理的。 http://www.youtube.com/watch?v=_CruQY55HOk。 。因此,如果你看视频,你应该能够摆脱内存泄漏。
此外,如果您有长时间运行的操作,请确保在线程上运行它们。我建议你使用asynctask。在UI线程上运行长时间运行操作也会导致内存泄漏。