Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any sizeof-like method in Java?

Tags:

java

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

like image 400
Ankit Sachan Avatar asked Mar 03 '10 10:03

Ankit Sachan


People also ask

Is there a sizeof function in Java?

Similarly, there is no sizeof() operator in Java. All primitive values have a predefined size, like int is 4 bytes, char is 2 byte, short is 2 byte, long and float is 8 byte, and so on.

Why sizeof is not in Java?

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.

How do you find the size of an object in Java?

Unlike languages such as C/ C++ which has a function sizeof() to get memory size of any object, Java has no such direct method. It is, still, possible to get the size of an object in Java using its instrumentation package. Instrumentation package in Java is used to instrument any Java code and get details of it.


2 Answers

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.

like image 190
Stephen C Avatar answered Oct 20 '22 12:10

Stephen C


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

like image 32
Ramesh Avatar answered Oct 20 '22 11:10

Ramesh