Anonymous Memory Mappings
當(dāng)前瞒窒,閾值為128 KB:小于或等于128 KB的分配來(lái)自堆完丽,而更大的分配來(lái)自匿名內(nèi)存映射向瓷。
Creating Anonymous Memory Mappings
#include <sys/mman.h>
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... */
系統(tǒng)調(diào)用munmap()釋放一個(gè)匿名映射,將分配的方法返回給內(nèi)核:
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系統(tǒng)(如BSD)沒(méi)有MAP_ANONYMOUS舰涌。相反猖任,他們通過(guò)映射一個(gè)特殊的設(shè)備文件/dev/zero來(lái)實(shí)現(xiàn)類似的解決方案。
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 <malloc.h>
int mallopt(int param, int value);
成功返回非0瓷耙, 失敗返回0朱躺,不設(shè)置errno。
對(duì)mallopt()的調(diào)用將param指定的內(nèi)存管理相關(guān)參數(shù)設(shè)置為由value指定的值搁痛。
必須在使用任何內(nèi)存分配接口前使用他
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 <malloc.h>
size_t malloc_usable_size(void *ptr);
對(duì)malloc_usable_size()的成功調(diào)用返回ptr所指向的內(nèi)存塊的實(shí)際分配大小长搀。
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);
成功調(diào)用malloc_trim()將盡可能地縮小數(shù)據(jù)段,減去保留的填充字節(jié)鸡典。
成功返回1源请。失敗返回0。
Debugging Memory Allocations
程序可以設(shè)置環(huán)境變量MALLOC_CHECK_彻况,以便在內(nèi)存子系統(tǒng)中啟用增強(qiáng)調(diào)試谁尸。
如果MALLOC_CHECK_設(shè)置為0,則內(nèi)存子系統(tǒng)將默默地忽略任何錯(cuò)誤纽甘。如果將其設(shè)置為1良蛮,則將信息消息打印到stderr。如果將其設(shè)置為2悍赢,則程序?qū)⒘⒓唇K止决瞳。 d通過(guò)ABORT()。因?yàn)镸ALLOC_CHECK_更改正在運(yùn)行的程序的行為左权,所以setuid程序忽略這個(gè)變量皮胡。
Obtaining Statistics
Linux提供了mallinfo()函數(shù),用于獲取與內(nèi)存分配系統(tǒng)相關(guān)的統(tǒng)計(jì)信息:
#include <malloc.h>
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 <malloc.h>
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