Stack and Heap_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Stack and Heap

Stack and Heap

 2013/7/10 3:35:22  2057  程序员俱乐部  我要评论(0)
  • 摘要:Stack:引用*StoredincomputerRAMjustliketheheap.*Variablescreatedonthestackwillgooutofscopeandautomaticallydeallocate.*Muchfastertoallocateincomparisontovariablesontheheap.*Implementedwithanactualstackdatastructure.*Storeslocaldata,returnaddresses
  • 标签:
Stack:
  引用 * Stored in computer RAM just like the heap.
   * Variables created on the stack will go out of scope and automatically deallocate.
   * Much faster to allocate in comparison to variables on the heap.
   * Implemented with an actual stack data structure.
   * Stores local data, return addresses, used for parameter passing
   * Can have a stack overflow when too much of the stack is used. (mostly from inifinite (or too much) recursion, very large allocations)
   * Data created on the stack can be used without pointers.
   * You would use the stack if you know exactly how much data you need to allocate before compile time and it is not too big.
   * Usually has a maximum size already determined when your program starts
Heap:
  引用 * Stored in computer RAM just like the stack.
   * Variables on the heap must be destroyed manually and never fall out of scope. The data is freed with delete, delete[] or free
   * Slower to allocate in comparison to variables on the stack.
   * Used on demand to allocate a block of data for use by the program.
   * Can have fragmentation when there are a lot of allocations and deallocations
   * In C++ data created on the heap will be pointed to by pointers and allocated with new or malloc
   * Can have allocation failures if too big of a buffer is requested to be allocated.
   * You would use the heap if you don't know exactly how much data you will need at runtime or if you need to allocate a lot of data.
   * Responsible for memory leaks
Stack
引用Often a function or method calls another function which in turn calls another function etc.
The execution of all those functions remains suspended until the very last function returns its value.

All the information required to resume the execution of these functions is stored on the stack.

In particular, local variables are stored on the stack.
Local variables are often stored for short amounts of time while a function/method block uses them to compute a task.
Once a function/method has completed its cycle, the space on the stack used by all local variables is freed.
This chain of suspended function calls is the <b>stack</b>, because elements in the stack (function calls) depend on each other.
The stack is important to consider in exception handling and thread executions.
Heap
引用The heap is simply the memory used by programs to store global variables.
Element of the heap (variables) have no dependencies with each other and can always be accessed randomly at any time.
All global variables are stored in heap memory.
All variables dynamically created by the program with "new()" or "malloc()" or similar commands are also stored on the heap.
In some programming languages, all instances of an object, including all the attributes of that instance, are stored on the heap.
In those programming languages, local variables of a function that have object type are implemented as creating the new object on the heap,
and storing a reference to that object in the local variable, which is on the stack.
When that function exits, the heap memory used by each local variable that has object is freed, and then all the stack used by that stack is freed.
Java中的Heap和Stack。
Java的堆是一个运行时数据区,类的对象从中分配空间。这些对象通过new建立,它们不需要程序代码来显式的释放。堆是由垃圾回收(garbage colloctor)来负责的,堆的优势是可以动态地分配内存大小,生存期也不必事先告诉编译器,因为它是运行时动态分配内存的,Java的垃圾收集器会自动回收这些不再使用的数据。但缺点点,由于要运行时动态分配内存,所欲存取速度较慢。
的优势在于,存取速度比堆块,仅次于寄存器,栈数据可以共享。但缺点是,存在栈中的数据大小与生存期必须是确定的,缺乏灵活性
栈中主要存放一些基本的数据类型对象句柄
whats the difference between .equals and ==?
Java的数据类型:
分类简单数据类型和复合数据类型
  • 简单数据类型:简单数据类型是不能再简化的、内置的数据类型,由编程语言定义,表示真实的数字、字符和整数。
  • 复合数据类型:由简单数据类型的组合形成的更大、更复杂的数据类型。例如类、接口和数字。

1、Java中针对Long型
不能使用==进行简单的比较。
==操作比较的是两个变量的值是否相等,对于引用变量表示的是两个变量在堆中存储的地址是否相同,即栈中的内容是否相同。
equals操作便是的两个变量是否是同一个对象的引用,即堆中的内容是否相同?
eg.
class="java" name="code"> 
        Long userId = 12222222222l;
        Long commentId = 12222222222l;
        System.out.println(userId==commentId);
        System.out.println(userId.equals(commentId));


参考资料:
http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap
http://wiki.answers.com/Q/What_is_difference_between_heap_memory_and_stack_memory
http://android.blog.51cto.com/268543/50100
http://blog.csdn.net/snowqoo/article/details/2455605
http://www.cnblogs.com/zhxhdean/archive/2011/03/25/1995431.html
  • 相关文章
发表评论
用户名: 匿名