在观察到我实施的BluetoothGattCallback覆盖功能" onCharacteristicChanged"正在泄漏记忆我一直试图了解泄漏的来源,但没有成功。我希望有人会建议为什么在这个函数中发生Log.d内存泄漏的原因以及可能导致呼叫外部明显泄漏的原因。
首先,我必须说我有一个Android服务实现BLE设备接口,当我从我支持的设备收到按钮按下事件时,该服务唤醒已关闭的应用程序。当按钮按下很少发生时,onCharacteristicChanged功能也会在设备报告电池电量时被调用,并且这些事件发生在常规的cadance上。内存慢慢泄漏,最终系统在崩溃后重新启动服务。
为了调试这个,我扼杀了我的" onCharacteristicChanged"覆盖实现,只是插入了内存跟踪行为。我注意到由于Log.d调用导致192个字节泄漏(数量与消息大小一致),此外该函数外部还有更多内存泄漏。我在logcat中观看时没有调用我服务中的其他功能。
在没有转储整个服务的情况下,下面是截断的BluetoothGattCallback,它具有onCharacteristicChanged覆盖功能和相关的支持功能,用于获取我正在使用的内存统计信息,以及当服务刚接收特征变化事件时的logcat输出
在阅读了源代码和logcat文本之后,是否有人建议为什么这个函数内部的Log.d内存泄漏是什么?以及什么可能导致调用之外的明显泄漏?任何关于平息内存泄漏可能需要的建议都将不胜感激。
来源
// defined as global to the service
long startingMemUsedSample, endMemUsedSample;
// --------------------------------------------------------------------
// BluetoothGattCallback implementation
// --------------------------------------------------------------------
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
// all other overrides truncated from this listing to keep it simple
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
startingMemUsedSample = getUsedMemorySize();
Log.d("onCharacteristicChanged", (startingMemUsedSample - endMemUsedSample) + " bytes leaked out of cycle");
// the guts of this handler were neutered from this handler for analysis
endMemUsedSample = getUsedMemorySize();
Log.d("onCharacteristicChanged", (endMemUsedSample - startingMemUsedSample) + " bytes leaked this cycle");
}
}
// --------------------------------------------------------------------
// getUsedMemorySize
// --------------------------------------------------------------------
public static long getUsedMemorySize() {
long freeSize = 0L;
long totalSize = 0L;
long usedSize = -1L;
try {
Runtime info = Runtime.getRuntime();
freeSize = info.freeMemory();
totalSize = info.totalMemory();
usedSize = totalSize - freeSize;
}
catch (Exception e) {
e.printStackTrace();
}
return usedSize;
}
服务刚刚接收电池级更新时收到的Logcat调试消息:
01-21 21:25:01.108 27282-27298 / com.acompany.bleservice D / onCharacteristicChanged:448个字节泄漏此循环
01-21 21:25:16.069 27282-27299 / com.acompany.bleservice D / onCharacteristicChanged:4448字节泄漏出循环
01-21 21:25:16.069 27282-27299 / com.acompany.bleservice D / onCharacteristicChanged:此周期泄漏了256个字节
01-21 21:25:31.031 27282-27298 / com.acompany.bleservice D / onCharacteristicChanged:4416个字节泄漏出循环
01-21 21:25:31.031 27282-27298 / com.acompany.bleservice D / onCharacteristicChanged:192个字节泄漏了这个周期
01-21 21:25:45.994 27282-27299 / com.acompany.bleservice D / onCharacteristicChanged:4352字节漏出周期
01-21 21:25:45.994 27282-27299 / com.acompany.bleservice D / onCharacteristicChanged:192个字节泄漏了这个周期
01-21 21:26:00.956 27282-27298 / com.acompany.bleservice D / onCharacteristicChanged:4224字节泄漏出循环
01-21 21:26:00.956 27282-27298 / com.acompany.bleservice D / onCharacteristicChanged:192个字节泄漏此循环
01-21 21:26:15.918 27282-27299 / com.acompany.bleservice D / onCharacteristicChanged:4352字节泄漏出循环
01-21 21:26:15.919 27282-27299 / com.acompany.bleservice D / onCharacteristicChanged:192个字节泄漏了这个周期
01-21 21:26:30.891 27282-27298 / com.acompany.bleservice D / onCharacteristicChanged:4352字节泄漏出循环
01-21 21:26:30.891 27282-27298 / com.acompany.bleservice D / onCharacteristicChanged:192个字节泄漏了这个周期
01-21 21:26:45.844 27282-27299 / com.acompany.bleservice D / onCharacteristicChanged:4352字节泄漏出循环
01-21 21:26:45.844 27282-27299 / com.acompany.bleservice D / onCharacteristicChanged:192个字节泄漏了这个周期
01-21 21:27:00.806 27282-27298 / com.acompany.bleservice D / onCharacteristicChanged:4352字节漏出周期
01-21 21:27:00.806 27282-27298 / com.acompany.bleservice D / onCharacteristicChanged:192个字节泄漏了这个周期