本文介绍了Integer vs int:关于内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有不同的内存占用整数 n
和 int 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
这篇关于Integer vs int:关于内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!