如何在j2me中通过手机获得最大可用堆大小?

时间:2011-06-21 14:37:12

标签: java java-me mobile midlet heap-memory

由于堆大小限制,我的Midlet需要以不同的方式处理不同的堆大小。例如,它应该加载更多或更少的位图字体。

1 个答案:

答案 0 :(得分:1)

// Get current size of heap in bytes
long heapSize = Runtime.getRuntime().totalMemory();

// Get maximum size of heap in bytes. The heap cannot grow beyond this size.
// Any attempt will result in an OutOfMemoryException.
long heapMaxSize = Runtime.getRuntime().maxMemory();

// Get amount of free memory within the heap in bytes. This size will increase
// after garbage collection and decrease as new objects are created.
long heapFreeSize = Runtime.getRuntime().freeMemory();

另请参阅此Runtime Java Documentation.