๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป DEV/Computer Science

How is code allocated in memory?

by vodkassi 2023. 6. 5.
728x90

This post covers the subject "How code is allocated in the computer's memory."

 

Types of Memory

When we typically bring up the concept "memory", we usually refer to RAM (Random Access Memory). However the term is not constrained to RAM itself, but rather refers to a broader range which includes both RAM and Disk. 

 

# RAM

RAM is also known as a computer's primiary memory, and is in charge of running (or processing) programs and software when they are executed. RAM is located physically closer to the computer's microprocessor (or CPU), which enables its fast lookup. In general, lookups take 100 times slower when processed on disk - to read 1 MB of data, while it take 250 ms on RAM, it takes 30,000 ms on disk.

* lookups refer to the amount of time to find and retrive data from a certain block of memory.

 

In compensation for its agility, however, memory contains data short-term. Thus all the information it contains is reset once the computer shuts down. 

 

# Disk

Disk is commanly referred as HDD (Hard Disk Drive), or also the computer's storage or secondary memory. Disk stores data long-term, so even though a program runs on the primary memory for fast retrieval, it has to be saved on a disk for the data's preservation.

 

img src: https://www.techtarget.com/whatis/definition/memory

 

What happens when a program is executed

Once any program is executed on the computer, 

  1. The program is retrieved from the disk (secondary memory).
  2. Then, it is loaded on the RAM (primary memory).

But what happens beneath the process above is not as simple as it looks. 

 

There are 4 major divisions of the RAM in which code is preserved in. 

  • Code
  • Data
  • Stack
  • Heap

Each of these divisions are in charge of the following roles:

  • Code: stores executable code of the program
  • Data: stores global & static variables
  • Stack: stores functions & function calls, local variables, parameters, primitive data types & variables
  • Heap: stores referential data types (objects, arrays) and initializes new variables

* One thing to look out for is the fact that the "stack" division of the RAM does NOT refer to the data structure. 

 

Divisions code and data are quite self explanatory, where as more details are needed to understand stack and heap. Why are primitive data types store in stack, when referential types are left to be stored in heap? This is due to the different ways their size are structured.

 

Memory size of stack is determined at compile time, which means all of the data it contains must have fixed sizes. Thus it continains immutatable, or primitive data types. Heap size, on the otherhand, is determined at runtime, so it stores data that do not have a definite size fixed yet. One important feature about this fact is that, even though a referential data is stored on heap (ex. an array), the reference, or variable pointing to the data has fixed size, thus saved on stack. 

 

Stack and heap have  free memory space in between, so when one side runs out of memory, it takes up the free space. This situation is known as stack overflow and heap overflow. 

 

 

 

These are the basic structures of a computer's memory. 

More for deep dive to come later on.

 

 

Appendix) 

 

 

๋Œ“๊ธ€