內(nèi)存管理 #2

Anonymous Memory Mappings

當(dāng)前,閾值為128 KB:小于或等于128 KB的分配來自堆,而更大的分配來自匿名內(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)沒有MAP_ANONYMOUS。相反,他們通過映射一個(gè)特殊的設(shè)備文件/dev/zero來實(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指定的值。

param

必須在使用任何內(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通過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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容