<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    敬的世界

    常用鏈接

    統(tǒng)計(jì)

    最新評(píng)論

    STACK & HEAP 從C/JAVA 系統(tǒng)三個(gè)角度看其區(qū)別 - TECH -摘錄

    來(lái)自:http://www.tkk7.com/cjren/archive/2007/12/25/56989.html

    Stack vs Heap Allocation
    How the memory of the computer is organized for a running program? When a program is loaded into memory, it is organized into three areas of memory, called segments: the text segment, stack segment, and heap segment. The text segment (sometimes also called the code segment) is where the compiled code of the program itself resides. This is the machine language representation of the program steps to be carried out, including all functions making up the program, both user defined and system.

    The remaining two areas of system memory is where storage may be allocated by the compiler for data storage. The stack is where memory is allocated for automatic variables within functions. A stack is a Last In First Out (LIFO) storage device where new storage is allocated and deallocated at only one ``end'', called the Top of the stack. This can be seen in Figure 14.13.?



    figure14.13.gif

    When a program begins executing in the function main(), space is allocated on the stack for all variables declared within main(), as seen in Figure 14.13(a). If main() calls a function, func1(), additional storage is allocated for the variables in func1() at the top of the stack as shown in Figure 14.13(b). Notice that the parameters passed by main() to func1() are also stored on the stack. If func1() were to call any additional functions, storage would be allocated at the new Top of stack as seen in the figure. When func1() returns, storage for its local variables is deallocated, and the Top of the stack returns to to position shown in Figure 14.13(c). If main() were to call another function, storage would be allocated for that function at the Top shown in the figure. As can be seen, the memory allocated in the stack area is used and reused during program execution. It should be clear that memory allocated in this area will contain garbage values left over from previous usage.

    The heap segment provides more stable storage of data for a program; memory allocated in the heap remains in existence for the duration of a program. Therefore, global variables (storage class external), and static variables are allocated on the heap. The memory allocated in the heap area, if initialized to zero at program start, remains zero until the program makes use of it. Thus, the heap area need not contain garbage.
    小結(jié):
    Stack: automatic variables within functions
    Heap: global variables (storage class external), and static variables
    ============================
    In java 情況如下
    (1)
    ?The stack is the program memory area, so all your primitive type variables and the memory adress of your objects are written on the stack. It is a fast access valuable memory area.
    The heap is where the VM keeps the objects, and it is a huge amount of memory. When you create an object, the VM puts the object in the HEAP and puts the adress of the object created on the STACK.
    (2)
    ?There are two kinds of memory used in Java. These are called stack memory and heap memory. Stack memory stores primitive types and the addresses of objects. The object values are stored in heap memory. An object reference on the stack is only an address that refers to the place in heap memory where that object is kept.
    ?It is useful to know that these two different kinds of memory exist in Java. Stack memory is the program's memory, and heap memory resides outside of the program.這好像有點(diǎn)跟C的不同(相反)。
    引入一點(diǎn)垃圾回收機(jī)制的知識(shí)
    ?When you need a new object, Java allocates the required memory. When you are done with an object, the memory is reclaimed for you automatically via Java's garbage collection facility.
    ?Garbage collection runs as a thread in the background, looking for objects that no longer have a usable reference. When it finds them, it destroys them and reclaims the memory.
    ?The implementation of garbage collection varies between Java Virtual Machines. They generally follow the same process, however. First, the garbage collector gets a snapshot of all running threads and all loaded classes. Then, all objects that are referred to by this thread set are marked as current. The process stops when all objects that it is possible to reach have been marked and the rest have been discarded.
    ?In order to help the Virtual Machine, it is a good idea to remove your references to unneeded objects. This is often done by simply setting your reference to null:
    ?Test t = new Test();
    ?t.someAction();
    ?// all done
    ?t = null;
    小結(jié):
    Stack: Primitive data types(primitive types), the addresses of objects(=references).
    Heap:? objects.

    ===============================================
    從系統(tǒng)的角度看 stack(棧)和heap(堆)
    Dynamic Data Structures: The Heap
    A typical personal computer or workstation today has somewhere between 16 and 64 megabytes of RAM installed. Using a technique called virtual memory, the system can swap pieces of memory on and off the machine's hard disk to create an illusion for the CPU that it has much more memory, for example 200 to 500 megabytes. While this illusion is complete as far as the CPU is concerned, it can sometimes slow things down tremendously from the user's perspective. Despite this drawback, virtual memory is an extremely useful technique for "increasing" the amount of RAM in a machine in an inexpensive way. Let's assume for the sake of this discussion that a typical computer has a total memory space of, for example, 50 megabytes (regardless of whether that memory is implemented in real RAM or in virtual memory).
    The operating system on the machine is in charge of the 50-megabyte memory space. The operating system uses the space in several different ways, as shown here:


    c-heap.gif


    The operating system and several applications, along with their global variables and stack spaces, all consume portions of memory. When a program completes execution, it releases its memory for reuse by other programs. Note that part of the memory space remains unused at any given time.

    This is, of course, an idealization, but the basic principles are correct. As you can see, memory holds the executable code for the different applications currently running on the machine, along with the executable code for the operating system itself. Each application has certain global variables associated with it. These variables also consume memory. Finally, each application uses an area of memory called the stack, which holds all local variables and parameters used by any function. The stack also remembers the order in which functions are called so that function returns occur correctly. Each time a function is called, its local variables and parameters are "pushed onto" the stack. When the function returns, these locals and parameters are "popped." Because of this, the size of a program's stack fluctuates constantly as the program is running, but it has some maximum size.

    As a program finishes execution, the operating system unloads it, its globals and its stack space from memory. A new program can make use of that space at a later time. In this way, the memory in a computer system is constantly "recycled" and reused by programs as they execute and complete.

    In general, perhaps 50 percent of the computer's total memory space might be unused at any given moment. The operating system owns and manages the unused memory, and it is collectively known as the heap. The heap is extremely important because it is available for use by applications during execution using the C functions malloc (memory allocate) and free. The heap allows programs to allocate memory exactly when they need it during the execution of a program, rather than pre-allocating it with a specifically-sized array declaration.

    posted on 2008-11-28 17:53 picture talk 閱讀(1331) 評(píng)論(5)  編輯  收藏

    評(píng)論

    # re: STACK & HEAP 從C/JAVA 系統(tǒng)三個(gè)角度看其區(qū)別 - TECH -摘錄 2009-04-29 20:53 1111

    fuck off  回復(fù)  更多評(píng)論   

    # re: STACK & HEAP 從C/JAVA 系統(tǒng)三個(gè)角度看其區(qū)別 - TECH -摘錄 2009-04-29 20:53 1111

    fuck off sun of pitch  回復(fù)  更多評(píng)論   

    # re: STACK & HEAP 從C/JAVA 系統(tǒng)三個(gè)角度看其區(qū)別 - TECH -摘錄 2009-04-29 20:53 1111

    mesela yani:)  回復(fù)  更多評(píng)論   

    # re: STACK & HEAP 從C/JAVA 系統(tǒng)三個(gè)角度看其區(qū)別 - TECH -摘錄[未登錄](méi) 2009-04-30 19:59 picture talk

    fuck you, bitch, what's the fucking matter with you ~@1111
      回復(fù)  更多評(píng)論   

    # re: STACK & HEAP 從C/JAVA 系統(tǒng)三個(gè)角度看其區(qū)別 - TECH -摘錄 2009-10-08 23:40 yanna

    good artical  回復(fù)  更多評(píng)論   


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 又粗又大又长又爽免费视频| 精品一区二区三区无码免费视频 | 无码人妻一区二区三区免费n鬼沢 无码人妻一区二区三区免费看 | 亚洲ⅴ国产v天堂a无码二区| 一本久久A久久免费精品不卡 | 无码视频免费一区二三区| 久久亚洲AV无码精品色午夜麻豆| 国产精品99精品久久免费| 久久综合九九亚洲一区| 亚洲一级在线观看| 四虎成年永久免费网站| 免费吃奶摸下激烈视频| 免费无码国产在线观国内自拍中文字幕| 毛片无码免费无码播放| 最近最新中文字幕完整版免费高清 | GOGOGO高清免费看韩国| 国产亚洲精品福利在线无卡一| 久久高潮一级毛片免费| 亚洲日本va在线视频观看| 精品视频一区二区三区免费| 国产jizzjizz视频全部免费| 免费人成视频在线播放| 国产亚洲成归v人片在线观看| 中文字幕在线免费观看视频| 亚洲一区影音先锋色资源| 国产精品亚洲专区无码唯爱网| av无码国产在线看免费网站| 亚洲中文字幕久久久一区| 免费A级毛片在线播放不收费| 国产免费一级高清淫曰本片| 亚洲精品国产成人99久久| 韩日电影在线播放免费版| 国产麻豆免费观看91| 亚洲嫩草影院久久精品| 91成人免费观看网站| 妇女自拍偷自拍亚洲精品| 亚洲AV无码专区电影在线观看 | 亚洲国产精品一区二区三区久久| 日韩亚洲产在线观看| av免费不卡国产观看| 羞羞漫画登录页面免费 |