48

I was wondering if there is a difference in the memory occupied by Integer n, and int n.

I know int n occupies 4 bytes normally, how about Integer n

Vivien Barousse
  • 20,555
  • 2
  • 63
  • 64
TimeToCodeTheRoad
  • 7,032
  • 16
  • 57
  • 70
  • 3
    The only possible answer is "it depends". – Kerrek SB Dec 07 '11 at 17:38
  • Related to http://stackoverflow.com/questions/76549/how-are-integer-arrays-stored-internally-in-the-jvm although not stricly a duplicate. – Ray Toal Dec 07 '11 at 17:40
  • @KerrekSB Can you elaborate on why "it depends?" – Craig Otis Dec 07 '11 at 17:42
  • 2
    @craig: Because as you hinted at in your answer, it's totally up to the JVM implementation how much heap space it wants to take for each of those things. If I'm not mistaken, there's nothing that would stop a compliant JVM from storing an `int` as 128 bits, as long as that decision doesn't leak out to the hosted program. All you can know is that the "width" of `int` and `Integer` are the same; they can both represent the same number of values. – Mark Peters Dec 07 '11 at 17:45
  • First of all depends on hardware: x86 (pointer "weights" 4 bytes) and x64 (pointer "weights" 8 bytes). Secondly it depends of JVM but I assume you imply Oracle JVM. – korifey Dec 07 '11 at 17:45
  • @craig: It's entirely possible that the JVM recognizes that a local `Integer` cannot possibly be used elsewhere and chooses to treat it like a plain `int`... or a really naive JVM could wrap *everything* and manage it through a GC. There's really no telling. Presumably you can concoct a situation in which an `int` requires a small amount of memory and an `Integer` causes a gcheap allocation, but the mechanics of this aren't under your control, and neither are they mandated by the language. – Kerrek SB Dec 07 '11 at 17:50
  • @Kerrek. If I write a program like `main ( String [ ] args ) { System . out ( int i ; System . out . println ( i ) : }` would the naive JVM throw a NullPointerException? – emory Dec 07 '11 at 18:16
  • 2
    Great link: http://www.javamex.com/tutorials/memory/object_memory_usage.shtml – Gray Dec 07 '11 at 18:52

4 Answers4

54

In general, the heap memory used by a Java object in Hotspot consists of:

  • an object header, consisting of a few bytes of "housekeeping" information;
  • memory for primitive fields, according to their size (int n->32 bits)
  • memory for reference fields (4 bytes each) (Integer n ->32 bits)
  • padding: potentially a few "wasted" unused bytes after the object data, to make every object start at an address that is a convenient multiple of bytes and reduce the number of bits required to represent a pointer to an object.

as per the suggestion of Mark Peters I would like add the link below http://www.javamex.com/tutorials/memory/object_memory_usage.shtml

rilaby
  • 603
  • 5
  • 15
Noufal Panolan
  • 1,357
  • 2
  • 14
  • 27
  • 1
    Upvote for being the only answer to qualify it with "in Hotspot". But downvote for not citing your source. – Mark Peters Dec 07 '11 at 17:51
  • thank for your suggestion and I have added the site link for more information. – Noufal Panolan Dec 08 '11 at 19:04
  • 1
    Also note that in addition to the heap memory requirement for the object (which may well be "zero" if a cached instance is re-used many times, as happens with `Integer.valueOf(small)`), you still need to store the pointer to the object in your stack frame or containing object. That is another 32 or 64 bits, i.e. the pointer also is already at least as big as an `int`. – Thilo Apr 19 '18 at 07:46
  • what is the reference fields used for? – lily Nov 15 '21 at 12:48
11

An Integer object in Java occupies 16 bytes.

I don't know whether running a 64- vs 32-bit JVM makes a difference. For primitive types, it does not matter. But I can not say for certain how the memory footprint of an object changes (if at all) under a 64-bit system.

You can test this for yourself here:

Java Tip 130: Do you know your data size?

Craig Otis
  • 31,257
  • 32
  • 136
  • 234
8

int is a primitive data type which takes 32 bits(4 bytes) to store.

When your Java code uses the new operator to create an instance of a Java object, much more data is allocated than you might expect. For example, it might surprise you to know that the size ratio of an int value to an Integer object — the smallest object that can hold an int value — is typically 1:4.

Integer is an object which takes 128 bits (16 bytes) to store int value.

When we creates new Integer using new Operator it allocates memory as per follows.

  1. Class Object(32 bits) - which consist of a pointer to the class information, which describes the object in our case its point to java.lang.Integer class

  2. Flags (32 bits)- It is collection of flags that describes the state of object. Like is it has hash-code, is it array or not i.e. its Shape.

  3. Lock (32 bits) - It stores synchronization information of object. whether the object currently synchronized or not.

Above 3 points are called as metadata of an Object.

  1. Lastly metadata is followed by the Object data (32 bits) itself. In case of Integer its single int value.

All the above explanation is as per 32 bit processor architecture. It can differ from JVM version and vendor.

navnath
  • 3,548
  • 1
  • 11
  • 19
4

For int: 4 bytes used per element without wrappers, and 16 per element with a wrapper.

A wrapped double reports as 24 bytes per element, with the actual double value as 64 bits (8 bytes).

For more details here

java_mouse
  • 2,069
  • 4
  • 21
  • 30