這將刪除頁面 "Memory Allocation Strategies - Part 1"
。請三思而後行。
Memory allocation seems to be one thing many people wrestle with. Many languages attempt to mechanically handle memory for you utilizing completely different strategies: rubbish collection (GC), automatic reference counting (ARC), resource acquisition is initialization (RAII), and neural entrainment audio possession semantics. However, attempting to abstract away memory allocation comes at a higher cost than most individuals understand. Most persons are taught to think about memory when it comes to the stack and the heap, the place the stack is mechanically grown for a process name, and the heap is a few magical factor that you can use to get Memory Wave that should stay longer than the stack. This dualistic method to memory is the mistaken way to give it some thought. It gives the programmer the mental model that the stack is a particular form of memory1 and that the heap is magical in nature. Fashionable working techniques virtualize memory on a per-process basis. Which means the addresses used within your program/process are particular to that program/process solely.
Resulting from operating programs virtualizing the memory area for us, this enables us to think about memory in a totally different way. Memory will not be longer this dualistic model of the stack and the heap but quite a monistic mannequin the place every thing is virtual memory. Some of that digital deal with house is reserved for procedure stack frames, a few of it's reserved for things required by the working system, and the rest we can use for whatever we want. This may sound just like unique dualistic model that I acknowledged beforehand, however, the biggest distinction is realizing that the memory is virtually-mapped and neural entrainment audio linear, and that you may split that linear memory space in sections. Lifetime Identified), this is the realm through which I will probably be overlaying the most in this series. More often than not, you do know the size of the allocation, or the upper bounds at the very least, and the lifetime of the allocation in question.
Lifetime Identified), this is the area in which it's possible you'll not know how much memory you require however you do know the way lengthy you can be utilizing it. The most common examples of this are loading a file into memory at runtime and populating a hash table of unknown size. It's possible you'll not know the amount of memory you will need a priori and as a result, you might have to "resize/realloc" the memory in order to suit all the data required. In C, malloc et al is a solution to this domain of problems. Lifetime Unknown), that is the realm through which it's possible you'll not know how long that memory must be round however you do know the way much memory is required. In this case, you could possibly say that the "ownership" of that memory across multiple programs is ailing-outlined. A standard resolution for this area of issues is reference counting or ownership semantics. Lifetime Unknown), that is the area by which you have got literally no thought how a lot memory you need nor how long it will be wanted for.
In apply, this is quite uncommon and you ought to try to keep away from these situations when possible. Nevertheless, the overall solution for this area of issues is rubbish collection3. Please word that in domain particular areas, these percentages will likely be utterly completely different. For instance, a web server that could be handling an unknown amount of requests could require a form of rubbish collection if the memory is proscribed or it could also be cheaper to only purchase extra memory. For the widespread class, the final method that I take is to consider memory lifetimes in terms of generations. An allocation era is a manner to organize memory lifetimes into a hierarchical structure4. Everlasting Allocation: Memory that isn't freed till the tip of the program. This memory is persistent throughout program lifetime. Transient Allocation: Memory that has a cycle-primarily based lifetime. This Memory Wave solely persists for the "cycle" and is freed at the top of this cycle. An example of a cycle may very well be a body within a graphical program (e.g. a sport) or an update loop.
這將刪除頁面 "Memory Allocation Strategies - Part 1"
。請三思而後行。