获取对象占用内存

  1. 添加依赖

    
    	org.apache.lucene
    	lucene-core
    	4.0.0
    
    
  2. 添加vm启动参数

    --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.nio=ALL-UNNAMED --add-opens java.base/sun.nio.ch=ALL-UNNAMED
    
  3. 编写测试代码

    public static void main(String[] args) throws Exception {
    	List list = new ArrayList<>() ;
    	for (int i = 0; i < 1000000; i++) {
    		list.add(new ChannelUserDTO()) ;
    	}
    	log.info("size 2: {}", RamUsageEstimator.humanSizeOf(list));
    }
    
    
    
    
    
    
    
    
    
    
    
    
    

你可能感兴趣的:(java)