Integer vs int:关于内存

2022-01-14 00:00:00 integer int java

我想知道是否有不同的内存占用整数 nint n.

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

我知道int n正常占用4个字节,Integer n

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

推荐答案

一般来说,Hotspot中Java对象使用的堆内存包括:

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

  • 一个对象头,由几个字节的管家"信息组成;
  • 原始字段的内存,根据其大小(int n->32 位)
  • reference 字段的内存(每个 4 字节)(整数 n ->32 位)
  • 填充:在对象数据之后可能会浪费"一些未使用的字节,以使每个对象从一个方便的字节倍数的地址开始,并减少表示指向对象的指针所需的位数.
  • 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.

根据 Mark Peters 的建议,我想添加下面的链接http://www.javamex.com/tutorials/memory/object_memory_usage.shtml

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

相关文章