不可恢复的错误。 mremap():失败:io_realm_internal_SharedGroup.cpp 第 188 行内存不足

Unrecoverable error. mremap(): failed: Out of memory in io_realm_internal_SharedGroup.cpp line 188

我已经将 Realm 实现为我的数据库,并且在我的应用程序中,我 运行 为 phone 联系人提供了一项服务,用于检查我的 phone 书中的任何更改。 该服务适用于大约 3 到 4 个更改,然后应用程序因我的 logcat.

中的此错误而崩溃

不可恢复的错误。 mremap():失败:io_realm_internal_SharedGroup.cpp 第 188 行

内存不足

这是我的代码:

 public static void refreshingContactsDB()
{


        createCountryDetailsArrayModel();

        TelephonyManager tm = (TelephonyManager) ApplicationController.getInstance()
                .getSystemService(Context.TELEPHONY_SERVICE);
        String simCountryISO = tm.getSimCountryIso();

        for (int i = 0; i < countryDetailsList.size(); i++) {
            if (simCountryISO.equalsIgnoreCase(countryDetailsList.get(i)
                    .getCode())) {

                dialCodePrefix = countryDetailsList.get(i).getDial_code();

            }
        }


        AppPreferenceManager.getInstance().setContactUpdate(false);

        Logger.debug("Contact Update Refreshing Contact Started -->");

        Thread background = new Thread(new Runnable() {
            public void run() {

                Realm realmRefresh = Realm.getInstance(ApplicationController.getInstance());
                ContentResolver cr = ApplicationController.getInstance()
                        .getContentResolver();

                Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, null);

                String duplicateName = "";
                String duplicatePhone = "";

                if (phones.getCount() > 0) {

                    final int nameIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                    final int numberIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

                    while (phones.moveToNext()) {
                        String name = phones.getString(nameIndex);
                        String phoneNo = phones.getString(numberIndex);

                        if (phoneNo != null && !phoneNo.isEmpty()) {

                            phoneNo = phoneNo.replace(" ", "");
                            phoneNo = phoneNo.replace("(", "");
                            phoneNo = phoneNo.replace(")", "");
                            phoneNo = phoneNo.replace("-", "");
                            phoneNo = phoneNo.replace("\u00a0", "");
                            phoneNo = phoneNo.replace("\u202c", "");
                            phoneNo = phoneNo.replace("\u2011", "");
                            phoneNo = phoneNo.replace("\u202a", "");
                            phoneNo = phoneNo.replace("*", "");
                            phoneNo = phoneNo.replace("#", "");

                            if (phoneNo.length() >= 5) {

                                if (name.equalsIgnoreCase(duplicateName) && phoneNo.equalsIgnoreCase(duplicatePhone)) {
                                    continue;
                                }

                                duplicateName = name;
                                duplicatePhone = phoneNo;

                                String formattedPhoneNumber;
                                formattedPhoneNumber = parseNumber(phoneNo);

                                //                                List<LocalContactDb> getContactsList = Select
                                //                                        .from(LocalContactDb.class)
                                //                                        .where(Condition.prop("PHONE").eq(
                                //                                                formattedPhoneNumber))
                                //                                        .list();
                                RealmResults<R_LocalContactDB> realmResults = realmRefresh.where(R_LocalContactDB.class).equalTo("phone", formattedPhoneNumber).findAll();
                                Logger.debug("Size: " + realmResults.size());
                                RealmResults<R_LocalContactDB> query = realmRefresh.where(R_LocalContactDB.class).findAll();
                                R_LocalContactDB rContacts = new R_LocalContactDB(null, null, false, 0);


                                if (realmResults.size() == 0) {
                                    realmRefresh.beginTransaction();
                                    R_LocalContactDB rCont = realmRefresh.copyToRealm(rContacts);
                                    rCont.setName(name);
                                    rCont.setPhone(formattedPhoneNumber);
                                    rCont.setStatus(0);
                                    rCont.setMatchedWithRecent(true);
                                    Logger.debug("New Size: " + query.size());
                                    realmRefresh.commitTransaction();

                                    Logger.debug("Contact Update " + name
                                            + " saved");
                                } else {
                                    realmRefresh.beginTransaction();
                                    if (!name.equalsIgnoreCase(realmResults.get(0).getName())) {
                                        realmResults.get(0).setName(name);
                                    }

                                    realmResults.get(0).setMatchedWithRecent(true);
                                    // realmResults.get(0);
                                    Logger.debug("New Size Else Condition: " + query.size());
                                    realmRefresh.commitTransaction();
                                    realmRefresh.close();


                                }

                            }


                        }

                    }
                    ContactsSyncService.contactUpdated= false ;

                    phones.close();


                }

                deleteExtraContacts();

                getNumbersFromDBAndUpdate();

            }

        });
        background.start();
    }

我已阅读此内容,但似乎无法从文档中完全理解它:

请注意,写入会相互阻塞,如果正在进行其他写入,则会阻塞它们所在的线程。如果您从 UI 线程进行写入,同时还从后台线程进行写入,这可能会导致 ANR 错误。为了避免这种情况,首先在事务外先在内存中创建对象,并且只在事务内执行简单的 Realm.copyToRealm() ,这将使阻塞时间保持在最低限度。

您很可能 运行 内存不足,因为您在 parseContactstoContactsDB 线程中调用 Realm.getInstance() 而没有再次关闭它。

因为每次调用该方法时都会创建一个新线程,所以每次都会有效地打开一个全新的领域,这会占用大量内存。

在退出线程之前调用 realm.close() 应该会再次释放这些资源并防止您 运行 内存不足。