java中计算对象内存大小

 1.使用java.SizeOf

2.http://sizeof.sourceforge.net/

Introduction

With java.SizeOf you can measure the real memory size of your Java objects. Download it here


The project is a little java agent what use the package java.lang.Instrument introduced in Java 5 and is released under GPL license.


java.sizeOf is in early stage of development but it's quite usable and it was very useful for us to know the memory size of our HttpSession's objects. The best use of the library is inside an aspect to avoid dependencies in your code.

Quickstart

1. include sizeOf.jar in the classpath of your application and use it in your code like this:
 import net.sourceforge.sizeof
 ...
 SizeOf.skipStaticField(true); //java.sizeOf will not compute static fields
 SizeOf.skipFinalField(true); //java.sizeOf will not compute final fields
 SizeOf.skipFlyweightObject(true); //java.sizeOf will not compute well-known flyweight objects
 System.out.println(SizeOf.deepSizeOf(<your object>)); //this will print the object size in bytes
You can dump object's size setting the min size to log (if you don't specify an output stream standard out is used):
 SizeOf.setMinSizeToLog(1024); //min object size to log in bytes
 SizeOf.setLogOutputStream(new FileOutputStream("<your log file>"));
Use the humanReadable() method to get the object size in byte, kilo or mega (if you need giga your in trouble guy!):
 SizeOf.humanReadable(SizeOf.deepSizeOf(<your object>));

2. start your application with the following JVM parameter:

 -javaagent:/path_to/sizeOf.jar

3. have fun!


Please use SourceForge forum for reporting any bug, comment or suggestion.

3.在项目中引入SizeOf.jar

4.在VM arguments中加入-javaagent:"D:\java_lib\SizeOf.jar"

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sjiang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值