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. Exchange Port

    Get-POPSettings-110 Get-IMAPSettings-143 Exchange Network Port References Exchange Server 2000 http: ...

  2. cpuspeed和irqbalance服务器的两大性能杀手

    启用 irqbalance 服务,既可以提升性能,又可以降低能耗. irqbalance 用于优化中断分配,它会自动收集系统数据以分析使用模式,并依据系统负载状况将工作状态置于 Performance ...

  3. Spring Data 分页和排序 PagingAndSortingRepository的使用(九)

    继承PagingAndSortingRepository 我们可以看到,BlogRepository定义了这样一个方法:Page<Blog> findByDeletedFalse(Page ...

  4. Python(并发编程进程)

    并发编程 二.多进程 要让Python程序实现多进程(multiprocessing),我们先了解操作系统的相关知识. Unix/Linux操作系统提供了一个fork()系统调用,它非常特殊.普通的函 ...

  5. 前端 javascript 数据类型 字符串

    字符串是由字符组成的数组,但在JavaScript中字符串是不可变的:可以访问字符串任意位置的文本,但是JavaScript并未提供修改已知字符串内容的方法. obj.charAt(n) 返回字符串中 ...

  6. 16.遇到就jar mismatch! Fix your dependencies的问题

    这是因为两个项目的jar包(android-support-v4.jar)不一致. 解决方法是把2个jar都删除,然后各自加上最新的jar包 但是换了之后发现R文件编不出来,原因是minsdk的设置问 ...

  7. wordpress 主题开发

    https://yusi123.com/3205.html https://themeshaper.com/2012/10/22/the-themeshaper-wordpress-theme-tut ...

  8. 模块讲解----os

    os:跟操作系统相关的信息 os模块的增删改查 一.cd进入: windowd: os.chdir("D:/软件/pychar/data/s13") print('获取当前位置:' ...

  9. oracle 查看隐藏参数

    隐藏参数 (hidden parameters) ,由oracle内部使用,以 '_' 开头. 可以通过以下两种方式查看所有隐藏参数: SELECT   i.ksppinm name, i.ksppd ...

  10. 斩获新知——记一次reverse的实现过程

    最近学习C++,在实现reverse模板函数的时候,从一个小问题开始,在对这个问题的旁敲侧击当中带起了更多疑惑,顺藤摸瓜之后,尽管没有将诸多问题完美解答,但整个过程下来却也似有所获.最初的问题从使用C ...