您的位置:首页 > 编程语言 > C语言/C++

转载 Java中类C/C++的sizeof()操作,知晓实例大小

2012-01-16 04:42 393 查看
======================================================

注:本文源代码点此下载

======================================================

在c/c++中有sizeof()操作,可轻易获知某个类型或实例占用内存大小,sizeof(int) 或者 sizeof(new testclass)。可是java中可没有这么直观的方法可用。
因本人看过不少人写代码总爱写成
list userlist = new arraylist();//注:声明时即初始化一个空 arraylist
userlist = userdao.getallusers();//注:方法getallusers()中会生成一个arraylist的
上面就造成平白多了一个空的 arraylist(),创建完后即刻就推向gc处置,我就在想这样一个空的 arraylist 会占用多少内存,于是找来了 optimizeit 观察后发现一个空的 arraylist 要占去 24b 内存。那 java 中有没有更便的捷的方法呢,于是在网上 google "java sizeof",引出不少话题。
但我觉得比较好的一段代码是
java.sizeof(http://sourceforge.net/projects/sizeof/),需要jdk1.5以上的版本支持,由它测定的空 arraylist 所占内存确也是 24b。下载到 sizeof_0_2_src.zip(其中含sizeof.jar,其实就一个类 sizeof),假设解压到 f:\component
library\java\javasizeof。
用法很简单,直接看它的 readme.txt 文件就行,因为只有一个类,方法也不多,全是静态方法,可以使用静态引入,看看就明白,只是现在的 0.2 版本推荐的方法是 sizeof() 和 deepsizeof(),而不再是 iterativesizeof() 的。
它还提供了一个测试代码 testsizeof(可从中学习 sizeof 的用法),在命令行下,进入到 f:\component library\java\javasizeof目录,然后执行
f:\component library\java\javasizeof>java -javaagent:sizeof.jar net.sourceforge.sizeof.test.testsizeof
显示结果是:
javagent: call premain instrumentation for class sizeof
starting test...
simple obj:40.0b
int:0.0b
long:0.0b
char:0.0b
double:16.0b
boolean:0.0b
integer:0.0b
empty string:0.0b
not empty string:0.0b
not empty string:0.0b
simple obj:24.0b
simple obj:40.0b
empty list:24.0b
10 list:24.0b
20 list no static:24.0b
1000 o arr:816.0b
应该会惊讶一下,为什么会出现那么多 0.0b?为什么要用1.5以上版本的jdk,为什么不是用 -classpath 参数,而是 -javaagent 参数?
我们不妨把 javasizeof 的src目录中源代码导入到 eclipse中(相信大多数人都用这个的),可以看到源代码 testsizeof,把 sizeof.skipflyweightobject(true) 行注释掉,运行 testsizeof。
不小心的话,你应该收到 java.lang.illegalstateexception: instrumentation is null 的异常,没错这个 sizeof 用到了 jdk1.5 后新加入的 java.lang.instrument.instrumentation 接口,所以您需要为 testsizeof 设置 vm arguments
-javaagent:"f:\component library\java\javasizeof\sizeof.jar"
(注意到在 sizeof.jar包中的 meta-inf\manifest.mf中有 premain-class: net.sourceforge.sizeof.sizeof,这就是 -javaagent: 所在意的。指向sizeof.jar的路径中有空格的话一定要用双引号引起来)
运行后结果就是:
javagent: call premain instrumentation for class sizeof
starting test...
simple obj:40.0b
int:16.0b
long:16.0b
char:16.0b
double:16.0b
boolean:16.0b
integer:16.0b
empty string:24.0b
not empty string:24.0b
not empty string:24.0b
simple obj:24.0b
simple obj:40.0b
empty list:24.0b
10 list:24.0b
20 list no static:24.0b
1000 o arr:816.0b
对于现在的输出值细细去体会吧,也许不符合您早已形成的较为稳固的想法,或许你发现确实存在出入。
sizeof_0_2_src.zip包中的 readme.txt 中部分内容:
java.sizeof is a simple java agent what can be used to calculate the memory size
of java objects.
the agent is implemented with the java.lang.instrument introduced with java 5.
here is a simple howto:
1) use the class sizeof in your code to test the size of your java object:
//configuration steps
sizeof.skipstaticfield(true);
sizeof.setminsizetolog(10);
//calculate object size
sizeof.iterativesizeof()
2) start the jvm with the argument: -javaagent:
/sizeof.jar
to avoid the dependencies of your code to sizeof the best use of the agent is in
conjunction with aspect.

======================================================

在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定
这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: