Memory Management Algorithams
Posted: Sat Oct 15, 2005 11:45 pm
Well, in the current build of my OS, I use a bitmap that defines what 4kb pages are free or used. I've also noticed that if only the last little bit of memory would be free, it would go searching through all those bits in the bitmap (32-bits at a time, however) and would waste a lot of time, especially when large amounts of memory are being allocated all at once (like when you first open a 5mb program, for instance). So I have a pointer that tells me where the last free set of pages was found, which is filled in when we pass over a free page right in front of the one we just gave out (during an allocation), as usually this will be the case.
What this lacks for speed, it makes up for in the relatively small space it takes up - for a full 4GB, only 128KB.
Now, I've decided I'd look at stack-based systems, in which it will push the addresses of all the open pages onto one big stack, and simply pop to allocate a page, or push to free a page. For 4GB, it takes up a full 4mb - that's 1024th of your memory. But it is very fast. If you were to write it in ASM, the number of clock cycles (not including the amount to do call and ring change, if needed, as the above would have to also) would probably be under 10, which is very fast.
What it lacks in efficency, it makes up for in speed.
So I think to myself, on top of all this, the second is also very easy to implement. Amongst the commenting in my implementation, it only has 21 lines of actual code. And it is very easy to follow. On top of that, it has error checking galore. Without error checking, I could get away with maybe 10 lines of code - not bad.
My question is this - Why would one want to use the bitmap system? The stack system only uses 4mb, and that's only if you've got 4GB of RAM. It's really not that big an amount, if you consider. Only 1mb for every Gigabyte. For that small penalty, you get a huge boost in speed.
I was wondering also about one other thing:
Is there any median? What algorithams could be fast and with light memory footprint? Is it actually possible to get faster than a stack-based system?
I've decided, FYI, to go with the stack-based system, but the function calls perfectly match up (I did this on purpose, and the same goes for most other things - I use a strict naming method for my functions and variables to make sure they don't have trouble between files) so I could easily swap one for the other anytime.
What this lacks for speed, it makes up for in the relatively small space it takes up - for a full 4GB, only 128KB.
Now, I've decided I'd look at stack-based systems, in which it will push the addresses of all the open pages onto one big stack, and simply pop to allocate a page, or push to free a page. For 4GB, it takes up a full 4mb - that's 1024th of your memory. But it is very fast. If you were to write it in ASM, the number of clock cycles (not including the amount to do call and ring change, if needed, as the above would have to also) would probably be under 10, which is very fast.
What it lacks in efficency, it makes up for in speed.
So I think to myself, on top of all this, the second is also very easy to implement. Amongst the commenting in my implementation, it only has 21 lines of actual code. And it is very easy to follow. On top of that, it has error checking galore. Without error checking, I could get away with maybe 10 lines of code - not bad.
My question is this - Why would one want to use the bitmap system? The stack system only uses 4mb, and that's only if you've got 4GB of RAM. It's really not that big an amount, if you consider. Only 1mb for every Gigabyte. For that small penalty, you get a huge boost in speed.
I was wondering also about one other thing:
Is there any median? What algorithams could be fast and with light memory footprint? Is it actually possible to get faster than a stack-based system?
I've decided, FYI, to go with the stack-based system, but the function calls perfectly match up (I did this on purpose, and the same goes for most other things - I use a strict naming method for my functions and variables to make sure they don't have trouble between files) so I could easily swap one for the other anytime.