http://stackoverflow.com/questions/1113819/arrays-heap-and-stack-and-value-types

Your array is allocated on the heap, and the ints are not boxed.

The source of your confusion is likely because people have said that reference types are allocated on the heap and value types are allocated on the stack. This is not an entirely accurate representation.

All local variables and parameters are allocated on the stack. This includes both value types and reference types. The difference between the two is only what is stored in the variable. Unsurprisingly, for a value type, the value of the type is stored directly in the variable, and for a reference type, the value of the type is stored on the heap, and a reference to this value is what is stored in the variable.

The same holds true for fields. When memory is allocated for an instance of an aggregate type (a class or a struct), it must include storage for each of its instance fields. For reference-type fields, this storage holds just a reference to the value, which would itself be allocated on the heap later. For value-type fields, this storage holds the actual value.

So, given the following types:

class RefType{
public int I;
public string S;
public long L;
}

struct ValType{
public int I;
public string S;
public long L;
}
The values of each of these types would require 16 bytes of memory (assuming a 32-bit word size). The field I in each case takes 4 bytes to store its value, the field S takes 4 bytes to store its reference, and the field L takes 8 bytes to store its value. So the memory for the value of both RefType and ValType looks like this:

0 ┌───────────────────┐
│ I │
4 ├───────────────────┤
│ S │
8 ├───────────────────┤
│ L │
│ │
16 └───────────────────┘
Now if you had three local variables in a function, of types RefType, ValType, and int[], like this:

RefType refType;
ValType valType;
int[] intArray;
then your stack might look like this:

0 ┌───────────────────┐
│ refType │
4 ├───────────────────┤
│ valType │
│ │
│ │
│ │
20 ├───────────────────┤
│ intArray │
24 └───────────────────┘
If you assigned values to these local variables, like so:

refType = new RefType();
refType.I = 100;
refType.S = "refType.S";
refType.L = 0x0123456789ABCDEF;

valType = new ValType();
valType.I = 200;
valType.S = "valType.S";
valType.L = 0x0011223344556677;

intArray = new int[4];
intArray[0] = 300;
intArray[1] = 301;
intArray[2] = 302;
intArray[3] = 303;
Then your stack might look something like this:

0 ┌───────────────────┐
│ 0x4A963B68 │ -- heap address of refType
4 ├───────────────────┤
│ 200 │ -- value of valType.I
│ 0x4A984C10 │ -- heap address of valType.S
│ 0x44556677 │ -- low 32-bits of valType.L
│ 0x00112233 │ -- high 32-bits of valType.L
20 ├───────────────────┤
│ 0x4AA4C288 │ -- heap address of intArray
24 └───────────────────┘
Memory at address 0x4A963B68 (value of refType) would be something like:

0 ┌───────────────────┐
│ 100 │ -- value of refType.I
4 ├───────────────────┤
│ 0x4A984D88 │ -- heap address of refType.S
8 ├───────────────────┤
│ 0x89ABCDEF │ -- low 32-bits of refType.L
│ 0x01234567 │ -- high 32-bits of refType.L
16 └───────────────────┘
Memory at address 0x4AA4C288 (value of intArray) would be something like:

0 ┌───────────────────┐
│ 4 │ -- length of array
4 ├───────────────────┤
│ 300 │ -- intArray[0]
8 ├───────────────────┤
│ 301 │ -- intArray[1]
12 ├───────────────────┤
│ 302 │ -- intArray[2]
16 ├───────────────────┤
│ 303 │ -- intArray[3]
20 └───────────────────┘
Now if you passed intArray to another function, the value pushed onto the stack would be 0x4AA4C288, the address of the array, not a copy of the array.

