Memory
Memory
we all have know about types of memory since childhood this in this weeks chapter we will look into internals of that memory
lstopo output on my system
This image kinda looks scary when you dont know what is what, in this blog ill try to simplify things so that you might get an idea of what this image is about
Types of Memory
- Registers are the fastest kind of memory, they live with the CPU and match the CPU speed examples include rax, rbx, rcx, rdx, rsi, rdi etc
- cache is a kind of memory that is slower than Registers but faster than other kinds of memory
- Hard Disk and SSD are kinds of memory that can be used as a storage device as they retain memory for a longer time but the con is they are very slow compared to other kinds of memory
- Then comes RAM ( Random Access Memory ) This kind of memory is one of the most important kind of memory as it is cheaper than registers but has higher speed compared to SSD or a Hard Disk
Registers
- These are the fundamental kinds of memory that come with your processor and are the fastest, they can be programmed to perform specific operators
- There are various kinds of Registers (The following are examples specific to x86_64 bit arch, can be different in different architectures)
- rax The accumulator
- rbx The base register
- rcx The program counter
- rdx The data register
- rdi The destination index
- rsi The source index
- rsp The stack pointer
- rbp The stack base pointer
- All these registers have various purposed when it comes to general purpose computing and low level programming
Cache
- This kind of memory is not necessary in a computer but is generally present to match the speed of the CPU, this memory caches up previous results that the registers might need
- This kind of memory cannot be accessed by developers while programming a software ### Types of cache
- L1 Cache extremely fast, built into each CPU core
- L2 cache slower than L1 but much faster than RAM, is either shared between cores or built per core
- L3 cache slower than L1 and L2 but still very fast when compared to RAM, shared between all cores on the processor, generally used for inter core communication
Random Access memory
- RAM is a volatile memory used by programs during execution it is one of the most useful memory, the perfect tradeoff between cost and speed, We have been studying about this for a long time so ill jut get to the important parts
- Memory segments in RAM
- .text segment : stores machine instructions (executable code), generally read only to avoid overwriting
- .data segment : stores initialized global and static variables
- .bss segment : stores uninitialized global or static variables
- Heap : Used of Dynamic memory allocation
- it grows upwards ie for example from 0x0000 to 0xffff (assuming there is only one process)
- when the heap overwrites the stack its called heap overflow and it can be very dangerous
- example to create a heap variable
1
int *x = malloc(sizeof(int));
- NOTE it is important to free the memory after usage or it may cause memory leaks
- stack : stores function call frames, local variables
- only two operations push and pop
- grows downwards ie from 0xffff to 0x0000
- if stack overwrites buffer its called a stack overflow
1
int x = 4869;
This post is licensed under CC BY 4.0 by the author.