Boost pool

34
boost::pool NEXT INSTITUTE 사사사

Transcript of Boost pool

Page 1: Boost pool

boost::pool

NEXT INSTITUTE 사현우

Page 2: Boost pool

pool 이 뭘까 ?

• 매우 빠르지만 사용에 한계가 있는 메모리 할당 방법 .

• 처음부터 메모리를 확보하고 , 그 안에서 적당한 메모리를 할당해줌 .

http://www.boost.org/doc/libs/1_50_0/libs/pool/doc/html/boost_pool/pool/introduc-tion.html

Page 3: Boost pool

왜 pool 을 써야할까 ?

• 풀을 사용하면 메모리를 더 통제할 수 있다 .

• 풀은 메모리 릭이 없다는 걸 보장해 준다 .

• 메모리 단편화 문제로부터 자유롭다 .

Page 4: Boost pool

언제 pool 을 써야할까 ?

• 한 시점에 작은 객체들을 할당한 다음 더 이상 그것들을 사용하지 않을 시점에 도달할 경우• 풀을 사용하면 소멸자를 실행하거나 단지 잊고 내버려둔다 (or just drop them off into

oblivion)

Page 5: Boost pool

언제 pool 을 써야할까 ?

• 작은 객체에 대하여 많은 할당과 해제가 이루어질 때• 많은 객체가 메모리에서 삭제될 때 • 메모리 관리에 대하여 더 효과적인 방법이 필요할 때

Page 6: Boost pool

boost::pool

• 일반적인 pool 인터페이스 .

• 빠른 메모리 할당 .

• 할당된 모든 chunk 에 대한 적절한 정렬을 보장 .

Page 7: Boost pool

boost::pool

Page 8: Boost pool

don’t take the time to free it!

• 함수가 종료되면• 할당자는 소멸되고• malloc 된 인자들은 해제된다 .

Page 9: Boost pool

boost::object_pool

• 객체 기반의 pool 인터페이스• 생성자와 소멸자를 자동으로 호출 시켜줌• 생성자의 인자가 4 개 이상이면 호출 불가 .

Page 10: Boost pool

boost::object_pool

Page 11: Boost pool

boost::object_pool

Page 12: Boost pool

boost::object_pool

Page 13: Boost pool

malloc & free vs construct & destroy

• malloc 과 free 는 메모리를 할당하고 해제만 해준다 .

• construct 와 destroy 는 메모리를 할당할 때 생성자를 호출하고 해제할 때 소멸자를 호출한다 .

Page 14: Boost pool

boost::singleton_pool

Page 15: Boost pool

boost::singleton_pool

• 전역적인 메모리 풀• boost::pool 과 같은 일을 하지만 싱글톤에 사용함• thread-safe

Page 16: Boost pool

boost::singleton_pool

• MyPoolTag 의 주소에 싱글톤 풀을 생성 .

Page 17: Boost pool

boost::singleton_pool

• boost::singleton_pool<MyPoolTag, sizeof(MyClass)>::malloc() 에서 싱글톤 동기화를 실행 .

Page 18: Boost pool

boost::singleton_pool

Page 19: Boost pool

release vs purge

• release_memory()–할당된 청크가 없는 메모리 블록을 릴리즈–해제되었을 때 true 를 반환

• purge_memory()–모든 메모리 블록을 릴리즈–해제되었을 때 true 를 반환

Page 20: Boost pool

boost::pool_allocator

• pool 기반의 메모리 할당자• boost::singleton_pool 를 기반으로 만들어짐• std::container 에 사용하기 위한 표준 Al-

locator 클래스를 지원

Page 21: Boost pool

boost::pool_allocator

• pool_allocator 로 할당한 메모리는 함수 종료 시 해제되지 않는다 .

• 명시적으로 boost::singleton_pool<boost::pool_allocator_tag, sizeof(int)>::release_memory() 를 호출해줘야 한다 .

Page 22: Boost pool

boost::pool_allocator

Page 23: Boost pool

boost::fast_pool_allocator

• NextSize : 생성될 때 첫 메모리 블록의 크기• MaxSize : 할당할 메모리 블록의 최대 크기

Page 24: Boost pool

boost::fast_pool_allocator

http://theboostcpplibraries.com/boost.pool

Page 25: Boost pool

pool_alloc vs fast_pool_alloc• pool_allocator–메모리에서 일정 크기만큼 영역 할당 후 그 영역에서 데이터를 삽입 , 삭제 .– std::vector 에 적합

• fast_pool_allocator–메모리 공간을 할당하지 않음–이전에 할당한 메모리 근처에 있는 주소를 할당함– std::list, map 등에 적합

Page 26: Boost pool

with thread

• singleton_pool 은 thread-safe 하다 .

• 여러 스레드에서 접근할 경우에 대비하여 내부적으로 동기화를 해줌 .

http://zepeh.tistory.com/306

Page 27: Boost pool

with thread

Page 28: Boost pool

with thread

Page 29: Boost pool

with thread

Page 30: Boost pool

with thread

Page 31: Boost pool

with thread

Page 32: Boost pool

with thread

• 동기화 때문에 벌어진 차이이므로 , 이 문제를 해결하기 위해선 사용하는 mutex 를 null_mutex 로 바꾸면 된다 .

http://zepeh.tistory.com/306

Page 33: Boost pool

with thread

• 동기화 때문에 벌어진 차이이므로 , 이 문제를 해결하기 위해선 사용하는 mutex 를 null_mutex 로 바꾸면 된다 .

Page 34: Boost pool

with thread