`

Stack and Heap

阅读更多
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.
 
        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
分享到:
评论

相关推荐

    Understanding and Using C Pointers 原版pdf by Reese

    The stack and heap are areas of memory used to support functions and dynamic memory allocation, respectively. Pointers are complex enough to deserve more in-depth treatment. This book provides that ...

    startup_armv7-m.7z

    Specifying stack and heap using the scatter file The ARM C library provides multiple implementations of the function __user_setup_stackheap(), and can select the correct one for you automatically ...

    Hands-On Data Structures and Algorithms with Rust.epub

    In this chapter, we think about stack and heap memory. Chapter 4, Lists, Lists, and More Lists, covers the first data structures: lists. Using several examples, this chapter goes into variations of ...

    Hands-On Penetration Testing on Windows - 2018 pdf 5分

    We'll work through core network hacking concepts and advanced Windows exploitation techniques, such as stack and heap overflows, precision heap spraying, and kernel exploitation, using coding ...

    Chapter 9. The Stack and the Heap

    We program in high-level languages for ... And perhaps we prefer solving problems strictly through an abstract quasi-mathematical algorithmic language without taking machine architecture into account.

    Bounds Checker6.01 for Delphi

    BoundsChecker automatically pinpoints static, stack and heap memory errors, and resource leaks. Unlike ordinary memory-checking tools, BoundsChecker validates the latest Windows APIs including ...

    The Run-time Heap and Stack

    在IT领域,尤其是在操作系统与编程语言的运行时环境中,“运行时堆和栈”(The Run-time Heap and Stack)是理解程序内存管理的核心概念。本文旨在深入解析这一主题,结合提供的部分文档内容,详细阐述堆和栈的区别...

    heap and stack

    深入理解堆(Heap)与栈(Stack):变量声明背后的故事 在计算机科学领域,堆(Heap)与栈(Stack)是两种常见的内存管理方式,它们在程序运行时扮演着至关重要的角色。本文将深入探讨堆与栈的概念、工作原理以及...

    IBM heapdump analyzer

    在一些平台上,在有些情况下,javacore也被称为javadump,它包含jvm和应用程序相关的在特定时刻的一些诊断信息,如操作系统,应用程序环境,线程,native stack本地堆,锁,和内存的信息。在生成heapdump文件的时候...

    英文原版-Problem Solving and Program Design in C 7th Edition

    In later chapters, students learn to implement fundamental data structures such as lists, stacks, queues, and trees in a language that fosters their understanding of stack- and heap-dynamic memory ...

    Z-Stack Home 1.2.0中预编译宏定义详细解释2

    - **Unique and Global Link Key Type Configuration**:这些宏定义允许用户配置独特的或全球性的链接密钥类型,从而增强了系统的灵活性和安全性。 - **Backwards Interoperability**:这一节介绍如何确保旧版本的...

    Selected.Topics.in.Cplusplus.15117

    C++ has stack memory and heap memory. You need to control where you want to put your objects. It has constructors and destructors. You need to know when and how they are called. Then it has multiple ...

    数据结构常用算法c++实现

    Stack Binary Heap Fibonacci Heap Priority Queue (list based) Bubble sort Selection sort Insertion sort Radix sort Quick sort Merge sort Heap sort Double linked list Skip list Self-organized linked-...

    JVM内核架构--JVM规范

    Whenever a method is invoked a new stack frame is added to the stack and corresponding frame is removed when its execution is completed. Native method stack: holds the state of each native method ...

    Problem.Solving.in.Data.Structures.and.Algorithms.Using.Cplusplus.epub

    We will be looking into a linked list, stack, queue, trees, heap, hash table and graphs. We will be looking into sorting, searching techniques. Then we will be looking into algorithm analysis, we ...

    Gray Hat Hacking, Second Edition

    Learn the basics of programming, stack operations, buffer overflow and heap vulnerabilities, and exploit development Test and exploit systems using Metasploit and other tools Break in to Windows and...

    Packt.Mastering.Csharp.and.NET.Programming

    - **The Stack and the Heap**: Explains the difference between stack-allocated and heap-allocated memory and their respective uses. - **Garbage Collection**: Discusses how the .NET runtime ...

    CC2530 document

    Upgrading To Z-Stack v2.0.0 Upgrading To Z-Stack v2.1.0 Upgrading To Z-Stack v2.4 ...Z-Stack Monitor and Test API Z-Stack API Z-Stack Compile Options 802.15.4 MAC API OSAL API HAL Driver API ...

    解决ads工程转到MDK(keil)可能出现的error

    以及“scatter-load file declares no heap or stack regions and __user_initial_stackheap is not defined”错误,这表明链接器未能找到`Main`符号或堆栈和堆的定义。解决此问题,通常在完成步骤1后,如果代码中...

Global site tag (gtag.js) - Google Analytics