Realm realm = Realm.getInstance(getRealmConfiguration());
lineList = response.getDatasList();
for (int i = 0; i < lineList.size(); i++) {
realm.beginTransaction();
Stock stock = realm.createObject(Stock.class);
stock.setCode(lineList.get(i).getCode());
stock.setName(lineList.get(i).getName());
Timber.d(i + " {} code " + lineList.get(i).getCode() + "|| name " + lineList.get(i)
.getName());
realm.commitTransaction();
}
realm.close();

Hi @alvin87
You shouldn't run out memory on such a small number of items. Are you app doing something else?
Note that it will be more efficient to move the transaction outside the loop as well.
realm.beginTransaction();
for (int i = 0; i < lineList.size(); i++) {
Stock stock = realm.createObject(Stock.class);
stock.setCode(lineList.get(i).getCode());
stock.setName(lineList.get(i).getName());
Timber.d(i + " {} code " + lineList.get(i).getCode() + "|| name " + lineList.get(i)
.getName());
}
realm.commitTransaction();
should fix your problem. Although I would recommend using realm.executeTransaction() instead so that you rollback failed transactions.
@alvin87 It looks like our storage engine is trying to allocate 32 MB (335544320-301989888)/(1024*1024). It's a big chuck. Can you say a bit about the Stock class?
This allocation is large because the storage engine will grow the database in chunks. The actual storage needed for this particular transaction may be much smaller. The allocation likely fails because the device a) is out of virtual address space or b) the address space has become too fragmented, so that a ~330MB block cannot be found. At the point of failure, the database is already ~300MB. I assume this is a 32-bit device. Do the app use lots of virtual memory for other purposes besides Realm? Possibly through 3rd party libraries?
We have a pending Core issue on lowering the use of virtual memory during commit: https://github.com/realm/realm-core/issues/1478
@finnschiermer Any estimate eta on the core issue? We have experienced this painful issue in the past whenever our realm db reached about 250mb range. Since then, we actively avoid storing medium sized to large string data into realm and our oom errors went away.
@diegomontoya We'll probably start testing a solution this week.

I got this.
@swjjxyxty Honestly, this message tends to occur more and more often when people open Realms on non-looper background threads that they don't close.
The error can easily be in your own code. If you open a Realm on a background thread using getInstance() or getDefaultInstance(), you HAVE to call close() on it at the end of execution on said thread.
@swjjxyxty As @Zhuinden points out, closing Realms is important: https://realm.io/docs/java/latest/#closing-realm-instances
@kneth @Zhuinden thanks,the problem has been resolved.
I checked all the Realm.getDefaultInstance() ,find a line not close realm.
hopeing to provide a method for obtaining the reference count,or give a warning.
Because this error is difficult to exclude.
@swjjxyxty It's great to hear! Yes, finding a missing close() can be difficult.
@swjjxyxty I highly recommend making sure close() is in a finally block so that you don't end up with _this_ error only when you get an obscure error at some point.
Most helpful comment
@swjjxyxty Honestly, this message tends to occur more and more often when people open Realms on non-looper background threads that they don't close.
The error can easily be in your own code. If you open a Realm on a background thread using
getInstance()orgetDefaultInstance(), you HAVE to callclose()on it at the end of execution on said thread.