find availabe internal memory in runtime from code
i must find the available internal memory in runtime from code because when i lower internal phone memory to less than 100K, sqlite operations throw SQLiteDiskIOException on each db operation, due to insufficient disk space
开发者_如何学编程any ideas?
clahav
In such a case, using an embedded DB seems overkill, as you have a low amount of RAM and, as a consequence, a weak environment. I would instead prefer prevalent layer like
- Space4J
- Prevayler
You'll loose some features over JDBC (transaction, SQL), but your memory consumption can be really lowered.
Calling Runtime.getRuntime().freeMemory()
gives you an estimate of the amount of free memory. However, it is not a particularly useful measure, since there is a good chance that you can allocate more memory than the reported value. When you try to allocate more memory than is currently free, the JVM will automatically run the GC in an attempt to reclaim enough space for your allocation, and this will usually succeed.
You can request the GC to run "right now" by calling System.gc()
, but this is generally a bad idea from a performance perspective. The GC is most efficient (in terms of time spent per byte reclaimed) if you just let the JVM run the GC when it needs to. The only case where it can be worthwhile to run the GC is if you know that you have CPU cycles to spare at a particular point and you want to mitigate GC pauses.
In your particular case, I'm not sure what advantage there is in knowing how much free memory there is. Why don't you just try the sqlite operations and catch / deal with the exceptions? Or increase the heap size? Or track down what is leaking memory or using it inefficiently?
Runtime rt=Runtime.getRuntime();
rt.freeMemory()
calling System.gc() may increasing the available free memory
精彩评论