简体   繁体   English

Java:向量的大小(以字节为单位)

[英]Java: Size of a Vector in bytes

Is it possible to get the size of the Vector or ArrayList or any object for that matter in bytes in java? 是否有可能在java中以字节为单位获取Vector或ArrayList或任何对象的大小? size() function gives only the number of elements. size()函数只给出元素的数量。 But I want to get the actual size of the object. 但我想得到对象的实际大小。

For the time being, I implemented my own for getting size. 目前,我实现了自己的规模。

long sizeof(ArrayList<String> list)
{
    long size = 0;
    for(String s: list)
        size+=s.length();
    return size;
}

Different JDK having different size for the implicite objects. 不同的JDK对于隐含对象具有不同的大小。

You can count it by implicite object size multiply the length of the vector or arraylist. 您可以通过隐含对象大小乘以向量或arraylist的长度来计算它。 For Example if you declare a araylist of integer with 10 records in it, then Java integers are 32 bits ie 32/8 = 4 bytes. 例如,如果声明一个包含10条记录的整数的araylist,则Java整数为32位,即32/8 = 4字节。 You have 10 records hence 10x4 = 40 byetes. 你有10条记录,因此10x4 = 40 byetes。

in standard java byte = 8 bits, short=16bits, int=32bits, long=64bits. 标准java字节= 8位,短= 16位,int = 32位,长= 64位。

First : try to read Does Java have an operator like "sizeof()" in C 第一:尝试阅读Java中是否有像“sizeof()”这样的运算符

Second : Try this code : 第二: 试试这段代码

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

This is supposed to show memory usage 这应该显示内存使用情况

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);

but it does not work. 但它不起作用。 This works (at least on my PC) 这有效(至少在我的电脑上)

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();
}

if it prints 8 for new Object and 40 for new String() then it works correctly, then create your object and see its size 如果它为新的Object打印8 ,为新的String()打印40 ,那么它可以正常工作,然后创建您的对象并查看其大小

See more here http://www.javaspecialists.eu/archive/Issue029.html 在这里查看更多http://www.javaspecialists.eu/archive/Issue029.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM