What is the memory consumption of an object in Java?

for how long will an object take up memory in java
size of json object in bytes java
how to calculate object size
java object size library
java string memory size
java getobjectsize
object length java
java object overhead

Is the memory space consumed by one object with 100 attributes the same as that of 100 objects, with one attribute each?

How much memory is allocated for an object? How much additional space is used when adding an attribute?

Memory usage of Java objects: general guide, General formula for calculating memory usage. In general, the heap memory used by a Java object in Hotspot consists of: an object header, consisting of a few​  20 x 16 bytes = 320 bytes for integer objects. The total size of the said Java array object = 12 + 320 bytes = 332 bytes + 4 bytes (padding) = 336 bytes. Memory usage of both Java Objects and Array: This article presents more examples on memory usage of objects types such as String.

It depends on architecture/jdk. For a modern JDK and 64bit architecture, an object has 12-bytes header and padding by 8 bytes - so minimum object size is 16 bytes. You can use a tool called Java Object Layout to determine a size and get details about object layout and internal structure of any entity or guess this information by class reference. Example of an output for Integer on my environment:

Running 64-bit HotSpot VM.
Using compressed oop with 3-bit shift.
Using compressed klass with 3-bit shift.
Objects are 8 bytes aligned.
Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]

java.lang.Integer object internals:
      0    12       (object header)                N/A
     12     4   int Integer.value                  N/A
Instance size: 16 bytes (estimated, the sample instance is not available)
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

So, for Integer, instance size is 16 bytes, because 4-bytes int compacted in place right after header and before padding boundary.

Code sample:

import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.util.VMSupport;

public static void main(String[] args) {

If you use maven, to get JOL:


How to Get the Size of an Object in Java, Memory Consumption in Java. Although there is no sizeof operator in Java, we actually don't need one. All primitive types have a standard size,  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

Each object has a certain overhead for its associated monitor and type information, as well as the fields themselves. Beyond that, fields can be laid out pretty much however the JVM sees fit (I believe) - but as shown in another answer, at least some JVMs will pack fairly tightly. Consider a class like this:

public class SingleByte
    private byte b;


public class OneHundredBytes
    private byte b00, b01, ..., b99;

On a 32-bit JVM, I'd expect 100 instances of SingleByte to take 1200 bytes (8 bytes of overhead + 4 bytes for the field due to padding/alignment). I'd expect one instance of OneHundredBytes to take 108 bytes - the overhead, and then 100 bytes, packed. It can certainly vary by JVM though - one implementation may decide not to pack the fields in OneHundredBytes, leading to it taking 408 bytes (= 8 bytes overhead + 4 * 100 aligned/padded bytes). On a 64 bit JVM the overhead may well be bigger too (not sure).

EDIT: See the comment below; apparently HotSpot pads to 8 byte boundaries instead of 32, so each instance of SingleByte would take 16 bytes.

Either way, the "single large object" will be at least as efficient as multiple small objects - for simple cases like this.

[JavaSpecialists 029], In Java, memory is allocated in various places such as the stack, heap, etc. In this newsletter I'm only going to look at objects which are stored on  Memory Consumption of the Java program Total runtime of a program In case the program is to some case a program which interacts with others also the response time is a very important fact of the performance of a program. A average CPU can do approximately 1 billion (10^9) operations per second.

No, registering an object takes a bit of memory too. 100 objects with 1 attribute will take up more memory.

Measuring actual memory consumption in Java: JMnemohistosyne, How much memory has been consumed by objects that have been instantiated and remain resident in memory? How many instances of each  So actually, Java does not collect any garbage. In fact, the more garbage there is, and the fewer that objects are marked alive, the faster the process is. To make this even more optimized, heap memory actually consists of multiple parts. We can visualize the memory usage and other useful things with JVisualVM,

It appears that every object has an overhead of 16 bytes on 32-bit systems (and 24-byte on 64-bit systems).

http://algs4.cs.princeton.edu/14analysis/ is a good source of information. One example among many good ones is the following.

http://www.cs.virginia.edu/kim/publicity/pldi09tutorials/memory-efficient-java-tutorial.pdf is also very informative, for example:

Java Tip 130: Do you know your data size?, developers have asked, "How much memory does a Java object consume? exploring memory usage, he offers a few tips on working around some Java   Memory Consumption in Java Although there is no sizeof operator in Java, we actually don't need one. All primitive types have a standard size, and there are typically no pad or alignment bytes. Still, this isn't always straightforward.

Java Performance - Memory and Runtime Analysis, The following code example demonstrate its usage. I assume you will know how to create a object Person yourself. Firstly "the size of an object" isn't a well-defined concept in Java. You could mean the object itself, with just its members, the Object and all objects it refers to (the reference graph). You could mean the size in memory or the size on disk. And the JVM is allowed to optimise things like Strings.

Java Memory Management, Stack memory is responsible for holding references to heap objects and for java ,java memory management ,java memory leak ,garbage  Whether it's a direct memory pointer or not, its goal is to get to the data for the object. That's basically all that really matters. If there's some "spare" bits (e.g. it's a 64-bit reference and you don't need all of that width just to represent the object's location) then the VM can use that data for other information such as its type, which

Calculating the Memory Usage of Objects and Arrays, For example, lets say, you want to calculate the memory of a Java object which holds two int variables, one boolean variable, one Long object,  It's the capacity of the java.util.ArrayList multiplied by the reference size (4 bytes on 32bit, 8bytes on 64bit) + [Object header + one int and one references]. Technically speaking the ArrayList has an Object[] where it stores the data. This is what a memory profiler is for. It will tell you for your platform.

  • int[128][6]: 128 arrays of 6 ints - 768 ints in total, 3072 bytes of data + 2064 bytes Object overhead = 5166 bytes total. int[256]: 256 ints in total - therefore non-comparable. int[768]: 3072 bytes of data + 16 byes overhead - about 3/5th of the space of the 2D array - not quite 246% overhead!
  • Ah, the original article used int[128][2] not int[128][6] - wonder how that got changed. Also shows that extreme examples can tell a different story.
  • The overhead is 16 bytes in 64bit JVM's.
  • @AlexWien: Some garbage-collection schemes may impose a minimum object size which is separate from padding. During garbage collection, once an object gets copied from an old location to a new one, the old location may not need to hold the data for the object any more, but it will need to hold a reference to the new location; it may also need to store a reference to the old location of the object wherein the first reference was discovered and the offset of that reference within the old object [since the old object may still contain references that haven't yet been processed].
  • @AlexWien: Using memory at an object's old location to hold the garbage-collector's book-keeping information avoids the need to allocate other memory for that purpose, but may impose a minimum object size which is larger than would otherwise be required. I think at least one version of .NET garbage collector uses that approach; it would certainly be possible for some Java garbage collectors to do so as well.
  • Actually, one instance of SingleByte would take 16 bytes on a Sun JVM, that is 8 bytes overhead, 4 bytes for the field, and then 4 bytes for object padding, since the HotSpot compiler rounds everything to multiples of 8.
  • "It appears that every object has an overhead of 16 bytes on 32-bit systems (and 24-byte on 64-bit systems)." It's not correct, at least for current JDKs. Take a look at my answer for Integer example. Overhead of object is at least 12 bytes for header for 64-bit system and modern JDK. Can be more because of padding, depends on actual layout of fields in object.
  • The second link to the memory efficient Java tutorial seems to be dead–I get "Forbidden".
  • a char is 16bit, not 8bit.
  • right you are. someone seems to have edited my original answer
  • Readers may also find this paper very illuminating: cs.virginia.edu/kim/publicity/pldi09tutorials/…
  • This worked great for getting a rough estimate of how much memory a (String, Integer) Guava Cache uses, per-element. Thanks!
  • I am pretty sure Sun Java 1.6 64bit, need 12 bytes for a plain object + 4 padding = 16; an Object + one integer field = 12 + 4 = 16