java实现sizeof功能

本文介绍了一种在Java中模拟C语言中sizeof关键字的方法,通过测量GC前后堆内存的变化来估算对象的大小,并给出了具体实现代码及测试结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java中模拟c中对sizeof的实现思路:利用java中GC内存回收前后的heap size差别,得出每个object的大小。

这是一个程序,java中没有现成的sizeof的实现,原因主要是java中的基本数据类型的大小都是固定的,所以看上去没有必要用sizeof这个关键字。实现的想法是这样的:java.lang.Runtime类中有一些简单的能涉及到内存管理的函数:Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method. long freeMemory()
Returns the amount of free memory in the Java Virtual Machine.
void gc()
Runs the garbage collector.
static Runtime getRuntime()
Returns the runtime object associated with the current Java application.
long maxMemory()
Returns the maximum amount of memory that the Java virtual machine will attempt to use.
void runFinalization()
Runs the finalization methods of any objects pending finalization.
使用这些简单的内存访问,可以得到内存的一些情况,我们通过建立一个大的某个类的数组,来查看内存用了多少,进而可以求得类的大小。程序原码:/* * 创建日期 2007-4-19*/ public class Sizeof { public static void main(String[] args) throws Exception { // Warm up all classes/methods we will use runGC(); usedMemory(); // Array to keep strong references to allocated objects final int count = 100000; 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 = null; /* Instantiate your data here and assign it to object*/ //object = new Object (); //object = new Integer (i); // ???? //object = new Long (i); //???? //object = new String (); //object = new Byte( (byte) 0); //???? //object = new Float( 0.2f); //???? //object = new Double( 0); //???? //object = new HelloWorld(); if (i >= 0) objects[i] = object; else { object = null; // Discard the warm up object runGC(); heap1 = usedMemory(); // Take a before heap snapshot } } runGC(); long heap2 = usedMemory(); // Take an after heap snapshot: final int size = (int) Math.round(((double) (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"); for (int i = 0; i < count; ++i) objects[i] = null; objects = null; } private static void runGC() throws Exception { // 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 < 500); ++i) { s_runtime.runFinalization(); s_runtime.gc(); Thread.yield(); usedMem2 = usedMem1; usedMem1 = usedMemory(); } } private static long usedMemory() { return s_runtime.totalMemory() - s_runtime.freeMemory(); } private static final Runtime s_runtime = Runtime.getRuntime(); } // End of class 需要注意的是上面的一句: Object[] objects = new Object[count];只是分配了数组空间,没有分配对象的空间。数组中只有引用而已。 结论:下代码测试基本对象时,得出的结果象下面:
Object类对象是 8字节
Integer类对象是 16字节
Long类对象是 16字节
Byte类对象是 16字节
怎么会这样呢???不解。 查看网上的帖子:解释如下: 这个例子写的很好,正好说明了java中基本类型封装对象所占内存的大小.
1.简单的Object对象要占用8个字节的内存空间,因为每个实例都至少必须包含一些最基本操作,比如:wait()/notify(),equals(), hashCode()等
2.使用Integer对象占用了16个字节,而int占用4个字节,说了封装了之后内存消耗大了4倍
3.那么Long看起来比Integer对象应该使用更多空间,结果Long所占的空间也是16个字节.
那么就正好说明了JVM的对于基本类型封装对象的内存分配的规则是如下:
Object所占内存(8个字节)+最大基本类型(long)所占内存(8个字节) = 16字节.
JVM强制使用8个字节作为边界.
所以所有基本类型封装对象所占内存的大小都是16字节.但是还是有区别,比如:Integer对象虽然占用了16个字节的内存,但是只是利用了 Object所占内存(8个字节)+int所占内存(4个字节) = 12字节.还有4个字节根本没有被使用.呵呵,仔细分析了一晚,还是有很多收获的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值