Byte[]分配在哪里?的更多相关文章

  1. 20165312 2017-2018-2 《JAVA程序设计》第2周学习总结

    20165312 2017-2018-2 <JAVA程序设计>第2周学习总结 一.对上一周学习的查漏补缺 1.上周在虚拟机中进行编译程序时出现错误,在上一周的博客中我有提到,当时还未找到解 ...

  2. CLR寄宿和AppDomain

    一.CLR寄宿 .net framework在windows平台的顶部允许.者意味着.net framework必须用windows能理解的技术来构建.所有托管模块和程序集文件必须使用windows ...

  3. go-byte数组最大的长度

    本来想打算用go来处理一个1G左右的txt文本的,但是在去读取的时候就报内存溢出了,提示数组已经无法在分配. 用的是:ioutil.ReadFile 方法来读取文本,它的返回值是一个[]byte 数组 ...

  4. 重温CLR(十六) CLR寄宿和AppDomain

    寄宿(hosting)使任何应用程序都能利用clr的功能.特别要指出的是,它使现有应用程序至少能部分使用托管代码编写.另外,寄宿还为应用程序提供了通过编程来进行自定义和扩展的能力. 允许可扩展性意味着 ...

  5. hdu5076

    好题,首先观察可得w[i][j]选择只有可能两种,一种比阀值大,一种比阀值小 比阀值大就一定选满足条件最大的w,比阀值小同样一定选满足条件最大的w 那么一个最小割模型就呼之欲出了,注意w可能是负数那么 ...

  6. weblogic漏洞总结 复现(未完)

    复现方式 Docker复现 WEBlogic爆出了很多漏洞 先了解一下现在主流的版本 Weblogic 10.3.6.0 Weblogic 12.1.3.0 Weblogic 12.2.1.1 Web ...

  7. 《深入理解Java虚拟机》内存分配策略

    上节学习回顾 1.判断对象存活算法:引用计数法和可行性分析算法 2.垃圾收集算法:标记-清除算法.复制算法.标记-整理算法 3.垃圾收集器: Serial:新生代收集器,采用复制算法,单线程. Par ...

  8. Java的内存分配

    java内存分配 A:栈 存储局部变量 B:堆 存储所有new出来的 C:方法区(方法区的内存中) 类加载时 方法信息保存在一块称为方法区的内存中, 并不随你创建对象而随对象保存于堆中; D:本地方法 ...

  9. JVM内存分配策略

    在 JVM内存垃圾回收方法 中,我们已经详细讨论了内存回收,但是,我们程序中生成的对象是如何进行分配的呢?以下所述针对的是HotSpot虚拟机. 1.Java堆结构 以HotSpot为例,如下图: H ...

随机推荐

  1. iOS如何让主界面不显示NavigationBar

    这个问题曾经困扰过我.现在我给出正解.- (void)viewWillAppear:(BOOL)animated {    [super viewWillAppear:animated]; [self ...

  2. 通过virt-manager 利用NFS创建、迁移虚拟机2

    前面一篇文章介绍了利用NFS创建虚拟机的过程,本文就介绍下如何利用NFS对虚拟机进行动态迁移. 动态迁移就是把一台虚拟机在不停止其运行的情况下迁移到另一台物理机上.这看起来似乎不太可能,不过还好kvm ...

  3. windows通过ssh连接虚拟机中的ubuntu步骤

    linux端开启ssh服务 1.安装openssh-server包 sudo apt-get install openssh-server 2.启动ssh server sudo /etc/init. ...

  4. Java 语言中 Enum 类型的使用介绍【转载】

    简介:本文主要介绍了 Java 语言中枚举类型,以及如何定制 Enum 类型的定义,如何正确使用 Enum 类型. From:http://www.ibm.com/developerworks/cn/ ...

  5. R中遇到的部分问题

    在Rstdio使用的是3.5.1的64位R版本中遇到问题:The Perl script 'WriteXLS.pl' failed to run successfully. 首先使用 Sys.whic ...

  6. SpringMVC的映射器,适配器,控制器

    一.各司其职   (1)HandlerMapping映射器 根据客户端请求的url,找到处理本次请求的处理器,即将请求的路径和controller关联起来 (2)HandlerAdapter适配器 对 ...

  7. 模块讲解----time与date time(时间模块)

    time和datetime 在python中,通常有一下几种方式来表示时间:1.时间戳:2.格式化时间字符串:3.元祖(struct_time):其中元祖(struct_time分为九个元素) UTC ...

  8. centOS7下安装laravel + composer

    1.wget https://dl.laravel-china.org/composer.phar -O /usr/local/bin/composer chmod a+x /usr/local/bi ...

  9. Cloudflare发布全球最快的DNS

    宣布1.1.1.1:速度最快,隐私优先的消费者DNS服务   Cloudflare的使命是帮助建立更好的互联网.今天我们很高兴能够在推出1.1.1.1--互联网最快,首先保护隐私的消费者DNS服务的同 ...

  10. 使用stringstream格式化字符串

    stringstream所在头文件为<sstream> 一般有如下常用功能: 1.安全格式化字符串 stringstream常用来安全的格式化若干个字符串,数值到一个缓冲区, 而不用担心溢 ...