Integer vs int: with regard to memory

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?


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

Tags:

Java

Integer

Int