SlideShare a Scribd company logo
㈜EXEM 기술연구소 개발본부 MFJ-Daemon팀 장훈
jemalloc
Chunk:
Heap에 있는 여러 메모리 구역을 여러 개로 나눠 놓은 것
Page :
가상 메모리를 일정한 크기로 나눈 블록
jemalloc
2006년 Jason Evans가 FreeBSD를 위해서 개발한 malloc.
메모리 단편화를 최소화하고, 멀티프로세서/멀티스레드 환경에서 동시성을 제공한다.
jemalloc?
jemalloc
멀티프로세서/멀티스레드 환경에서 동시성을 제공
jemalloc
Arena Thread Cache
jemalloc
Arena :
메모리를 여러 개로 나눈 것, 기본적으로 16개(프로세서 x 4).
Thread 는 Arena를 Round-Robin 방식으로 선택.
Thread가 메모리를 할당하기 위해 Arena에 접근할 때 Lock을 사용.
jemalloc
Thread Cache:
작은 단위의 잦은 메모리할당의 경우, Arena를 참조하지 않고 바로 malloc할 수 있도록,
각 스레드에게 thread cache라는 영역을 준다.
jemalloc
메모리 단편화를 최소화
jemalloc
Internal Fragmentation :
내부단편화, 할당된 메모리 크기 보다 적게 사용될 때 발생
External Fragmentation :
외부 단편화, 다양한 크기의 메모리 할당하고 해제할 때 발생
jemalloc
Buddy Allocation Slab Allocation
jemalloc
Buddy Allocation:
외부 단편화를 해결하기 위한 알고리즘.
메모리의 크기를 절반씩 분할 하면서 가장 잘맞는 크기의 메모리를 찾는다.
jemalloc
1024K
사용 할 수 있는 가장 큰 블록을 하나 할당 받음
해당 블록을 요청 받은 크기에 따라 적당히 나눠서 사용
나눌 때는 ½로 나눔
jemalloc
1024K
512KRequest 100K 256KA 128K
1024K를 ½로 나누면서 요청받은 100K에 적합한 Block을 찾음
jemalloc
1024K
512KRequest 100K 256KA 128K
512KRequest 240K BA 128K
jemalloc
1024K
512KRequest 100K 256KA 128K
512KRequest 240K BA 128K
512KRequest 64K BA 128KC 64K
jemalloc
1024K
512KRequest 100K 256KA 128K
512KRequest 240K BA 128K
512KRequest 64K BA 128KC 64K
Request 256K BA 128KC 64K D 256K
jemalloc
1024K
512KRequest 100K 256KA 128K
512KRequest 240K BA 128K
512KRequest 64K BA 128KC 64K
Request 256K BA 128KC 64K D 256K
Release B 256KA 128KC 64K D 256K
B를 반환 한 후에, 자신의 나머지 반쪽(Buddy) 영역을 확인하고 비어있다면 합침
현재는 A와 C가 있기 때문에 합치지 않음
jemalloc
1024K
512KRequest 100K 256KA 128K
512KRequest 240K BA 128K
512KRequest 64K BA 128KC 64K
Request 256K BA 128KC 64K D 256K
Release B 256KA 128KC 64K D 256K
Release A 256K128K 128KC 64K D 256K
jemalloc
1024K
512KRequest 100K 256KA 128K
512KRequest 240K BA 128K
512KRequest 64K BA 128KC 64K
Request 256K BA 128KC 64K D 256K
Release B 256KA 128KC 64K D 256K
Release A 256K128K 128KC 64K D 256K
Request 75K 256KE 128KC 64K D 256K
jemalloc
1024K
512KRequest 100K 256KA 128K
512KRequest 240K BA 128K
512KRequest 64K BA 128KC 64K
Request 256K BA 128KC 64K D 256K
Release B 256KA 128KC 64K D 256K
Release A 256K128K 128KC 64K D 256K
Request 75K 256KE 128KC 64K D 256K
Release C 256KE 128K D 256K
jemalloc
1024K
512KRequest 100K 256KA 128K
512KRequest 240K BA 128K
512KRequest 64K BA 128KC 64K
Request 256K BA 128KC 64K D 256K
Release B 256KA 128KC 64K D 256K
Release A 256K128K 128KC 64K D 256K
Request 75K 256KE 128KC 64K D 256K
Release C 256KE 128K D 256K
512KRelease E D 256K
jemalloc
Slab Allocation :
내부 단편화를 최소화 하기 위해 고안.
- 자주 사용하는 자료구조는 할당과 해제가 빈번하므로 캐쉬
- 단편화를 막기 위해 리스트는 연속된 순서로 정리
- 해제하면 해제 리스트로 들어가기 때문에 단편화가 없음
- 바로 다음 할당 시 해제 리스트에서 깨내서 사용
jemalloc
Small: [2, 4, 8], [16, 32, 48, ..., 128], [129, 256, 320, ...., 512], [1KB, 2KB]
Large: [4 KB, 8KB, 12 KB, ..., 4072 KB]
Huge: [4 MB, 8MB, 12MB, ...]
Allocation Size Class Category
4KB pages and a 16 byte quantum 환경
64bit OS
jemalloc
huge 오브젝트(chunk-aligned)는 red-black tree에 할당해서 관리
log시간에 찾을 수 있음
red-blacktree: self-balancing binary search tree
Search : binary tree와 동일
Insert : Color Promotion + Rotation
Delete : Color Demotion + Rotation
Search / Insert / Delete 모두 O(logN) 보장
https://www.cs.usfca.edu/~galles/visualization/RedBlack.html
jemalloc
Small 혹은 Large 할당은 Buddy 알고리즘과 Slab 알고리즘을 이용해서 Chunk의 Page Run에 할당
jemalloc
Chunk의 Fullness State
50 > 25 > 75 > 0 > init
jemalloc
Multi-threadedbenchmarks
jemalloc
Single-threadedbenchmarks
jemalloc 세미나

