android - 领域是否会在 android 中创建内存泄漏
问题描述
我将 Realm 用于本地数据库,并在 Timer 中使用,每 40 秒检查一次数据是否存在我设置了同步变量,在此过程之后我关闭了 realm realm.close,我想知道,下面的代码是否存在内存泄漏,因为我创建了 Realm每 40 秒对象一次,我最终将其关闭,但我对此表示怀疑?是否会导致内存泄漏
//Timer starts for sending LogTable data
Log.i("Timer Start","Start Timer");
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
jsonArray = new JsonArray();
jsonObject = new JsonObject();
//Realm.init(context);
Realm realm = Realm.getDefaultInstance();
try {
realm.executeTransaction(realm1 -> {
//getting all data from realm DB
RealmResults<LogTable> realmResults = realm.where(LogTable.class).equalTo("sync", (String) null).findAll();
Log.i("RealmResults Login Table:", "" + realmResults.size());
for (int i = 0; i < realmResults.size(); i++) {
jsonObject.addProperty("dev_userid", realmResults.get(i).getDev_userid());
jsonObject.addProperty("date", realmResults.get(i).getDate());
jsonObject.addProperty("logtype", realmResults.get(i).getLogtype());
jsonObject.addProperty("logtext", realmResults.get(i).getLogtext());
jsonObject.addProperty("logErrorCode", realmResults.get(i).getLogErrorCode());
jsonArray.add(jsonObject);
if (i != 0) {
mSocket.emit("signalSendDeviceLogRtc", jsonArray.toString(), (Ack) args -> {
Log.i("callback of LogRtc", "" + args);
Object result = args[0];
if (result.equals(true)) {
Log.i("reult of LogRtc", "" + result);
Realm realm2 = Realm.getDefaultInstance();
try {
RealmResults<LogTable> realmResults2 = realm2.where(LogTable.class).equalTo("sync", (String) null).findAll();
realm2.executeTransaction(realm21 -> {
int count = 0;
for (LogTable logTable : realmResults2){
logTable.setSync("1");
count++;
if (count == 10)
break;
}
});
}catch (Exception e){
e.printStackTrace();
}finally {
realm2.close();
}
}
});
jsonArray = new JsonArray();
jsonObject = new JsonObject();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}finally {
realm.close();
}
Log.i("Timer","Works");
}
};
timer.scheduleAtFixedRate(timerTask,1000,40000);
解决方案
推荐阅读
- c - 无法使用共享 C 库中的函数 cpuset_create
- google-bigquery - 在 bigquery 中拆分字符串
- python - 如果满足条件,则迭代数据帧的行并检索列的值
- wordpress - 如何访问 azure 上的 Wordpress 文件和我的 wp-admin 显示 403 禁止错误
- android - 有什么方法可以拦截 android 上 WebView 上的 websocket 连接?
- javascript - 在选项卡更改时触发窗口重新加载以更新 React 中的选项卡内容
- elasticsearch - 弹性嵌套布尔查询的输出之间的差异
- r - 在 geom_point 中填充区域
- pentaho - Pentaho91:将数据网格行复制到两个合并连接。这是一个错误吗?
- css - 居中可折叠但内容下降