Is there any sizeof-like method in Java?

后端 未结 15 990
暗喜
暗喜 2020-11-27 15:03

Is there any built-in method in Java to find the size of any datatype? Is there any way to find size?

相关标签:
15条回答
  • 2020-11-27 15:53

    I don't think it is in the java API. but most datatypes which have a number of elements in it, have a size() method. I think you can easily write a function to check for size yourself?

    0 讨论(0)
  • 2020-11-27 15:55

    There is a contemporary way to do that for primitives. Use BYTES of types.

    System.out.println("byte " + Byte.BYTES);
    System.out.println("char " + Character.BYTES);
    System.out.println("int " + Integer.BYTES);
    System.out.println("long " + Long.BYTES);
    System.out.println("short " + Short.BYTES);
    System.out.println("double " + Double.BYTES);
    System.out.println("float " + Float.BYTES);
    

    It results in,

    byte 1
    char 2
    int 4
    long 8
    short 2
    double 8
    float 4
    
    0 讨论(0)
  • 2020-11-27 15:58

    From the article in JavaWorld

    A superficial answer is that Java does not provide anything like C's sizeof(). However, let's consider why a Java programmer might occasionally want it.

    A C programmer manages most datastructure memory allocations himself, and sizeof() is indispensable for knowing memory block sizes to allocate. Additionally, C memory allocators like malloc() do almost nothing as far as object initialization is concerned: a programmer must set all object fields that are pointers to further objects. But when all is said and coded, C/C++ memory allocation is quite efficient.

    By comparison, Java object allocation and construction are tied together (it is impossible to use an allocated but uninitialized object instance). If a Java class defines fields that are references to further objects, it is also common to set them at construction time. Allocating a Java object therefore frequently allocates numerous interconnected object instances: an object graph. Coupled with automatic garbage collection, this is all too convenient and can make you feel like you never have to worry about Java memory allocation details.

    Of course, this works only for simple Java applications. Compared with C/C++, equivalent Java datastructures tend to occupy more physical memory. In enterprise software development, getting close to the maximum available virtual memory on today's 32-bit JVMs is a common scalability constraint. Thus, a Java programmer could benefit from sizeof() or something similar to keep an eye on whether his datastructures are getting too large or contain memory bottlenecks. Fortunately, Java reflection allows you to write such a tool quite easily.

    Before proceeding, I will dispense with some frequent but incorrect answers to this article's question. Fallacy: Sizeof() is not needed because Java basic types' sizes are fixed

    Yes, a Java int is 32 bits in all JVMs and on all platforms, but this is only a language specification requirement for the programmer-perceivable width of this data type. Such an int is essentially an abstract data type and can be backed up by, say, a 64-bit physical memory word on a 64-bit machine. The same goes for nonprimitive types: the Java language specification says nothing about how class fields should be aligned in physical memory or that an array of booleans couldn't be implemented as a compact bitvector inside the JVM. Fallacy: You can measure an object's size by serializing it into a byte stream and looking at the resulting stream length

    The reason this does not work is because the serialization layout is only a remote reflection of the true in-memory layout. One easy way to see it is by looking at how Strings get serialized: in memory every char is at least 2 bytes, but in serialized form Strings are UTF-8 encoded and so any ASCII content takes half as much space

    0 讨论(0)
提交回复
热议问题