Featured post
c++ - How to implement a memory heap -
wasn't sure how phrase title, question is:
i've heard of programmers allocating large section of contiguous memory @ start of program , dealing out necessary. in contrast going os every time memory needed. i've heard faster because avoid cost of asking os contiguous blocks of memory constantly.
i believe jvm this, maintaining own section of memory , allocating objects that.
my question is, how 1 implement this?
thanks, dragonwrenn
most c , c++ compilers provide heap memory-manager part of standard library, don't need @ in order avoid hitting os every request.
if want improve performance, there number of improved allocators around can link , go. e.g. hoard, wheaties mentioned in now-deleted answer (which quite -- wheaties, why'd delete it?).
if want write own heap manager learning exercise, here basic things needs do:
- request big block of memory os
- keep linked list of free blocks
- when allocation request comes in:
- search list block that's big enough requested size plus book-keeping variables stored alongside.
- split off big enough chunk of block current request, put rest in free list
- if no block big enough, go os , ask big chunk
- when deallocation request comes in
- read header find out size
- add newly freed block onto free list
- optionally, see if memory following listed on free list, , combine both adjacent blocks 1 bigger 1 (called coalescing heap)
- Get link
- X
- Other Apps
Comments
Post a Comment