java 获取对象的大小限制_如何获取Java中内存对象的大小? sizeof = ? | 学步园

本文介绍了如何通过Java代码计算对象在内存中的大小,包括使用Sizeof类进行内存分配和垃圾回收,以测量不同类型的对象占用的内存空间,如Object、Integer、Long、String等。
摘要由CSDN通过智能技术生成

今天无意中找到一篇比较权威的文章,描述如何获取Java对象的大小的。原文链接如下

把测试源代码贴出来,文章具体描述直接看原文吧。

/**

* A simple class to experiment with your JVM's garbage collector

* and memory sizes for various data types.

*

* @author Vladimir Roubtsov

*/

public class Sizeof {

public static void main(String [] args) throws Exception {

// "warm up" all classes/methods that we are going to use:

runGC();

usedMemory();

// array to keep strong references to allocated objects:

final int count = 10000; // 10000 or so is enough for small ojects

Object [] objects = new Object [count];

long heap1 = 0;

// allocate count+1 objects, discard the first one:

for (int i = -1; i < count; ++ i) {

Object object;

// INSTANTIATE YOUR DATA HERE AND ASSIGN IT TO 'object':

//object = new Object(); // 8 bytes

//object = new Integer(i); // 16 bytes

//object = new Long(i); // same size as Integer?

//object = createString(10); // 56 bytes? fine...

object = createString(9)+' '; // 72 bytes? the article explains why

//object = new char [10]; // 32 bytes

//object = new byte [32][1]; // 656 bytes?!

if (i >= 0)

objects [i] = object;

else {

object = null; // discard the "warmup" object

runGC();

heap1 = usedMemory(); // take a "before" heap snapshot

}

}

runGC();

long heap2 = usedMemory(); // take an "after" heap snapshot:

final int size = Math.round(((float)(heap2 - heap1))/count);

System.out.println("'before' heap: " + heap1 +

", 'after' heap: " + heap2);

System.out.println("heap delta: " + (heap2 - heap1) +

", {" + objects [0].getClass() + "} size = " + size + " bytes");

}

// a helper method for creating Strings of desired length

// and avoiding getting tricked by String interning:

public static String createString(final int length) {

final char [] result = new char [length];

for (int i = 0; i < length; ++ i)

result [i] = (char) i;

return new String(result);

}

// this is our way of requesting garbage collection to be run:

// [how aggressive it is depends on the JVM to a large degree, but

// it is almost always better than a single Runtime.gc() call]

private static void runGC() throws Exception {

// for whatever reason it helps to call Runtime.gc()

// using several method calls:

for (int r = 0; r < 4; ++ r)

_runGC();

}

private static void _runGC() throws Exception {

long usedMem1 = usedMemory(), usedMem2 = Long.MAX_VALUE;

for (int i = 0; (usedMem1 < usedMem2) && (i < 1000); ++ i) {

s_runtime.runFinalization();

s_runtime.gc();

Thread.currentThread().yield();

usedMem2 = usedMem1;

usedMem1 = usedMemory();

}

}

private static long usedMemory() {

return s_runtime.totalMemory() - s_runtime.freeMemory();

}

private static final Runtime s_runtime = Runtime.getRuntime();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值