Integer vs int: with regard to memory

前端 未结 4 1049
春和景丽
春和景丽 2020-11-28 08:45

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 normal

4条回答
  •  庸人自扰
    2020-11-28 08:59

    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

提交回复
热议问题