测试JAVA对象占用的内存方法

依赖JAR包:SizeOf.jar

http://sourceforge.net/projects/sizeof

java.SizeOf is a simple java agent what can be used to calculate the memory size
of java objects.

VM 运行参数 :-javaagent:G:\SizeOf.jar

start the jvm with the argument: -javaagent:<path to>/SizeOf.jar

public class HeapSize {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HashMap<Object, Object> map = new HashMap<Object, Object>();
//		SizeOf.turnOnDebug();
		String a = "a";
		System.out.println("a:" + SizeOf.humanReadable(SizeOf.deepSizeOf(a)));
		String size = SizeOf.humanReadable(SizeOf.deepSizeOf(map));
		System.out.println("map size: " + size);
		ArrayList<Object> list = new ArrayList<Object>();
		size = SizeOf.humanReadable(SizeOf.deepSizeOf(list));
		System.out.println("list size: " + size);
		HashSet<Object> set = new HashSet<Object>();
		size = SizeOf.humanReadable(SizeOf.deepSizeOf(set));
		System.out.println("set size: " + size);
	}

}

输出:

JAVAGENT: call premain instrumentation for class SizeOf
a:88.0b
map size: 128.0b
list size: 80.0b
set size: 160.0b


转载于:https://my.oschina.net/frankwu/blog/152428

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值