gpt4 book ai didi

安卓 Realm : Out of memory

转载 作者:行者123 更新时间:2023-11-29 00:11:54 25 4
gpt4 key购买 nike

使用迭代次数超过3000次的循环:

for (Element e2 : elinks2) {
AllAuthors allauthor = new AllAuthors();
allauthor.setUrl_base(href.substring(0, href.length() - 1));
allauthor.setAuthor_fio(e2.text().trim());
allauthor.setLetters(e.attr("href"));

Realm realm_2 = null;
try {
realm_2 = Realm.getInstance(new File(func.getFolder("db")), getResources().getString(R.string.app_name_db) + ".realm");
realm_2.beginTransaction();
realm_2.copyToRealmOrUpdate(allauthor);
realm_2.commitTransaction();
} finally {
if(realm_2 != null) {
realm_2.close();
}
}
}

添加(或更新)大约 1000 条记录后出现错误

Out of memory in io_realm_internal_SharedGroup.cpp line 164

在线发誓

realm_2.commitTransaction();

什么建议?

AllAuthors.class:

@RealmClass
public class AllAuthors extends RealmObject {

@PrimaryKey
private String url_base;

private String author_fio;

private String letters;

....Standard getters & setters generated....
}

最佳答案

您不断打开和关闭一个 Realm,同时进行大量小额交易。虽然这种模式效率很低,但它本身不应该导致内存不足。但是,如果您在后台线程中运行它,对于每个事务 Realm 都必须维护与原始数据的差异,直到所有线程上的 Realms 都可以更新(这发生在 Looper 事件或调用 Realm.refresh() ).我建议重构为以下内容,这样既更快又占用内存更少:

Realm realm_2 = Realm.getInstance(new File(func.getFolder("db")), getResources().getString(R.string.app_name_db) + ".realm");
realm_2.beginTransaction();
for (Element e2 : elinks2) {
AllAuthors allauthor = new AllAuthors();
allauthor.setUrl_base(href.substring(0, href.length() - 1));
allauthor.setAuthor_fio(e2.text().trim());
allauthor.setLetters(e.attr("href"));
realm_2.copyToRealmOrUpdate(allauthor);
}
realm_2.commitTransaction();
realm_2.close();

关于安卓 Realm : Out of memory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29641874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com