内存管理 #2

Anonymous Memory Mappings

当前,阈值为128 KB:小于或等于128 KB的分配来自堆,而更大的分配来自匿名内存映射。

Creating Anonymous Memory Mappings

#include 
void *mmap(void *start,  size_t length, 
          int prot, int flags, int fd, off_t offset);
int munmap(void *start, size_t length);
void *p; 
p = mmap (NULL, /* do not care where */
                  512 * 1024, /* 512 KB */
                  PROT_READ | PROT_WRITE, /* read/write */
                  MAP_ANONYMOUS | MAP_PRIVATE, /*   anonymous, private */
                  −1, /* fd (ignored) */
                  0); /* offset (ignored) */
if (p == MAP_FAILED)
      perror ("mmap");
else
      /* 'p' points at 512 KB of anonymous memory... */

系统调用munmap()释放一个匿名映射,将分配的方法返回给内核:

int ret;
/* all done with 'p', so give back the 512 KB mapping */
ret = munmap (p, 512 * 1024);
if (ret)
    perror ("munmap");

Mapping /dev/zero

其他Unix系统(如BSD)没有MAP_ANONYMOUS。相反,他们通过映射一个特殊的设备文件/dev/zero来实现类似的解决方案。

void *p;
int fd;
/* open /dev/zero for reading and writing */
fd = open("/dev/zero", O_RDWR);
if(fd < 0){
    perror("open");
    return -1;
}
/* map [0,page size) of /dev/zero */
p = mmap (NULL, /* do not care where */
                  getpagesize (), /* map one page */
                  PROT_READ | PROT_WRITE, /* map read/write */
                  MAP_PRIVATE, /* private mapping */
                  fd, /* map /dev/zero */
                  0); /* no offset */
if (p == MAP_FAILED) {
    perror ("mmap");
    if (close (fd))
        perror ("close");
        return −1; 
}
/* close /dev/zero, no longer needed */
if (close (fd))
    perror ("close");
/* 'p' points at one page of memory, use it... */

Advanced Memory Allocation

#include 
int mallopt(int param, int value);

成功返回非0, 失败返回0,不设置errno。
对mallopt()的调用将param指定的内存管理相关参数设置为由value指定的值。

内存管理 #2_第1张图片
param

必须在使用任何内存分配接口前使用他

int ret;
/* use mmap() for all allocations over 64 KB */
ret = mallopt (M_MMAP_THRESHOLD, 64 * 1024);
if (!ret)
    fprintf (stderr, "mallopt failed!\n");

Fine-Tunnung with malloc_usable_size() and malloc_trim()

#include 
size_t malloc_usable_size(void *ptr);

对malloc_usable_size()的成功调用返回ptr所指向的内存块的实际分配大小。

size_t len = 21;
size_t size;
char *buf;
buf = malloc (len);
if (!buf) {
    perror ("malloc");
    return −1;
 }
size = malloc_usable_size (buf);
/* we can actually use 'size' bytes of 'buf' ... */
#include malloc.h>
int malloc_trim(size_t padding);

成功调用malloc_trim()将尽可能地缩小数据段,减去保留的填充字节。
成功返回1。失败返回0。

Debugging Memory Allocations

程序可以设置环境变量MALLOC_CHECK_,以便在内存子系统中启用增强调试。
如果MALLOC_CHECK_设置为0,则内存子系统将默默地忽略任何错误。如果将其设置为1,则将信息消息打印到stderr。如果将其设置为2,则程序将立即终止。 d通过ABORT()。因为MALLOC_CHECK_更改正在运行的程序的行为,所以setuid程序忽略这个变量。

Obtaining Statistics

Linux提供了mallinfo()函数,用于获取与内存分配系统相关的统计信息:

#include 
struct mallinfo mallinfo (void);
/* all sizes in bytes */
struct mallinfo {
int arena; /* size of data segment used by malloc */
int ordblks; /* number of free chunks */
int smblks; /* number of fast bins */
int hblks; /* number of anonymous mappings */
int hblkhd; /* size of anonymous mappings */
int usmblks; /* maximum total allocated size */
int fsmblks; /* size of available fast bins */
int uordblks; /* size of total allocated space */
int fordblks; /* size of available chunks */
int keepcost; /* size of trimmable space */
};
struct mallinfo m;
m = mallinfo ();
printf ("free chunks: %d\n", m.ordblks);

Linux also provides the malloc_stats() function, which prints memory-related
statistics to stderr:

#include 
void malloc_stats (void);

Invoking malloc_stats() in a memory-intensive program yields some big numbers:

Arena 0:
system bytes = 865939456
in use bytes = 851988200
Total (incl. mmap):
system bytes = 3216519168
in use bytes = 3202567912
max mmap regions = 65536
max mmap bytes = 2350579712

你可能感兴趣的:(内存管理 #2)