我想记录一个项目中对象所占用的内存量(以字节为单位),以便比较数据结构的大小。但是在Java中似乎没有相应的方法来实现这个目的。据说,在C/C ++中有一个名为sizeOf()
的方法,但在Java中不存在。我尝试使用Runtime.getRuntime().freeMemory()
在创建对象之前和之后记录JVM中的可用内存,然后记录差异,但无论数据结构中有多少元素,它只会给出0或131304,而且其他任何中间值都没有。请求帮助!
我想记录一个项目中对象所占用的内存量(以字节为单位),以便比较数据结构的大小。但是在Java中似乎没有相应的方法来实现这个目的。据说,在C/C ++中有一个名为sizeOf()
的方法,但在Java中不存在。我尝试使用Runtime.getRuntime().freeMemory()
在创建对象之前和之后记录JVM中的可用内存,然后记录差异,但无论数据结构中有多少元素,它只会给出0或131304,而且其他任何中间值都没有。请求帮助!
java.lang.instrumentation
包。import java.lang.instrument.Instrumentation;
public class ObjectSizeFetcher {
private static Instrumentation instrumentation;
public static void premain(String args, Instrumentation inst) {
instrumentation = inst;
}
public static long getObjectSize(Object o) {
return instrumentation.getObjectSize(o);
}
}
使用 getObjectSize
:public class C {
private int x;
private int y;
public static void main(String [] args) {
System.out.println(ObjectSizeFetcher.getObjectSize(new C()));
}
}
看看https://github.com/DimitrisAndreou/memory-measurer,Guava在内部使用它,并且ObjectGraphMeasurer
特别易于直接使用,无需任何特殊的命令行参数。
import objectexplorer.ObjectGraphMeasurer;
public class Measurer {
public static void main(String[] args) {
Set<Integer> hashset = new HashSet<Integer>();
Random random = new Random();
int n = 10000;
for (int i = 1; i <= n; i++) {
hashset.add(random.nextInt());
}
System.out.println(ObjectGraphMeasurer.measure(hashset));
}
}
MemoryMeasurer
;请按照那里的说明操作。 - Louis Wassermanjava.lang.instrument.Instrumentation
类提供了一种很好的方法来获取 Java 对象的大小,但是它要求你定义一个 premain
并且使用 Java 代理运行你的程序,如果你不需要任何代理,这就非常无聊了,然后你必须为你的应用程序提供一个虚假的 Jar 代理。
因此,我使用来自sun.misc
的 Unsafe
类的替代方案。所以,考虑到根据处理器架构的对象堆对齐和计算最大字段偏移量,可以测量 Java 对象的大小。在下面的示例中,我使用辅助类 UtilUnsafe
来获取对sun.misc.Unsafe
对象的引用。
private static final int NR_BITS = Integer.valueOf(System.getProperty("sun.arch.data.model"));
private static final int BYTE = 8;
private static final int WORD = NR_BITS/BYTE;
private static final int MIN_SIZE = 16;
public static int sizeOf(Class src){
//
// Get the instance fields of src class
//
List<Field> instanceFields = new LinkedList<Field>();
do{
if(src == Object.class) return MIN_SIZE;
for (Field f : src.getDeclaredFields()) {
if((f.getModifiers() & Modifier.STATIC) == 0){
instanceFields.add(f);
}
}
src = src.getSuperclass();
}while(instanceFields.isEmpty());
//
// Get the field with the maximum offset
//
long maxOffset = 0;
for (Field f : instanceFields) {
long offset = UtilUnsafe.UNSAFE.objectFieldOffset(f);
if(offset > maxOffset) maxOffset = offset;
}
return (((int)maxOffset/WORD) + 1)*WORD;
}
class UtilUnsafe {
public static final sun.misc.Unsafe UNSAFE;
static {
Object theUnsafe = null;
Exception exception = null;
try {
Class<?> uc = Class.forName("sun.misc.Unsafe");
Field f = uc.getDeclaredField("theUnsafe");
f.setAccessible(true);
theUnsafe = f.get(uc);
} catch (Exception e) { exception = e; }
UNSAFE = (sun.misc.Unsafe) theUnsafe;
if (UNSAFE == null) throw new Error("Could not obtain access to sun.misc.Unsafe", exception);
}
private UtilUnsafe() { }
}