---
# System prepended metadata

title: Memory Management

---

# TLDR
My research on virtual address space, address translating, proctection attribute, alignment, stack ,heap
## Virtual address space
The virtual address space for a process is the set of virtual memory addresses that it can use. The address space for each process is private and cannot be accessed by other processes unless it is shared.
A virtual address does not represent the actual physical address instead the system maintain a page table for each process which is an internal data structure used to translate virtual address into their corresponding physical address. Each time a thread is references an address the system translate the virtual address to physical address 

## Address translation
Address translation in operating system is the process of converting virtual addresses used by programs into physical address in the computer's physical memory. This is critical part of memory management in modern operating system.

### What is logical address?
Which is also know as a virtual address, is an address generate by the CPU during program execution. It's the address seen by the process and is relative to the program's address space. The process access memory using logical address which are translated by the operating system into physical address. An address is created by the CPU while a program is running is known as logical address. The CPU use the logical address as a reference to the actual memory location. All logical address created from a program's perspective are referred to as being in the logical address space.

### What is physical address?
A physical address is the actual address in the main memory where data is stored. Physical address is used by memory management unit is to translate logical address to physical address. The user must use the corresponding logical address to go to the logical address rather than directly accessing the physical address 

## System proctection in operating system
System proctection refers to mechanism implemented by the operating system to ensure the security and integrity of the system. System proctection involves various technique to prevented unauthorized access, misuse or modification of the operating system and its resources

There are several way an operating system can provide system proctection:
- Firewall
- Antivirus software
- Encryption
- Access control

## Alignment
Data alignment is the way data is arranged and accessed in computer memory. Data alignment and data structure padding are two different but are related to each other and together known as Data Structure Alignment.
Data alignment means putting the data in memory at an address equal to some multiple of word size. This increase the performance of the system due to the way CPU handles memory.
Data structure alignment it may be necessary to insert some extra bytes between the end of the last data structure and the next data structure as the data is placed in memory as multiples of fixed word size.

## Stack and heap in memory allocation
**Stack Allocation:** The allocation happens on contiguous blocks of memory. We call it a stack memory allocation because the allocation happens in the function call stack. The size of memory to be allocated is known to the compiler and whenever a function is called, its variables get memory allocated on the stack. And whenever the function call is over, the memory for the variables is de-allocated. This all happens using some predefined routines in the compiler. A programmer does not have to worry about memory allocation and de-allocation of stack variables. This kind of memory allocation is also known as Temporary memory allocation because as soon as the method finishes its execution all the data belonging to that method flushes out from the stack automatically. This means any value stored in the stack memory scheme is accessible as long as the method hasn’t completed its execution and is currently in a running state.

**Heap Allocation:** The memory is allocated during the execution of instructions written by programmers. Note that the name heap has nothing to do with the heap data structure. It is called a heap because it is a pile of memory space available to programmers to allocate and de-allocate. Every time when we made an object it always creates in Heap-space and the referencing information to these objects is always stored in Stack-memory. Heap memory allocation isn’t as safe as Stack memory allocation because the data stored in this space is accessible or visible to all threads. If a programmer does not handle this memory well, a memory leak can happen in the program.