java的运行时数据区: 1.The pc Register:The Java virtual machine can support many threads of execution at once (§2.19). Each Java virtual machine thread has its own pc (program counter) register. 2.Java Virtual Machine Stacks:这就是我们常说的栈了。 3.Heap:堆。The Java virtual machine has a heap that is shared among all Java virtual machine threads. The heap is the runtime data area from which memory for all class instances and arrays is allocated. 3.Method Area 方法区,执行代码,有些类似于C程序里的text段。The method area is analogous to the storage area for compiled code of a conventional language or analogous to the "text" segment in a UNIX process. 4.Runtime Constant Pool 常量池(实际也分配在方法区里面) 5.Native Method Stacks 调用native方法用的。
2. 栈的优势是,存取速度比堆要快,仅次于直接位于CPU中的寄存器。但缺点是,存在栈中的数据大小与生存期必须是确定的,缺乏灵活性。另外,栈数据可以共享。堆的优势是可以动态地分配内存大小,生存期也不必事先告诉编译器,Java的垃圾收集器会自动收走这些不再使用的数据。但缺点是,由于要在运行时动态分配内存,存取速度较慢。更多的请看:http://blog.csdn.net/justinavril/archive/2008/09/11/2911676.aspx
当在一段代码块定义一个变量时,Java就在栈中为这个变量分配内存空间,当超过变量的作用域后,Java会自动释放掉为该变量所分配的内存空间,该内存空间可以立即被另作他用。
堆内存用来存放由new创建的对象和数组。
在堆中分配的内存,由Java虚拟机的自动垃圾回收器来管理。
在堆中产生了一个数组或对象后,还可以在栈中定义一个特殊的变量,让栈中这个变量的取值等于数组或对象在堆内存中的首地址,栈中的这个变量就成了数组或对象的引用变量。
引用变量就相当于是为数组或对象起的一个名称,以后就可以在程序中使用栈中的引用变量来访问堆中的数组或对象。
1、栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。
2、堆区(heap) — 一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收 。注意它与数据结构中的堆是两回事,分配方式倒是类似于链表,呵呵。
3、全局区(静态区)(static)—,全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域, 未初始化的全局变量和未初始化的静态变量在相邻的另一块区域。 - 程序结束后有系统释放
4、文字常量区 —常量字符串就是放在这里的。 程序结束后由系统释放
5、程序代码区—存放函数体的二进制代码。
昨天刚分析了C++里的这个问题。
栈(stack)与堆(heap) 4部分 分别用来存放不同的东西code segment (代码段): 自然用来放代码了data segment(数据段):用来存放 static的变量,和字符串常量栈(stack): 用来存放局部变量 什么形参呀!方法内部的变量!堆(heap): 用来存放动态new创建的对象和数组 4部分 各司其职,配合作战 这样才能完成一个程序的编译
1.The pc Register:The Java virtual machine can support many threads of execution at once (§2.19). Each Java virtual machine thread has its own pc (program counter) register.
2.Java Virtual Machine Stacks:这就是我们常说的栈了。
3.Heap:堆。The Java virtual machine has a heap that is shared among all Java virtual machine threads. The heap is the runtime data area from which memory for all class instances and arrays is allocated.
3.Method Area 方法区,执行代码,有些类似于C程序里的text段。The method area is analogous to the storage area for compiled code of a conventional language or analogous to the "text" segment in a UNIX process.
4.Runtime Constant Pool 常量池(实际也分配在方法区里面)
5.Native Method Stacks 调用native方法用的。