r/AskComputerScience • u/timidpippin • 1d ago
Dumb question maybe, but how much memory gets used just assigning addresses to memory?
If every byte has to have its own unique address, how is that actually kept track of? Is it just generated on the fly, or is there some equal amount of memory set aside just for producing and labeling those unique addresses?
If the addresses that already hold data aren't each stored somewhere individually, how does it avoid overwriting memory that's already in use?
And how much does ASLR affect all of this?
6
Upvotes
7
u/VirtuteECanoscenza 1d ago
If you mean the physical address nothing stores it. It's the position of the byte.
If you mean virtual addresses, you don't need to store reach one because virtual addresses typically use contiguous blocks of physical memory so you can only map at a higher level this reducing the number of things to track.
If you wanted to have a virtual address space where each physical byte can have an arbitrary address then yes your page table would be bigger than the original storage.
6
u/Some-Dog5000 1d ago
Whatever memory your operating system is using, part of that is dedicated to memory management. It's not constant, but it doesn't use a lot of memory, for the same reason that the index of a book is much more compact than the actual book.
The page table is the most relevant data structure here. A special piece of hardware, called the memory management unit, assists the OS with managing the page table by doing the "producing and labeling of addresses" (more accurately, translating a memory address that a program requests into a physical address).
3
u/Recycled5000 1d ago edited 1d ago
Addresses are done with wiring (that decodes) whereas the actual storage is a memory cell. This wiring is built into the memory hardware, fixed, constant so consumes no “memory” itself.
The larger the physical address space, the more wiring is needed to give each cell its unique address.
Address wiring consumes a fair amount of real estate and hence time, which is partly why historically memory didn’t keep pace with the CPUs — the memory was growing instead.
We play some tricks to help. Typically the storage size is a cache line or so, and so the main memory doesn’t have to decode down to a byte, which saves a number of (low end) bits in the address decode. The cache then selects the bytes or words to give to the processor.
Also, the physical processors limit the number of address bits (like 48) even though the computer is 64 bit cpu.
3
u/Single_Duck_4660 1d ago
Page tables stores the addresses I think
1
u/stevevdvkpe 1d ago
The address of each memory location isn't stored in memory along with the contents of the location like a label. Memory is arranged as a fixed list of storage locations and an address is the position in that list. The 1000th item in the list doesn't have to be labeled with the number 1000; the memory hardware can just be asked "give me memory location 1000" and it finds that location by its position.
Programs have to keep track of the memory locations they are using to avoid overwriting memory locations that are already in use.
ASLR (Address Space Layout Randomization) is picking different base locations for portions of memory that don't need to reside at fixed locations, so that attacks that depend on overwriting a value at some known location can't easily predict where it will be. When a program is iniitalized it can be told "your data starts at this address and your stack starts at this address" and each program gets a different random starting address for its data and stack regions.
1
u/MasterGeekMX BSCS 1d ago
Thing is that you don't store each byte all the time.
Yes, each Byte get's it's own address, but in the end of the day they use the same amount of data. Take for example a street where 100 houses exist. While that is a lot, you can name every singe one with two numbers (assuming that the first house is number 0 and the last is 99).
Or a real world example, if you have 32 bits, you can count up to 4,294,967,296. That is the number of Bytes on 4 GB. This means that with the same 32 bits, you can address every single Byte in 4GB. No need to store a table with each, as naturally each are wired so the only way to access them is giving the address of each.
Now, when programs run, at the surface level there is no record of where each Byte lives directly, which is where I think your question lives. Instead programs simply "know" where they left variables, with instructions being constructed by the compiler to be done in such a way that all the code that access a given variable always works in some specific memory address.
1
u/nixiebunny 1d ago
The addresses of operands are stored in the program instructions. The machine language program is about twice as big as it would be if it had no addresses stored in the code. There are address registers used for accessing arrays and relative addressing modes that make address storage more efficient.
1
u/schorsch3000 1d ago
if you have 10 post'it's and line them op from position 0 to 9, how many post'it's do you need to have that numbering written down?
exactly, zero, its self ordering :-)
1
u/CFLAddLoader 1d ago
Each RAM chip is a grid of bytes split up by bank, row, column, and byte offset within the column. A memory controller takes several RAM sticks (themselves made of several RAM chips), and stiches their separate multidimensional grids into one address space (called the physical address space by the CPU). So the CPU can ask to read physical byte 25 and the memory controller figures out which RAM stick, RAM chip, bank, row, column, and byte offset to use. This uses maybe a few dozen bytes per RAM stick at most.
The operating system (and sometimes the firmware too) will keep track of what broad regions of the physical address space it is using for what purposes. Each table entry there will have maybe an 8 byte pointer, and 8 byte length, and an unknown (but small) number of bytes indicating what purpose that large region is used for. These tables are tiny compared to their size.
The operating system also likes to keep track of bytes in large groups called pages. The page size is CPU-dependent (and some CPUs let you pick between several options), but 2^12 bytes is a pretty common choice (2^14 and 2^16 are pretty popular too). The CPU will use a page table that uses 8 bytes per page, and the OS might use a few dozen bytes so it knows what each page is up to (or group of pages or whatever). But this is small compared to the page size of at least 2^12.
Heap allocators allow a large memory region to be divided into many smaller pieces. The application says it needs a memory block of a certain size, and the allocator finds an unused block of at least that size. When the application is done with a memory block, the allocator marks the block as free so it can be used again. It might subdivide free blocks that are too big, or combine free blocks that are right next to each other, or maintain a pool of free blocks of one particular size so if the application asks for something of that exact size it aready has a block and doesn't need to subdivide anything. The allocator might use a dozen or two bytes per block it keeps track of, maybe right at the front of the block, or maybe in a special metadata region.
So typically at each subdivision, a few dozen bytes are used to keep track of things. So it doesn't really make sense to keep track of individual bytes, because that wastes way more on metadata then you are using on data. But all of these levels will be used at the same time.
1
u/duane11583 1d ago
This is nit a run time thing you need to worry about
It is handled by the compiler and linker
1
u/geocar 1d ago
You mean how big are page tables?
With 4k pages and 64-bit pointers, you can have 512 pages mapped. Those page tables are in memory so every 2mb contiguous of addresses has 4kb of overhead.
Address space randomisation affects this by affecting how likely addresses will be contiguous when they are otherwise unrelated, making things slower and wasting more memory on page tables.
1
u/Discordbothost 4h ago
Separate allocation tracking from bounds checking. In C, writing a ninth byte into an 8-byte buffer is undefined behavior; it may corrupt nearby data without a page fault. The allocator knowing a block is occupied doesn't stop every bad write to it.
0
u/dontwantgarbage 1d ago
Addresses are just numbers. If you have a bunch of boxes, you can take a pen and write “1” on one box, “2” on another box, etc. you didn’t “get” the numbers from anywhere. You just made them up and assigned each one to a box.
Now, of course, you shouldn’t write “1” one two different boxes because if somebody asks you for box 1, you won’t know which one to give them. But that’s on you.
For physical addresses, the motherboard circuitry assigns ranges of numbers to memory chips. So if somebody asks for byte 12345, it asks (say) memory chip in the second socket, “I now you have 10240 bytes inside you. Please give me byte 2105.”
For virtual addresses, the operating system tells the CPU, “if somebody asks for virtual byte 12345, go get physical byte 22585.” (Which the motherboard circuitry converts to “hey, memory chip in the third socket, give me byte 2105.”) This virtual-to-physical mapping itself requires memory.
ASLR is a policy that operating systems have that they will change up what virtual addresses they use for various things. Today the high score is stored at 12345 but tomorrow it is stored at 22585. This makes it harder for bad guys to attack the high score table because it keeps moving around.
(Sometimes hardware gets messed up and accidentally writes “1” on two different boxes. This is known as an address conflict and the result is that when the circuitry asks chip 2 for byte 2105, both chip 2 and chip 3 reply and things go downhill from there.)
-1
u/LawElectrical2434 1d ago
Let's keep it simple, we have to differentiate between stack and heap.
Stack:
Like a growing stack of tasks... Imagine putting a paper with instructions. One instructions tells you to take another paper of instructions. You take it and put it on top of this one. You now only read the top page of instructions, it might tell you to take another paper, then you move on to that. As you see, that is a load of instructions, growing as a stack. Those papers can also hold variables. We do not need to track where they are, since, they are on the paper and we only read the top paper, even though we can look at references from variables handed over from below. But then, the reference tells us where it is. Fun fact, but not important, stacks grow from higher addresses to lower addresses... So think stalactites, not stalagmites.
Heap:
That is a form of memory that is random access, means you can get to any point. It also survives the destruction of your instruction paper, meaning you worked if off. But for that, you need to track where it is. And you have a logic where to find the new gap. An allocator usually does this. A common strategy is halving the space. You ask it for 100 bytes, it looks through the memory and finds it still empty, so, it halves it, looks at the first halve and finds 100 bytes free, so it halves it again, until it doesn't, then it gives you the address of the last halved point. Has the advantage of finding a right-sized gap quickly and the downside of leaving holes. Always a trade off. You can actually exchange allocators in applications and see if they are sped up by it. There is no optimal allocator for all programs. And that one quite simply tracks the memory spent in a table. It needs to know if it is allocated. But luckily it doesn't need to track all the memory, otherwise, it would need as much space as memory. It tracks usually an address and an offset to that address, which is a range. From here to there, allocated as one package.
And that's pretty much it. An easy overview. The details can and have filled multiple PhD theses.