微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PostgreSQL的内存管理策略(1)

作为一个大的系统软件,如果避免内存泄露(memory leakage)是系统实现所面对的关键问题之一,Postgresql利用MemoryContext这个概念来解决这个问题,先来看看几个数据结构的定义:
注:在Postgresql代码中,一般struct会以名字加上后缀“Data”,而该struct的指针就是该名字。
typedef struct MemoryContextMethods
{
void *(*alloc) (MemoryContext context,Size size);
/* call this free_p in case someone #define's free() */
void (*free_p) (MemoryContext context,void *pointer);
void *(*realloc) (MemoryContext context,void *pointer,Size size);
void (*init) (MemoryContext context);
void (*reset) (MemoryContext context);
void (*delete) (MemoryContext context);
Size (*get_chunk_space) (MemoryContext context,void *pointer);
bool (*is_empty) (MemoryContext context);
void (*stats) (MemoryContext context);
#ifdef MEMORY_CONTEXT_CHECKING
void (*check) (MemoryContext context);
#endif
} MemoryContextMethods;
typedef struct MemoryContextData
{
NodeTag type; /* identifies exact kind of context */
MemoryContextMethods *methods; /* virtual function table */
MemoryContext parent; /* NULL if no parent (toplevel context) */
MemoryContext firstchild; /* head of linked list of children */
MemoryContext nextchild; /* next child of same parent */
char *name; /* context name (just for debugging) */
} MemoryContextData;
Postgresql对它的注释是“ A logical context in which memory allocations occur”,即它是一个内存分配的逻辑上下文,类似于C++中的虚拟类,对它有很多的实现方法,目前在Postgresql中,只有AllocSetContext一种。从MemoryContextData的结构我们很容易看到,它是把每个内存上下文组织成一棵树,每个内存上下文有一个名字和一系列内存空间操作方法,这一整套操作方法的申明是在结构MemoryContextMethods中给出。

原文地址:https://www.jb51.cc/postgresql/197496.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