Runtime - why is freeMemory() not showing memory consumed correctly?

后端 未结 4 1007
天涯浪人
天涯浪人 2021-01-03 04:58

Below is the code snippet to examine the memory

public class TestFreeMemory {

    public static void main(String ... args){

        Runtime rt = Runtime.g         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-03 05:33

    It's a good question since you are expecting an INCREASE in memory usage to be reflected it seems a logical test. You can assume this behaviour is because the Heap management and garbage collection system is somewhat more complex that a simple Free vs Allocated boundary. Allocation is probably done in chunks much bigger than your one String + one Integer and so free memory is likely to be counted by summing up free chunks.

提交回复
热议问题