首页 > 解决方案 > 领域是否会在 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);

标签: androidrealm

解决方案


推荐阅读