[备忘] Java Object Size

参考文章: http://devblog.streamy.com/2009/07/24/determine-size-of-java-object-class/

Java没有 C 语言的 sizeof, 所以一个对象所占用的内存, 需要用profiler 工具才能得出.

当项目的最底层的对象有千万数量级别时, 需要非常谨慎地设计类, 以控制 Object Size, 否则很容易出现 Out of Memory.

无论是静态函数, 还是成员函数, 都不会计入 Object Size, 所以可以放心地加入function.

尽量用原生数据类型(byte, char, boolean, int ,long, xxx), 而不是wrapper类型(Integer, Boolean, Long, xxx)

int 占 4个字节, 而 Integer占20个字节

String 占 12 bytes, boolean = 1 byte,  byte = 1 byte, int = 4 bytes, long = 8 bytes.

除了原生数据类型和 wrapper类型, 一般对象型数据成员占 4 个 bytes, 有点象指针.

要尽量减少成员变量的数目:

1. 不要为中间运算方便而设置的成员变量.

2. 不要将业务上的parent对象放进来, 尽管在OOP中, 这样会带来很多的方便.

不用说, 集合肯定是优先用 [], 而不是java.util.xxx中的Collections类(比如Map,List)

smithfox | Monday 30 May 2011 at 5:06 pm | | Java       

three comments

Chanel iPhone 6 Cases

Android is one of the most common operating systems found on many devices around the globe today. The latest iteration is Android 5+ Lollipop, which is found on the Nexus 9 as we have it.

Chanel iPhone 6 Cases, (URL) - 20-04-’15 18:22
Selfie Stick with Bluetooth Remote

At the conclusion of the video you’ll see all of the aspects mentioned for each phone listed side-by-side for an easy comparison. By looking at the things more important to you it should be easier to choose between them.

Selfie Stick with Bluetooth Remote, (URL) - 18-06-’15 16:34
Yeezy Boost 350 Pink

The shoes will cost $2,000 to be customized, customers would have to send their original shoes to Chambrone to have them transformed, and it will take at least two months before they’re returned.

Yeezy Boost 350 Pink, (URL) - 01-06-’16 14:41
(optional field)
(optional field)
为阻止垃圾广告, 请在提交评论前, 回答一个简单问题(Please answer an simple question)
Remember personal info?
Notify
Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.