Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
mallocしただけでメモリが確保できるって本当ですか?
Search
MakKi
December 22, 2021
Programming
310
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
mallocしただけでメモリが確保できるって本当ですか?
AWS (t2.micro) デモバージョン
MakKi
December 22, 2021
More Decks by MakKi
See All by MakKi
テストだけじゃない!インプロセスDBで生まれるGoらしさ
makki_d
0
57
テストだけじゃない!インプロセスDBで生まれるGoらしさ
makki_d
0
72
SQLだけでマイグレーションしたい!
makki_d
0
1.4k
Recap: An Operating System in Go
makki_d
2
190
XSLTで作るBrainfuck処理系
makki_d
0
440
眼鏡と視力についての誤解を解く
makki_d
0
240
標準ライブラリの動向とイテレータのパフォーマンス
makki_d
3
790
range over funcのエラー処理
makki_d
1
1.9k
GoとテストとインプロセスDB
makki_d
3
710
Other Decks in Programming
See All in Programming
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
1
210
Vibes Containers 〜AIで変わるコンテナ設計と運用〜
tkikuc
3
520
DroidKaigi 2026 「個人開発という実験場: Android エンジニアが手にする4つの自由」
slashnephy
0
230
Seeing Through Serverless: Observability for AWS Lambda with ADOT and CloudWatch Application Signals
seike460
PRO
1
130
Building an Out-of-Order CPU
latte72
1
750
世界の中心で、AI(App Intents)をさけぶ ー App Intents中心設計の実践ガイド
touyou
0
330
ソフトウェアラスタライザ
fadis
1
800
AWS Step Functions 大規模並列の壁を越える / jaws-sonic-2026-niigata-step-functions
kasacchiful
PRO
1
430
{ Android | Kotlin } Gradle Plugin in 2026
ryunen344
1
300
Can LLMs Replicate 4 Years of Compose Migration? Exploring the boundaries of automation with 279 XML files from a real product
makun
0
130
typoなんかねぇよ
raspython3
0
680
市販E-Readerを乗っ取れ 〜Embedded Swiftで電子ペーパーガジェットを制御する〜
trickart
0
160
Featured
See All Featured
Everyday Curiosity
cassininazir
0
310
Building AI with AI
inesmontani
PRO
1
1.2k
A Soul's Torment
seathinner
7
3.6k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Practical Orchestrator
shlominoach
191
12k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
490
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
370
The Art of Programming - Codeland 2020
erikaheidi
57
14k
A better future with KSS
kneath
240
18k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
67k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.7k
We Are The Robots
honzajavorek
0
360
Transcript
mallocしただけで メモリが確保できるって 本当ですか? KLab株式会社 牧内大輔
自己紹介 • 牧内大輔 ◦ Twitter: @makki_d ◦ Github: makiuchi-d •
KLab株式会社 ◦ スマホ向けオンラインゲームの会社 ◦ エンジニアリングマネージャー • 特徴 ◦ Linux使い(Kubuntu + Compiz) ◦ 仕事:オンライン対戦のネットワーク基盤とか ◦ 趣味:バーコード/QRコードライブラリをGoに移植 ▪ そういえば昨晩blog記事かきました • QRコードを1ピクセルずつ消していく闇のゲームの攻略法 • https://makiuchi-d.github.io/2021/12/22/yami-qrcode.ja.html
今日はmallocの話をします
C言語触ったことある人
mallocが何か知っている人
mallocについて • C標準ライブラリの関数 • メモリの動的割り当て #include <stdlib.h> void *malloc(size_t size);
mallocの使い方 • こんなコードを見たことがあると思います void *p = malloc(1000); if(p == NULL){
// メモリ確保失敗の処理 ... }
このプログラムを 見てください
#define MALLOC_COUNT 20 #define PAGE_SIZE 4096 int main() { char
*ptrs[MALLOC_COUNT] = {NULL}; int count = 0; int i; printf("start. pid=%d", getpid()); getchar(); for(i=0; i<MALLOC_COUNT; ++i){ ptrs[i] = (char *) malloc(100 * 1024 * 1024); if(ptrs[i] == NULL){ printf("malloc returns NULL\n"); break; } count++; printf("malloc: p[%d] = %p", i, ptrs[i]); getchar(); } printf("total %d MB allocated", count * 100); getchar(); for(i=0; i<count; ++i){ int t; for(t=0; t<100*1024*1024; t += PAGE_SIZE){ ptrs[i][t] = 0; } printf("touch %d MB", (i+1)100); getchar(); } for(i=0; i<count; ++i){ free(ptrs[i]); } return 0; }
#define MALLOC_COUNT 20 #define PAGE_SIZE 4096 int main() { char
*ptrs[MALLOC_COUNT] = {NULL}; int count = 0; int i; printf("start. pid=%d", getpid()); getchar(); for(i=0; i<MALLOC_COUNT; ++i){ ptrs[i] = (char *) malloc(100 * 1024 * 1024); if(ptrs[i] == NULL){ printf("malloc returns NULL\n"); break; } count++; printf("malloc: p[%d] = %p", i, ptrs[i]); getchar(); } printf("total %d MB allocated", count * 100); getchar(); for(i=0; i<count; ++i){ int t; for(t=0; t<100*1024*1024; t += PAGE_SIZE){ ptrs[i][t] = 0; } printf("touch %d MB", (i+1)100); getchar(); } for(i=0; i<count; ++i){ free(ptrs[i]); } return 0; } 100MBずつ20回malloc 失敗したらbreak 確保できたらアドレスを表示 確保した合計を表示 確保した領域に書き込み 書き込んだ領域のサイズ表示 メモリ解放
問題:何MB確保できるでしょうか • デモ用マシンについて ◦ AWS EC2 t2.micro Ubuntu20.04 LTS ◦
物理メモリ 1GB ▪ total 968MiB、free 700MiBちょっと ◦ スワップ領域 0 1. 700 MB 2. 900 MB 3. 2000 MB
動かしてみます
結果 • 2000MBまでmalloc成功 • この段階ではメモリ使用率は増えていない • 確保したメモリに書き込んではじめてメモリ使用率増加 • 800MBくらいでOOM-Killerによって強制終了
なぜこうなったのか
仮想アドレス空間 • プロセスから見えるメモリアドレス空間 ◦ ひと塊の大きなメモリに見える ◦ 物理メモリの大きさとは無関係 • プロセス毎に独立 ◦
他プロセスのメモリにアクセスできない • 物理メモリとのマッピングはOSが管理 ◦ CPUの機能(MMU)を利用 物理メモリ 仮想アドレス
mallocしたとき • 仮想アドレス空間を予約 ◦ このアドレスが戻り値 • 物理メモリへのマッピングはまだしない ◦ メモリ使用量は増えない 物理メモリ
仮想アドレス ?
書き込みしたとき • このとき初めて物理メモリにマッピング ◦ メモリ使用量増加 最終的に物理メモリが足りなくなり メモリを開放するために プロセスが殺されました(OOM-Killer) 物理メモリ 仮想アドレス
まとめ • mallocしただけでは物理メモリは確保されない • 戻り値チェックだけではメモリ不足を防げない • 大量にメモリを使うプログラムを書くときは気をつけましょう • 裏には複雑な仕組みが隠れていてコンピュータっておもしろい!
参考文献とか • [試して理解] Linuxのしくみ ~実験と図解で学ぶOSとハードウェアの基礎知識 ◦ 竹内 覚、2018、技術評論社 ◦ ISBN:
978-4774196077 ※今回のmallocの挙動は、glibcのmallocにおいて heapではなくpageからメモリ確保する場合のものです。 ※mallocはもっと複雑なことをやっているので、 興味のある人は小崎先生の動画を見ましょう。 https://www.youtube.com/watch?v=0-vWT-t0UHg