4

是否可以在 java 中以字节为单位获取 Vector 或 ArrayList 或任何对象的大小?size() 函数只给出元素的数量。但我想得到对象的实际大小。

4

4 回答 4

0

这应该显示内存使用情况

Runtime rt = Runtime.getRuntime();
long m0 = rt.totalMemory() -  rt.freeMemory();
Object obj = new Object();      // create your object here
long m1 = rt.totalMemory() -  rt.freeMemory();
System.out.println(m1 - m0);

但它不起作用。这有效(至少在我的电脑上)

public static void main(String[] args) throws Exception {
    Object obj = createObject();
    long m0 = usedMem();
    long m1 = usedMem();
    obj = null;
    for (int i = 0; i < 100; i++)
        System.gc();
    m0 = usedMem();
    obj = createObject();
    for (int i = 0; i < 100; i++)
        System.gc();
    m1 = usedMem();
    System.out.println(m1 - m0);
}

private static long usedMem() {
    return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
}

private static Object createObject() {
    return new Object();
}

如果它8为 new Object 和40new String() 打印,那么它可以正常工作,然后创建你的对象并查看它的大小

在此处查看更多信息http://www.javaspecialists.eu/archive/Issue029.html

于 2013-04-17T12:15:30.927 回答
0

目前,我实现了自己的以获得大小。

long sizeof(ArrayList<String> list)
{
    long size = 0;
    for(String s: list)
        size+=s.length();
    return size;
}
于 2013-04-17T11:54:50.400 回答
0

不同的 JDK 对隐式对象有不同的大小。

您可以通过隐式对象大小乘以向量或数组列表的长度来计算它。例如,如果您声明一个包含 10 条记录的整数数组,那么 Java 整数是 32 位,即 32/8 = 4 个字节。您有 10 条记录,因此 10x4 = 40 字节。

在标准 java 字节 = 8 位,短 = 16 位,整数 = 32 位,长 = 64 位。

于 2013-04-17T11:58:40.883 回答
0

第一:尝试阅读Java中是否有像C中的“sizeof()”这样的运算符

第二:试试这个代码

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 [128][3]

            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 = 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");
        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.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 ();
} // End of class
于 2013-04-17T12:05:27.147 回答