Byte[]分配在哪里?
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[]分配在哪里?的更多相关文章
- 20165312 2017-2018-2 《JAVA程序设计》第2周学习总结
20165312 2017-2018-2 <JAVA程序设计>第2周学习总结 一.对上一周学习的查漏补缺 1.上周在虚拟机中进行编译程序时出现错误,在上一周的博客中我有提到,当时还未找到解 ...
- CLR寄宿和AppDomain
一.CLR寄宿 .net framework在windows平台的顶部允许.者意味着.net framework必须用windows能理解的技术来构建.所有托管模块和程序集文件必须使用windows ...
- go-byte数组最大的长度
本来想打算用go来处理一个1G左右的txt文本的,但是在去读取的时候就报内存溢出了,提示数组已经无法在分配. 用的是:ioutil.ReadFile 方法来读取文本,它的返回值是一个[]byte 数组 ...
- 重温CLR(十六) CLR寄宿和AppDomain
寄宿(hosting)使任何应用程序都能利用clr的功能.特别要指出的是,它使现有应用程序至少能部分使用托管代码编写.另外,寄宿还为应用程序提供了通过编程来进行自定义和扩展的能力. 允许可扩展性意味着 ...
- hdu5076
好题,首先观察可得w[i][j]选择只有可能两种,一种比阀值大,一种比阀值小 比阀值大就一定选满足条件最大的w,比阀值小同样一定选满足条件最大的w 那么一个最小割模型就呼之欲出了,注意w可能是负数那么 ...
- weblogic漏洞总结 复现(未完)
复现方式 Docker复现 WEBlogic爆出了很多漏洞 先了解一下现在主流的版本 Weblogic 10.3.6.0 Weblogic 12.1.3.0 Weblogic 12.2.1.1 Web ...
- 《深入理解Java虚拟机》内存分配策略
上节学习回顾 1.判断对象存活算法:引用计数法和可行性分析算法 2.垃圾收集算法:标记-清除算法.复制算法.标记-整理算法 3.垃圾收集器: Serial:新生代收集器,采用复制算法,单线程. Par ...
- Java的内存分配
java内存分配 A:栈 存储局部变量 B:堆 存储所有new出来的 C:方法区(方法区的内存中) 类加载时 方法信息保存在一块称为方法区的内存中, 并不随你创建对象而随对象保存于堆中; D:本地方法 ...
- JVM内存分配策略
在 JVM内存垃圾回收方法 中,我们已经详细讨论了内存回收,但是,我们程序中生成的对象是如何进行分配的呢?以下所述针对的是HotSpot虚拟机. 1.Java堆结构 以HotSpot为例,如下图: H ...
随机推荐
- HTML代码大全
1.html的简介* 什么是html?- HyperText Markup Language:超文本标记语言,网页语言** 超文本:超出文本的范畴,使用html可以轻松实现这样操作** 标记:html ...
- Windows系统优化
1.关闭家庭组,因为这功能会导致硬盘和CPU处于高负荷状态: 关闭方法:windows + R 打开运行,输入services.msc回车,右侧窗格找到“HomeGroup Listener”和“Ho ...
- 【react路由】react 路由被自动加了个#
路由自动加#是由hashhistory造成: https://segmentfault.com/q/1010000012097148 单页面应用 前端跳转 or 服务器跳转: https://my.o ...
- 服务器初识、linux安装、linux初识
电脑硬件 电源 既然是人体的心脏,保障电源供应,就需要质量好的电源,生产环境中单个核心服务器最好是双电源AB线路. 一个接220V电路,一个可能接蓄电池UPS(不间断电源) cpu 常见品牌:Inte ...
- notepad插件:url变成可以点击的连接
- nginx 413 request entity too large解决办法
nginx 出现:413 request entity too large,一般是在上传图片的时候,上传的图片大小超过了服务器设置的最大上传大小,需要修改nginx和PHP的设置: (1)打开 /us ...
- Yii2 教程 - yii2-redis 扩展详解
该教程已被合并到<Yii2 权威指南中文版>中!Yiichina 教程地址为<yii2-redis 扩展详解>! 一.简介 yii2-redis 扩展为 Yii2 框架提供了 ...
- 深入理解Oracle调试事件:10046事件详解
10046事件是SQL_TRACE的扩展,被戏称为"吃了兴奋剂的SQL_TRACE" 有效的追踪级别: ① 0级:SQL_TRACE=FASL ...
- 使用stringstream格式化字符串
stringstream所在头文件为<sstream> 一般有如下常用功能: 1.安全格式化字符串 stringstream常用来安全的格式化若干个字符串,数值到一个缓冲区, 而不用担心溢 ...
- Java内存解析 程序的执行过程
Java内存解析 栈.堆.常量池等虽同属Java内存分配时操作的区域,但其适用范围和功用却大不相同.本文将深入Java核心,简单讲解Java内存分配方面的知识. 首先我们先来讲解一下内存中的各个区域. ...