How Computer Memory is Organized
Thông tin tài liệu:
Nội dung trích xuất từ tài liệu:
How Computer Memory is Organized How Computer Memory is OrganizedComputers use memory to hold programs being executed, and the data that theseprograms use. In order to understand the differences between value and reference types, itis helpful to understand how data is organized in memory.Operating systems and runtimes (such as the common language runtime) frequentlydivide the memory used for holding data into two separate chunks, each of which ismanaged in a distinct manner. These two chunks of memory are traditionally called thestack and the heap. The stack and the heap serve very different purposes: • When you call a method, the memory required for its parameters and its local variables is always acquired from the stack. When the method finishes (because it either returns or throws an exception), the memory acquired for the parameters and local variables is automatically released back to the stack and is available for reuse when another method is called. • When you create an object (an instance of a class) by using the new keyword and a constructor call, the memory required to build the object is always acquired from the heap. You have seen that the same object can be referenced from several places by using reference variables. When the last reference to an object disappears, the memory used by the object becomes available for reuse (although it might not be reclaimed immediately). Chapter 13 includes a more detailed discussion of how heap memory is reclaimed. NOTE All value types are created on the stack. All reference types (objects) are created on the heap.The names stack and heap come from the way in which the runtime organizes memory: • Stack memory is organized like a stack of boxes piled on top of each other. When a method is called, each parameter is put in a box which is placed on top of the stack. Each local variable is likewise assigned a box, and these are placed on top of the boxes already on the stack. When a method finishes, all its boxes are removed from the stack. • Heap memory is like a large pile of boxes strewn around a room rather than stacked neatly on top of each other. Each box has a label indicating whether it is in use or not. When a new object is created, the runtime searches for an empty box and allocates it to the object. The reference to the object is stored in a local variable on the stack. The run-time keeps track of the number of references to each box (remember that two variables can refer to the same object). When the last reference disappears, the runtime marks the box as not in use, and at some point in the future will empty the box and make it available for reuse.Using the Stack and the HeapNow lets examine what happens when the following Method is called:void Method(int param){ Circle c; c = new Circle(param); ...}Suppose the value passed into param is the value 42. A piece of memory (just enough foran int) is allocated from the stack, and initialized with the value 42. Inside the method,another piece of memory big enough to hold a reference is also allocated from the stack,but left uninitialized (this is for the Circle variable, c). Next, another piece of memory bigenough for a Circle object is allocated from the heap. This is what the new keyword does.The Circle constructor runs to convert this raw heap memory into a Circle object. Areference to this Circle object is stored in the variable c. The following graphic illustratesthe situation:At this point, you should note two things: 1. Although the object itself is stored on the heap, the reference to the object (variable c) is stored on the stack. 2. Heap memory is not infinite. If heap memory is exhausted, the new operator will throw an OutOfMemoryException and the object will not be created.NOTEThe Circle constructor could also throw an exception. If it does, the memory allocated tothe Circle object will be reclaimed and the value returned by the constructor will be a nullreference.When the function ends, the parameters and local variables go out of scope. The memoryacquired for c and the memory acquired for param is automatically released back to thestack. The runtime notes that the Circle object is no longer referenced, and at some pointin the future will arrange for its memory to be reclaimed by the heap (see Chapter 13).
Tìm kiếm theo từ khóa liên quan:
ngôn ngữ lập trình lập trình ngôn ngữ C# C# Memory is OrganizedGợi ý tài liệu liên quan:
-
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 276 0 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 267 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 266 0 0 -
Giáo trình Lập trình cơ bản với C++: Phần 1
77 trang 232 0 0 -
Bài giảng Một số hướng nghiên cứu và ứng dụng - Lê Thanh Hương
13 trang 226 0 0 -
Giáo án Tin học lớp 11 (Trọn bộ cả năm)
125 trang 218 1 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 208 0 0 -
Bài tập lập trình Windows dùng C# - Bài thực hành
13 trang 186 0 0 -
Giáo trình Lập trình C căn bản: Phần 1
64 trang 170 0 0 -
Bài giảng Nhập môn về lập trình - Chương 1: Giới thiệu về máy tính và lập trình
30 trang 168 0 0 -
Thiết kế mạch logic bằng Verilog - HDL
45 trang 164 0 0 -
Báo cáo thực tập: Quản lý nhân sự & tiền lương
52 trang 154 0 0 -
Giáo trình nhập môn lập trình - Phần 22
48 trang 139 0 0 -
Giáo trình Lập trình C căn bản - HanoiAptech Computer Education Center
136 trang 134 0 0 -
LUẬN VĂN: ỨNG DỤNG NGÔN NGỮ LẬP TRÌNH RÀNG BUỘC COMET VÀO BÀI TOÁN LẬP THỜI KHÓA BIỂU
43 trang 132 0 0 -
161 trang 130 1 0
-
Giáo trình lập trình hướng đối tượng - Lê Thị Mỹ Hạnh ĐH Đà Nẵng
165 trang 112 0 0 -
Bài giảng Phương pháp lập trình: Chương 9 - GV. Từ Thị Xuân Hiền
36 trang 112 0 0 -
Giáo trình Ngôn ngữ lập trình 2
50 trang 108 0 0 -
150 trang 104 0 0