100

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

1

16 Answers 16

70

No. There is no such method in the standard Java SE class library.

The designers' view is that it is not needed in Java, since the language removes the need for an application1 to know about how much space needs to be reserved for a primitive value, an object or an array with a given number of elements.

You might think that a sizeof operator would be useful for people that need to know how much space their data structures take. However you can also get this information and more, simply and reliably using a Java memory profiler, so there is no need for a sizeof method.


Previous commenters made the point that sizeof(someType) would be more readable than 4. If you accept that readability argument, then the remedy is in your hands. Simply define a class like this ...

public class PrimitiveSizes {
    public static int sizeof(byte b) { return 1; } 
    public static int sizeof(short s) { return 2; }
    // etcetera
}

... and statically import it ...

import static PrimitiveSizes.*;

Or define some named constants; e.g.

public static final int SIZE_OF_INT = 4;

Or (Java 8 and later) use the Integer.BYTES constant, and so on.


Why haven't the Java designers implemented this in standard libraries? My guess is that:

  • they don't think there is a need for it,
  • they don't think there is sufficient demand for it, and
  • they don't think it is worth the effort.

There is also the issue that the next demand would be for a sizeof(Object o) method, which is fraught with technical difficulties.

The key word in the above is "they"!


1 - A programmer may need to know in order to design space efficient data structures. However, I can't imagine why that information would be needed in application code at runtime via a method call.

0
15

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

2
  • 8
    "Fortunately, Java reflection allows you to write such a tool quite easily." How?
    – dfrankow
    Dec 27, 2012 at 18:23
  • @dfrankow - Traverse the Class chain for the class and its superclasses. For each Class, call getDeclaredFields(). Then call getType() on each field. Then compute the memory size based on the sizes of the respective primitive types and references + object header + packing. Note: the computation will be platform specific. But also note that this has already been implemented by other people; see some other answers and chase down the respective source code for specific details.
    – Stephen C
    Mar 2, 2023 at 5:00
11

The Java Native Access library is typically used for calling native shared libraries from Java. Within this library there exist methods for determining the size of Java objects:

The getNativeSize(Class cls) method and its overloads will provide the size for most classes.

Alternatively, if your classes inherit from JNA's Structure class the calculateSize(boolean force) method will be available.

2
  • 1
    Thank you for this answer! Trying to use JNA where an argument is derived using C's sizeof() and this is exactly what I needed! May 19, 2015 at 4:59
  • @sprocketonline - the links are broken, could you please update them
    – Naman
    Dec 20, 2016 at 4:55
11

You can do bit manipulations like below to obtain the size of primitives:

public int sizeofInt() {
    int i = 1, j = 0;
    while (i != 0) {
        i = (i<<1); j++;
    }
    return j;
}

public int sizeofChar() {
    char i = 1, j = 0;
    while (i != 0) {
        i = (char) (i<<1); j++;
    }
    return j;
}
3
  • 2
    sizeof (in C) returns the number of bytes, while these routines would return the number of bits.
    – LarsH
    Nov 19, 2013 at 1:58
  • 5
    Using i<<8 instead will give the number of bytes. May 29, 2015 at 9:29
  • Well, yes you can calculate the bytes / bits like this, but it is unnecessary. The Java Language Specification guarantees that the number of bytes needed to represent each primitive type is never going to change.
    – Stephen C
    Nov 2, 2016 at 13:23
11

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
9

As mentioned here, there are possibilities to get the size of primitive types through their wrappers.

e.g. for a long this could be Long.SIZE / Byte.SIZE from java 1.5 (as mentioned by zeodtr already) or Long.BYTES as from java 8

3

You can use Integer.SIZE / 8, Double.SIZE / 8, etc. for primitive types from Java 1.5.

0
2

The Instrumentation class has a getObjectSize() method however, you shouldn't need to use it at runtime. The easiest way to examine memory usage is to use a profiler which is designed to help you track memory usage.

1

EhCache provides a SizeOf class that will try to use the Instrumentation agent and will fall back to a different approach if the agent is not loaded or cannot be loaded (details here).

Also see the agent from Heinz Kabutz.

1

I decided to create an enum without following the standard Java conventions. Perhaps you like this.

public enum sizeof {
    ;
    public static final int FLOAT = Float.SIZE / 8;
    public static final int INTEGER = Integer.SIZE / 8;
    public static final int DOUBLE = Double.SIZE / 8;
}
1

Try java.lang.Instrumentation.getObjectSize(Object). But please be aware that

It returns an implementation-specific approximation of the amount of storage consumed by the specified object. The result may include some or all of the object's overhead, and thus is useful for comparison within an implementation but not between implementations. The estimate may change during a single invocation of the JVM.

0

There's a class/jar available on SourceForge.net that uses Java instrumentation to calculate the size of any object. Here's a link to the description: java.sizeOf

0

Just some testing about it:

public class PrimitiveTypesV2 {

public static void main (String[] args) {
    Class typesList[] = {
            Boolean.class , Byte.class, Character.class, Short.class, Integer.class,
            Long.class, Float.class, Double.class, Boolean.TYPE, Byte.TYPE, Character.TYPE,
            Short.TYPE, Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE
        };
    
    try {               
        for ( Class type : typesList ) {
            if (type.isPrimitive()) {
                System.out.println("Primitive type:\t" + type); 
            }
            else {
                boolean hasSize = false;
                java.lang.reflect.Field fields[] = type.getFields();
                for (int count=0; count<fields.length; count++) {
                    if (fields[count].getName().contains("SIZE")) hasSize = true;
                }
                if (hasSize) {
                    System.out.println("Bits size of type " + type + " :\t\t\t" + type.getField("SIZE").getInt(type) );
                    double value = type.getField("MIN_VALUE").getDouble(type);
                    long longVal = Math.round(value);
                    if ( (value - longVal) == 0) {
                        System.out.println("Min value for type " + type + " :\t\t" + longVal );
                        longVal = Math.round(type.getField("MAX_VALUE").getDouble(type));
                        System.out.println("Max value for type " + type + " :\t\t" + longVal );
                    }
                    else {
                        System.out.println("Min value for type " + type + " :\t\t" + value );
                        value = type.getField("MAX_VALUE").getDouble(type);
                        System.out.println("Max value for type " + type + " :\t\t" + value );
                    }
                }
                else {
                    System.out.println(type + "\t\t\t type without SIZE field.");
                }
            } // if not primitive
        } // for typesList
    } catch (Exception e) {e.printStackTrace();}
} // main
} // class PrimitiveTypes
0

Here is a Class I have used for estimating how the memory behaves in my application.

https://github.com/BeatEngine/java-sizeof-object/blob/main/MemoryObjectSizeCalculation.java

It parses the objects and sum the size of (native) simple types.

private void parse(final Object someObject) {
        boolean couldBeNative = true;
        for (Field field : getAllFields(someObject)) {
            Object value;
            try {
                field.setAccessible(true);
                value = field.get(someObject);
                couldBeNative = false;
            } catch (IllegalAccessException | InaccessibleObjectException e) {
                continue;
            }
            if (value != null) {
                if (visitedObjects.add(value)) {
                    if (addSizeNeedToParse(value)) {
                        parse(value);
                    }
                }
            }
        }
        if (couldBeNative) {
            addSizeNeedToParse(someObject);
        }
    }
-1

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?

-1

yes..in JAVA

System.out.println(Integer.SIZE/8);  //gives you 4.
System.out.println(Integer.SIZE);   //gives you 32.

//Similary for Byte,Long,Double....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.