The error of 'Cannot allocate memory' can be caused by using a lot of resources while you don't allocate spaces for them in advance, the resources include locks, mutexes, etc.
1) The memory will be allocated from heap if you haven't set any alloc functions.
2) All the databases in an environment share the same cache(16m here).
3) As mentioned, the error can be from failing to allocating lock, mutex, and other resources. You need to get the exact location of where the error happens.
4) I think you have asked the question about the DBT flag 0. The memory will be allocated from heap by default, and be related to the cursor. It is transient, which means that it can be overwritten by next DBC->get, and freed until the cursor closes. More information can be found from here:
From the simple description about the program, the error seems to be caused by not setting enough locks and lock objects. Generally Berkeley DB uses page lock, and if you enable lock subsystem, by default every page will be locked until the cursor closes. If your database has many pages, then many locks and lock objects are required, so you need to set the number before opening environment, since the default number(1000) may be not enough in this case.
To avoid setting such a big number of locks and lock objects, you can try the DB->set_lk_exclusive, since it locks on the database instead of pages.
Another way is to re-structure your program to divide so much operations into many cursors/transactions, and start the next curor/transaction after previous finishes. And if you are just using the cursors to do so many read operations, you can also evaluate using an isolation level like DB_READ_COMMITTED instead of the default level, and this can avoid dividing.
This thread may help you on this topic:
5 The data from DBC->get with flag 0 has no business with the cache. Cache is thread-safe to provide read/write atomicity. If you care about your program's consistence in multi-threads environment, you need to turn on lock and transaction subsystems.
Winter, Oracle Berkeley DB
Thank you Winter, after I set lock/lock object to 50k, issues were solved.