More Related Content

PDF
Physical Memory Management.pdf
PDF
The Linux Block Layer - Built for Fast Storage
PPTX
Slab Allocator in Linux Kernel
PDF
Linux Kernel - Virtual File System
PDF
Linux Performance Analysis: New Tools and Old Secrets
PDF
Twitter의 snowflake 소개 및 활용
PDF
Windows Registered I/O (RIO) vs IOCP
PPT
Linux SD/MMC Driver Stack
Physical Memory Management.pdf
The Linux Block Layer - Built for Fast Storage
Slab Allocator in Linux Kernel
Linux Kernel - Virtual File System
Linux Performance Analysis: New Tools and Old Secrets
Twitter의 snowflake 소개 및 활용
Windows Registered I/O (RIO) vs IOCP
Linux SD/MMC Driver Stack

What's hot (20)

PDF
Ndc2014 시즌 2 : 멀티쓰레드 프로그래밍이 왜 이리 힘드나요? (Lock-free에서 Transactional Memory까지)
PDF
Memory Mapping Implementation (mmap) in Linux Kernel
PDF
Ceph Block Devices: A Deep Dive
PDF
Process Scheduler and Balancer in Linux Kernel
PDF
/proc/irq/<irq>/smp_affinity
PDF
malloc & vmalloc in Linux
PPTX
Query logging with proxysql
ODP
Monitoring IO performance with iostat and pt-diskstats
PDF
[232] 성능어디까지쥐어짜봤니 송태웅
PDF
Kernel Recipes 2017: Using Linux perf at Netflix
PDF
Page cache in Linux kernel
PDF
Decompressed vmlinux: linux kernel initialization from page table configurati...
PDF
spinlock.pdf
PDF
Reverse Mapping (rmap) in Linux Kernel
PDF
Qemu device prototyping
PDF
使ってみよう!JDK Flight Recorder
PPTX
Quic을 이용한 네트워크 성능 개선
PDF
LinuxCon 2015 Linux Kernel Networking Walkthrough
PDF
Trace memory leak with gdb (GDB로 메모리 누수 찾기)
PDF
Build a High Available NFS Cluster Based on CephFS - Shangzhong Zhu
Ndc2014 시즌 2 : 멀티쓰레드 프로그래밍이 왜 이리 힘드나요? (Lock-free에서 Transactional Memory까지)
Memory Mapping Implementation (mmap) in Linux Kernel
Ceph Block Devices: A Deep Dive
Process Scheduler and Balancer in Linux Kernel
/proc/irq/<irq>/smp_affinity
malloc & vmalloc in Linux
Query logging with proxysql
Monitoring IO performance with iostat and pt-diskstats
[232] 성능어디까지쥐어짜봤니 송태웅
Kernel Recipes 2017: Using Linux perf at Netflix
Page cache in Linux kernel
Decompressed vmlinux: linux kernel initialization from page table configurati...
spinlock.pdf
Reverse Mapping (rmap) in Linux Kernel
Qemu device prototyping
使ってみよう!JDK Flight Recorder
Quic을 이용한 네트워크 성능 개선
LinuxCon 2015 Linux Kernel Networking Walkthrough
Trace memory leak with gdb (GDB로 메모리 누수 찾기)
Build a High Available NFS Cluster Based on CephFS - Shangzhong Zhu
Ad

Similar to jemalloc 세미나 (9)

PDF
메모리 할당에 관한 기초
PDF
Memory & object pooling
PPTX
How2heap
PDF
Cache governance
PPTX
System+os study 6
 
PDF
B tree & index
PPTX
Scalable web architecture and distributed systems
 
PPTX
Scalable web architecture and distributed systems
PDF
Slipp 발표 자료 20151212
메모리 할당에 관한 기초
Memory & object pooling
How2heap
Cache governance
System+os study 6
 
B tree & index
Scalable web architecture and distributed systems
 
Scalable web architecture and distributed systems
Slipp 발표 자료 20151212
Ad

More from Jang Hoon (7)

PDF
Kubernetes forum Seoul 2019 Review
PPTX
Deview 2018 review
PDF
Aws Summit Seoul 2018
PDF
Netty 세미나
PDF
Deview 2017 review
PDF
AWS re:Invent 2017
PDF
배워봅시다 머신러닝 with TensorFlow
Kubernetes forum Seoul 2019 Review
Deview 2018 review
Aws Summit Seoul 2018
Netty 세미나
Deview 2017 review
AWS re:Invent 2017
배워봅시다 머신러닝 with TensorFlow

jemalloc 세미나