经典文章: stack 和 heap
来源:互联网 发布:w3cschool php 手册 编辑:程序博客网 时间:2024/05/16 11:15
http://www.javatutorialhub.com/java-stack-heap.html
The JVM divided the memory into following sections.
1) Heap
2) Stack
3) Code
4) Static
This division of memory is required for its effective management.
The code section contains your bytecode.
The Stack section of memory contains methods, local variables and reference variables.
The Heap section contains Objects (may also contain reference variables).
The Static section contains Static data/methods.
Of all of the above 4 sections, you need to understand the allocation of memory in Stack & Heap the most, since it will affect your programming efforts
difference between instance variables and local variables
Instance variables are declared inside a class but not inside a method
class Student{
int num; // num is instance variable
public void showData{}
Local variables are declared inside a method including method arguments.
public void sum(int a){
int x = int a + 3;
// a , x are local variables
}
the following video demonstrates how memory is allocated in stack & heap.
Please be patient . Video will load in some time. If you still face issue viewing video clickhere
Points to Remember:
- When a method is called , a frame is created on the top of stack.
- Once a method has completed execution , flow of control returns to the calling method and its corresponding stack frame is flushed.
- Local variables are created in the stack
- Instance variables are created in the heap & are part of the object they belong to.
- Reference variables are created in the stack.
Point to Ponder:
What if Object has a reference as its instance variable?
public static void main(String args[]){
A parent = new A();
//more code
}
class A{
B child = new B();
int e;
//more code
}
class B{
int c;
int d;
//more code
}
In this case , the reference variable “child” will be created in heap ,which in turn will be pointing to its object, something like the diagram shown below.
- 经典文章: stack 和 heap
- [经典] 堆(heap)和栈(stack)有什么区别
- heap和stack
- heap和stack
- Heap和Stack
- stack和heap
- stack 和 heap区别
- JVM Heap和Stack
- Stack和Heap
- heap和stack
- heap和stack区别
- heap和stack区别
- stack和heap的区别
- Heap和Stack的区别
- heap 和Stack的区别
- heap 和Stack的区别
- 堆(heap)和栈(stack)
- Stack和Heap的区别
- sql2000取当前月的天数
- 把logo添加到PowerPoint幻灯片中
- ognl.OgnlException: target is null for setProperty(null, "x", [Ljava.lang.String;@1a4
- 判断SIM卡属于哪个移动运营商
- DataTable绑定到GridView时,RowDataBound事件
- 经典文章: stack 和 heap
- static的作用
- [转]Unity 3.4新增功能中文介绍和3.5展望
- XFire webservice 服务端开发(一)
- Eclipse打不开,出现了jvm terminated.Exit code=-1的错误
- 最大子数组和
- 单例模式(singleton)解析例子
- 设计模式之抽象工厂 代码示例
- xfire+spring整合webservice