The heap is the area in memory in which objects are created.
// 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();
本文介绍了Java中堆内存的管理方式,包括获取当前堆大小、最大堆容量、以及空闲内存等关键信息。通过这些信息可以帮助开发者更好地理解Java内存管理机制,并有效避免内存溢出等问题。
810

被折叠的 条评论
为什么被折叠?



