Memory usage for a specific list of object

后端 未结 4 1903
别跟我提以往
别跟我提以往 2021-01-03 05:28

i have a list of simples pojos (a User class) with about 15 simple fields & 1 arrayList. Those represent users & maybe 100 or 1000 of them will be store in memory in

4条回答
  •  时光取名叫无心
    2021-01-03 05:52

    If you want a simple test, you can set the new size to be large and do the following. This only works if your new size is much larger than the data you are creating. e.g.

    -XX:NewSize=1g -verbosegc

    The value will be correct provided you don't see any GC.

    long before = Runtime.getRuntime().freeMemory();
    
    //build object here
    
    long used = before - Runtime.getRuntime().freeMemory();
    

    Note: this assumes you don't generate an temporary objects.

提交回复
热议问题