当前位置: 首页 > news >正文

java对象占用内存大小计算方式

案例一:

User

public class User {
	
	
}

UserSizeTest

public class UserSizeTest {
	static final Runtime runTime=Runtime.getRuntime();
	public static void main(String[] args) {
		final int count = 100000;
		User[] us=new User[count];
		
		long heap1 = 0;
		
		for (int i = -1; i < count; ++i) {
			User user=null ;
			user=new User();
			if (i >= 0)
				us[i] = user;
			else {
				user = null; 
				heap1 = getUsedMemory(); 
			}
		}
		long heap2 = getUsedMemory();
		System.out.println("user大小:"+((float)heap2-heap1)/count+" bytes");
		for (int i = 0; i < count; i++) {
			us[i]=null;
		}
		runTime.gc();
	}
	static long getUsedMemory(){
		return runTime.totalMemory()-runTime.freeMemory();
	}
}

结果:

user大小:7.62576 bytes


说明:

空对象占用8个字节




案例二:

public class User {
	boolean flag;
	long id;
	Date date ;
	
}

执行測试类结果;

user大小:23.50192 bytes

说明:

boolean 1+ long 8 + 引用类型 Date 4 =13 凑齐8的倍数 =16
	
16+空对象 8 =24


转载于:https://www.cnblogs.com/blfshiye/p/4293377.html

相关文章:

  • iOS开发之oc(六)--点语法
  • Sample SecondarySort 浅析
  • 导入项目时Loading descriptor ...
  • 【BZOJ】【2940】【POI2000】条纹
  • IOS开发基础知识--碎片8
  • 远程debug WebSphere 和 Watch时提示error(s)_during_the_evaluation
  • javascirpt怎样模仿块级作用域(js高程笔记)
  • python 多线程编程
  • 一:Html基本结构
  • ETL的考虑
  • sass学习(2)——关于变量
  • C# 语言基础(转义字符)
  • 第一天开通博客园
  • iOS开发之进阶指南 持续更新
  • 服务器安装2个tomcat
  • Angular6错误 Service: No provider for Renderer2
  • express.js的介绍及使用
  • JavaScript 奇技淫巧
  • MySQL的数据类型
  • overflow: hidden IE7无效
  • PAT A1120
  • Vultr 教程目录
  • 对象引论
  • 回流、重绘及其优化
  • 开发基于以太坊智能合约的DApp
  • 设计模式走一遍---观察者模式
  • 再次简单明了总结flex布局,一看就懂...
  • 正则与JS中的正则
  • AI又要和人类“对打”,Deepmind宣布《星战Ⅱ》即将开始 ...
  • ​【C语言】长篇详解,字符系列篇3-----strstr,strtok,strerror字符串函数的使用【图文详解​】
  • (1)(1.13) SiK无线电高级配置(六)
  • (floyd+补集) poj 3275
  • (阿里云万网)-域名注册购买实名流程
  • (动手学习深度学习)第13章 计算机视觉---微调
  • (黑马C++)L06 重载与继承
  • (四)JPA - JQPL 实现增删改查
  • (转)EOS中账户、钱包和密钥的关系
  • (转)重识new
  • .net core开源商城系统源码,支持可视化布局小程序
  • .net framework profiles /.net framework 配置
  • .NET Remoting学习笔记(三)信道
  • .NET 解决重复提交问题
  • .NET(C#、VB)APP开发——Smobiler平台控件介绍:Bluetooth组件
  • /usr/lib/mysql/plugin权限_给数据库增加密码策略遇到的权限问题
  • @EventListener注解使用说明
  • [BZOJ4554][TJOI2016HEOI2016]游戏(匈牙利)
  • [C# WPF] 如何给控件添加边框(Border)?
  • [Cocoa]iOS 开发者账户,联机调试,发布应用事宜
  • [Editor]Unity Editor类常用方法
  • [emacs] CUA的矩形块操作很给力啊
  • [HCTF 2018]WarmUp (代码审计)
  • [hdu 4405] Aeroplane chess [概率DP 期望]
  • [HJ56 完全数计算]
  • [IDF]被改错的密码
  • [JavaWeb]——过滤器filter与拦截器Interceptor的使用、执行过程、区别