Skip to Content
LINUX系統程式設計 第二版
book

LINUX系統程式設計 第二版

by Robert Love
December 2013
Intermediate to advanced
496 pages
8h 57m
Chinese
GoTop Information, Inc.
Content preview from LINUX系統程式設計 第二版
330
|
第九章
經由匿名映射分配記憶體有以下好處:
與碎裂無關。當程式不再需要一個匿名記憶體映射時,映射會被解除,而且記憶體
會被立即釋回系統。
匿名記憶體映射的尺寸是可調整的,具有可調整的存取權限,而且可以像一般映射
(見第 4 章)那樣收到警告訊息。
每個記憶體分配存在於獨立的記憶體映射中。不需要管理全域性的堆積。
相較於堆積,匿名記憶體映射還有兩項缺點:
每個記憶體映射的大小是系統頁面尺寸的整數倍。因此,如果一個記憶體分配的大
小不是頁面尺寸的整數倍,則會造成 slack space
5
的浪費。slack space 與小型的記憶
體分配比較有關,相較於記憶體分配的大小,這會浪費比較多的空間。
相較於從堆積把記憶體釋回系統(並未涉及任何核心的互動),建立一個新的記憶體
映射會導致更多的開銷。記憶體分配的規模越小,此現象越顯著。
為了讓優點有發揮的空間,同時避開缺點,
glibc
malloc()
實作會使用資料區段來滿足
小型的分配需求,以及使用匿名記憶體映射來滿足大型的分配需求。分配需求是大型或
小型,取決於一個可設定的門檻值(見第 9 章〈進階記憶體分配〉一節),而且不同的
glibc
版本可能會有所差異。當前的門檻值為 128 KB:記憶體分配小於或等於 128 KB
者來自堆積,然而大於 128 KB 的記憶體分配則來自匿名記憶體映射。
建立匿名記憶體映射
或許是因為你想要為特定的分配需求使用記憶體映射(而非堆積)、或許是因為你要撰
寫自己的記憶體分配系統,你可能會想要手動建立你自己的匿名記憶體映射—無論是哪 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

深入理解運算原理|從簡單的機器到無所不能的程式

深入理解運算原理|從簡單的機器到無所不能的程式

Tom Stuart
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
算法技术手册(原书第2 版)

算法技术手册(原书第2 版)

George T.Heineman, Gary Pollice, Stanley Selkow

Publisher Resources

ISBN: 9789862769812