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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
MakKi
February 21, 2019
Technology
860
4
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
mallocしただけでメモリが確保できるって本当ですか?
updated at 2019/Jun/29
MakKi
February 21, 2019
More Decks by MakKi
See All by MakKi
テストだけじゃない!インプロセスDBで生まれるGoらしさ
makki_d
0
58
テストだけじゃない!インプロセスDBで生まれるGoらしさ
makki_d
0
73
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 Technology
See All in Technology
消えない 動かない 効かない
yama3133
1
120
20260912_スクフェス三河
kgnkhkr
0
340
絵ではじめるKubernetesセキュリティ
aoi1
3
320
AI時代のAPI品質を支えるガードレール / API Guardrails for API quality in the AI era
yokawasa
1
250
AIに丸投げしないトイル削減 / Eliminating Toil Without Leaving It All to AI
kohbis
4
1.1k
Sigmaユーザーのための有用リソース一挙公開 & Sigmaで使えるMCP #sigma_ucj /useful-resources-for-sigma-computing-users-and-mcps-with-sigma
shinyaa31
0
210
[2026-09-11]SREは誰のもの?運用エンジニアが始める 「SRE領域への越境」とチームの進化の軌跡 〜Road to NEXT CRE
tosite
0
190
安心して変更できるWebフロントエンドの作り方
pirosikick
4
2k
The Agent Builder Loop from Daily Work to OSS
minorun365
PRO
3
160
AIネイティブプロダクトで顧客価値を最大化するプロダクトエンジニアとFDEの協働
righttouch
PRO
0
270
SREは、MCPとAutopilotをこう使え!
kazumax55
1
300
深夜のクラウド懺悔室 1:29:300 or 1:0:0
kazzpapa3
1
240
Featured
See All Featured
sira's awesome portfolio website redesign presentation
elsirapls
0
410
It's Worth the Effort
3n
188
29k
So, you think you're a good person
axbom
PRO
2
2.2k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
510
The Limits of Empathy - UXLibs8
cassininazir
1
660
For a Future-Friendly Web
brad_frost
183
10k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.3k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
510
Producing Creativity
orderedlist
PRO
348
41k
A Modern Web Designer's Workflow
chriscoyier
699
190k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
410
Building AI with AI
inesmontani
PRO
1
1.2k
Transcript
mallocしただけで メモリが確保できるって 本当ですか? KLab株式会社 牧内大輔
自己紹介 • 牧内大輔 ◦ Twitter: @makki_d ◦ Github: makiuchi-d •
KLab株式会社 ◦ エンジニアリングマネージャー ◦ スマホ向けオンラインゲームの会社 • 特徴 ◦ Linux使い(Kubuntu + Compiz) ◦ 仕事:オンライン対戦のネットワーク基盤とか ◦ 趣味:バーコード/QRコードライブラリをGoに移植
今日はmallocの話をします
C言語触ったことある人
mallocが何か知っている人
mallocについて • C標準ライブラリの関数 • メモリの動的割り当て #include <stdlib.h> void *malloc(size_t size);
mallocの使い方 • こんなコードを見たことがあると思います void *p = malloc(1000); if(p == NULL){
// メモリ確保失敗の処理 ... }
このプログラムを 見てください
#define TARGET_GB 20 #define PAGE_SIZE 4096 #define ONE_GB (1 *
1024 * 1024 * 1024) int main() { char *ptrs[TARGET_GB] = {NULL}; int total_gb = 0; int i; printf("start. pid=%d", getpid()); getchar(); for(i=0; i<TARGET_GB; ++i){ ptrs[i] = (char *) malloc(ONE_GB); if(ptrs[i] == NULL){ printf("malloc returns NULL\n"); break; } total_gb++; printf("malloc: p[%d] = %p", i, ptrs[i]); getchar(); } printf("total %d GB allocated", total_gb); getchar(); for(i=0; i<total_gb; ++i){ int t; for(t=0; t<ONE_GB; t += PAGE_SIZE){ ptrs[i][t] = 0; } printf("touch %d GB", i+1); getchar(); } for(i=0; i<total_gb; ++i){ free(ptrs[i]); } return 0; }
#define TARGET_GB 20 #define PAGE_SIZE 4096 #define ONE_GB (1 *
1024 * 1024 * 1024) int main() { char *ptrs[TARGET_GB] = {NULL}; int total_gb = 0; int i; printf("start. pid=%d", getpid()); getchar(); for(i=0; i<TARGET_GB; ++i){ ptrs[i] = (char *) malloc(ONE_GB); if(ptrs[i] == NULL){ printf("malloc returns NULL\n"); break; } total_gb++; printf("malloc: p[%d] = %p", i, ptrs[i]); getchar(); } printf("total %d GB allocated", total_gb); getchar(); for(i=0; i<total_gb; ++i){ int t; for(t=0; t<ONE_GB; t += PAGE_SIZE){ ptrs[i][t] = 0; } printf("touch %d GB", i+1); getchar(); } for(i=0; i<total_gb; ++i){ free(ptrs[i]); } return 0; } 1GBずつ20回malloc 失敗したらbreak 確保できたらアドレスを表示 確保した合計を表示 確保した領域に書き込み 書き込んだ領域のサイズ表示 メモリ解放
問題:何GB確保できるでしょうか • このPCについて ◦ Kubuntu 18.04 64bit (Linux version 4.15.0-54-generic)
◦ 物理メモリ 16GB ▪ ただし、VRAM領域もあるので実質約15.5GB ◦ スワップ領域 2GB 1. 15 GB 2. 17 GB 3. 20 GB
動かしてみます
結果 • 20GBまでmalloc成功 • この段階ではメモリ使用率は増えていない • 確保したメモリに書き込んではじめてメモリ使用率増加 • 16GBに到達する前に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
None