1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" Modified Sat Jul 24 19:00:59 1993 by Rik Faith (faith@cs.unc.edu)
24 .\" Clarification concerning realloc, iwj10@cus.cam.ac.uk (Ian Jackson), 950701
25 .\" Documented MALLOC_CHECK_, Wolfram Gloger (wmglo@dent.med.uni-muenchen.de)
26 .\" 2007-09-15 mtk: added notes on malloc()'s use of sbrk() and mmap().
28 .\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved.
29 .\" Translated 1998-08-12, NAKANO Takeo <nakano@apm.seikei.ac.jp>
30 .\" Modified 1998-12-18, NAKANO Takeo
31 .\" Modified 2000-08-23, NAKANO Takeo
32 .\" Modified 2001-05-19, Kentaro Shirakata <argrath@ub32.org>
33 .\" Modified 2001-12-13, Kentaro Shirakata <argrath@ub32.org>
34 .\" Modified 2005-03-15, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
35 .\" Modified 2007-10-12, Akihiro MOTOKI, LDP v2.66
37 .\" WORD: OOM killer ¥á¥â¥êÉÔ²ò·è´ï
39 .TH MALLOC 3 2009-01-13 "GNU" "Linux Programmer's Manual"
41 .\"O calloc, malloc, free, realloc \- Allocate and free dynamic memory
43 calloc, malloc, free, realloc \- ưŪ¤Ê¥á¥â¥ê¤Î³ä¤êÅö¤Æ¤È²òÊü¤ò¹Ô¤¦
47 .B #include <stdlib.h>
49 .BI "void *calloc(size_t " "nmemb" ", size_t " "size" );
51 .BI "void *malloc(size_t " "size" );
53 .BI "void free(void " "*ptr" );
55 .BI "void *realloc(void " "*ptr" ", size_t " "size" );
60 .\"O allocates memory for an array of
64 .\"O bytes each and returns a pointer to the allocated memory.
65 .\"O The memory is set to zero.
71 ¸Ä¤«¤é¤Ê¤ëÇÛÎó¤Ë¥á¥â¥ê¤ò³ä¤êÅö¤Æ¡¢
72 ³ä¤êÅö¤Æ¤é¤ì¤¿¥á¥â¥ê¤ËÂФ¹¤ë¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
73 ¥á¥â¥ê¤ÎÆâÍƤϿôÃÍ¥¼¥í (Á´¥Ó¥Ã¥È¤¬¥¼¥í¤Î¥Ð¥¤¥È) ¤Ë¥»¥Ã¥È¤µ¤ì¤ë¡£
80 .\"O returns either NULL,
81 .\"O .\" glibc does this:
82 .\"O or a unique pointer value that can later be successfully passed to
93 ¤Ë¸å¤ÇÅϤ·¤Æ¤âÌäÂê¤Îµ¯¤³¤é¤Ê¤¤°ì°Õ¤Ê¥Ý¥¤¥ó¥¿ÃͤòÊÖ¤¹¡£
98 .\"O bytes and returns a pointer to the allocated memory.
99 .\"O The memory is not cleared.
104 ³ä¤êÅö¤Æ¤é¤ì¤¿¥á¥â¥ê¤ËÂФ¹¤ë¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
105 ¥á¥â¥ê¤ÎÆâÍƤϥ¯¥ê¥¢¤µ¤ì¤Ê¤¤¡£
110 .\"O returns either NULL,
111 .\"O .\" glibc does this:
112 .\"O or a unique pointer value that can later be successfully passed to
121 ¤Ë¸å¤ÇÅϤ·¤Æ¤âÌäÂê¤Îµ¯¤³¤é¤Ê¤¤°ì°Õ¤Ê¥Ý¥¤¥ó¥¿ÃͤòÊÖ¤¹¡£
124 .\"O frees the memory space pointed to by
126 .\"O which must have been returned by a previous call to
131 .\"O Otherwise, or if
133 .\"O has already been called before, undefined behavior occurs.
136 .\"O is NULL, no operation is performed.
140 ¤¬»Ø¤¹¥á¥â¥ê¶õ´Ö¤ò²òÊü¤¹¤ë¡£¤³¤Î¥Ý¥¤¥ó¥¿¤Ï¡¢°ÊÁ°¤Ë¸Æ¤Ó½Ð¤µ¤ì¤¿
144 ¤Î¤¤¤º¤ì¤«¤¬ÊÖ¤·¤¿ÃͤǤʤ±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
145 ¤³¤ì°Ê³°¤Î¥Ý¥¤¥ó¥¿¤ò»ØÄꤷ¤¿¤ê¡¢¤¹¤Ç¤Ë
147 ¤¬¼Â¹Ô¤µ¤ì¤Æ¤¤¤¿¤ê¤·¤¿¾ì¹ç¤ÎÆ°ºî¤ÏÄêµÁ¤µ¤ì¤Æ¤¤¤Ê¤¤¡£
149 ¤¬ NULL ¤Î¾ì¹ç¤Ë¤Ï¡¢¤Ê¤ó¤ÎÆ°ºî¤â¹Ô¤ï¤ì¤Ê¤¤¡£
152 .\"O changes the size of the memory block pointed to by
160 ¤¬¼¨¤¹¥á¥â¥ê¥Ö¥í¥Ã¥¯¤Î¥µ¥¤¥º¤òÊѹ¹¤·¤Æ
163 .\"O The contents will be unchanged to the minimum of the old and new sizes;
164 .\"O newly allocated memory will be uninitialized.
165 ¿·µì¤Î¥µ¥¤¥º¤Î¤¦¤Á¡¢¾®¤µ¤¤¤Û¤¦¤Î¥Ö¥í¥Ã¥¯¤Ë´Þ¤Þ¤ì¤ëÆâÍƤÏÊѹ¹¤µ¤ì¤Ê¤¤¡£
166 ¿·¤·¤¯³ä¤êÅö¤Æ¤é¤ì¤¿¥á¥â¥ê¤ÎÆâÍƤϽé´ü²½¤µ¤ì¤Ê¤¤¡£
169 .\"O is NULL, then the call is equivalent to
170 .\"O .IR malloc(size) ,
171 .\"O for all values of
175 .\"O is equal to zero,
178 .\"O is not NULL, then the call is equivalent to
189 ¤¬ NULL ¤Ç¤Ê¤¤¾ì¹ç¤Ë¤Ï¡¢
194 .\"O is NULL, it must have been returned by an earlier call to
206 ¤Î¤¤¤º¤ì¤«¤¬ÊÖ¤·¤¿ÃͤǤʤ±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
207 .\"O If the area pointed to was moved, a
211 ¤¬»Ø¤¹Îΰ褬°ÜÆ°¤µ¤ì¤Æ¤¤¤¿¾ì¹ç¤Ï
214 .\"O .SH "RETURN VALUE"
220 .\"O return a pointer to the allocated memory, which is suitably
221 .\"O aligned for any kind of variable.
222 .\"O On error, these functions return NULL.
223 .\"O NULL may also be returned by a successful call to
228 .\"O or by a successful call to
238 ¤Ï¡¢³ä¤êÅö¤Æ¤é¤ì¤¿¥á¥â¥ê¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
239 ³ä¤êÅö¤Æ¤é¤ì¤¿¥á¥â¥ê¤Ï¡¢¤¢¤é¤æ¤ë¼ïÎà¤ÎÊÑ¿ô¤ËÂбþ¤Ç¤¤ë¤è¤¦¤Ë
240 ¥¢¥é¥¤¥ó¥á¥ó¥È¤µ¤ì¤Æ¤¤¤ë¡£
241 ¥¨¥é¡¼¤Î¾ì¹ç¡¢¤³¤ì¤é¤Î´Ø¿ô¤Ï NULL ¤òÊÖ¤¹¡£
251 ¤¬À®¸ù¤·¤¿¾ì¹ç¤Ë¤â NULL ¤¬ÊÖ¤µ¤ì¤ë¡£
254 .\"O returns no value.
259 .\"O returns a pointer to the newly allocated memory, which is suitably
260 .\"O aligned for any kind of variable and may be different from
262 .\"O or NULL if the request fails.
265 .\"O was equal to 0, either NULL or a pointer suitable to be passed to
270 .\"O fails the original block is left untouched; it is not freed or moved.
272 ¤Ï¿·¤¿¤Ë³ä¤êÅö¤Æ¤é¤ì¤¿¥á¥â¥ê¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
273 ¤³¤ì¤Ï¤¢¤é¤æ¤ë¼ïÎà¤ÎÊÑ¿ô¤ËÂбþ¤Ç¤¤ë¤è¤¦¤Ë¥¢¥é¥¤¥ó¥á¥ó¥È¤µ¤ì¤Æ¤ª¤ê¡¢
275 ¤È¤Ï°Û¤Ê¤ë¤³¤È¤â¤¢¤ë¡£
276 ³ä¤êÅö¤ÆÍ×µá¤Ë¼ºÇÔ¤·¤¿¾ì¹ç¤Ï NULL ¤¬Ê֤롣
278 ¤¬ 0 ¤Î¾ì¹ç¤Ë¤Ï¡¢NULL ¤â¤·¤¯¤Ï
280 ¤ËÅϤ¹¤³¤È¤¬¤Ç¤¤ë¥Ý¥¤¥ó¥¿¤¬Ê֤롣
282 ¤¬¼ºÇÔ¤·¤¿¾ì¹ç¤Ë¤Ï¡¢¸µ¤Î¥Ö¥í¥Ã¥¯¤ÏÊѹ¹¤µ¤ì¤Ê¤¤¡£
283 ¤Ä¤Þ¤ê¡¢²òÊü¤µ¤ì¤¿¤ê°ÜÆ°¤µ¤ì¤¿¤ê¤Ï¤·¤Ê¤¤¡£
284 .\"O .SH "CONFORMING TO"
291 .\"O allocates memory from the heap, and adjusts the size of the heap
292 .\"O as required, using
296 ¤Ï¡¢¥Ò¡¼¥×¤«¤é¥á¥â¥ê¤ò³ä¤êÅö¤Æ¡¢É¬Íפ˱þ¤¸¤Æ¥Ò¡¼¥×¤Î¥µ¥¤¥º¤ò
299 .\"O When allocating blocks of memory larger than
300 .\"O .B MMAP_THRESHOLD
301 .\"O bytes, the glibc
303 .\"O implementation allocates the memory as a private anonymous mapping using
305 .\"O .B MMAP_THRESHOLD
306 .\"O is 128 kB by default, but is adjustable using
307 .\"O .BR mallopt (3).
309 ¥Ð¥¤¥È¤è¤ê¤âÂ礤ʥá¥â¥ê¥Ö¥í¥Ã¥¯¤ò³ä¤êÅö¤Æ¤ë¾ì¹ç¡¢
314 ¤ò»È¤Ã¤Æ¥×¥é¥¤¥Ù¡¼¥È¤Ê̵̾¥Þ¥Ã¥Ô¥ó¥° (anonymous mapping) ¤È¤·¤Æ
321 .\" FIXME . there is no mallopt(3) man page yet.
322 .\"O Allocations performed using
324 .\"O are unaffected by the
326 .\"O resource limit (see
327 .\"O .BR getrlimit (2)).
329 ¤ò»È¤Ã¤Æ¹Ô¤ï¤ì¤¿¥á¥â¥ê³ä¤êÅö¤Æ¤Ï
331 ¥ê¥½¡¼¥¹¾å¸Â¤Î±Æ¶Á¤ò¼õ¤±¤Ê¤¤
335 .\"O The Unix98 standard requires
345 .\"O Glibc assumes that this is done
346 .\"O (and the glibc versions of these routines do this); if you
347 .\"O use a private malloc implementation that does not set
349 .\"O then certain library routines may fail without having
356 ¤Ï¼Â¹Ô¤Ë¼ºÇÔ¤·¤¿¤È¤¤Ë
360 ¤ËÀßÄꤹ¤ë¤³¤È¤Ë¤Ê¤Ã¤Æ¤¤¤ë¡£
361 Glibc ¤Ç¤Ï¤³¤ì¤¬¼é¤é¤ì¤Æ¤¤¤ë¤³¤È¤ò²¾Äꤷ¤Æ¤¤¤ë
362 (¤Þ¤¿¤³¤ì¤é¤Î¥ë¡¼¥Á¥ó¤Î glibc ¥Ð¡¼¥¸¥ç¥ó¤Ï¤³¤Î¤³¤È¤ò¼é¤Ã¤Æ¤¤¤ë)¡£
363 ¸Ä¿ÍŪ¤ËÊ̤Πmalloc ¤Î¼ÂÁõ¤ò»È¤Ã¤Æ¤¤¤Æ¡¢¤½¤Î malloc ¤¬
365 ¤òÀßÄꤷ¤Ê¤¤¾ì¹ç¤Ë¤Ï¡¢¼ºÇÔ¤·¤¿ºÝ¤Ë
367 ¤Ë¥¨¥é¡¼¤ÎÍýͳ¤òÀßÄꤷ¤Ê¤¤¥é¥¤¥Ö¥é¥ê¥ë¡¼¥Á¥ó¤¬¤¢¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
375 .\"O are almost always related to heap corruption, such as overflowing
376 .\"O an allocated chunk or freeing the same pointer twice.
382 ¤Û¤È¤ó¤É¤Î¾ì¹ç¤Ï¥Ò¡¼¥×¤ÎÇ˲õ (corruption) ¤¬¸¶°ø¤Ç¤¢¤ë¡£
383 Î㤨¤Ð¡¢³ä¤êÅö¤Æ¤é¤ì¤¿Îΰè¤ò¥ª¡¼¥Ð¡¼¥Õ¥í¡¼¤¹¤ë¡¢
384 Ʊ¤¸¥Ý¥¤¥ó¥¿¤ËÆóÅÙ free ¤¹¤ë¡¢¤Ê¤É¤¬¤³¤ì¤Ë¤¢¤¿¤ë¡£
386 .\"O Recent versions of Linux libc (later than 5.4.23) and glibc (2.x)
389 .\"O implementation which is tunable via environment variables.
391 .\"O .B MALLOC_CHECK_
392 .\"O is set, a special (less efficient) implementation is used which
393 .\"O is designed to be tolerant against simple errors, such as double
396 .\"O with the same argument, or overruns of a single byte (off-by-one
398 .\"O Not all such errors can be protected against, however, and
399 .\"O memory leaks can result.
400 ºÇ¶á¤Î¥Ð¡¼¥¸¥ç¥ó¤Î Linux libc (5.4.23 °Ê¹ß) ¤È glibc (2.x) ¤Ç¤Ï¡¢
402 ¤ÎÆ°ºî¤ò´Ä¶ÊÑ¿ô¤Ë¤è¤Ã¤ÆÀ©¸æ¤Ç¤¤ë¤è¤¦¤Ê¼ÂÁõ¤¬¤µ¤ì¤Æ¤¤¤ë¡£
404 ¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¡¢Æüì¤Ê¼ÂÁõ¤¬ÍѤ¤¤é¤ì¡¢
405 ñ½ã¤Ê¥¨¥é¡¼¤Ë¤ÏÂѤ¨¤ë¤³¤È¤¬¤Ç¤¤ë¤è¤¦¤Ë¤Ê¤ë (¸úΨ¤Ï°¤¯¤Ê¤ë)¡£Î㤨¤Ð¡¢
407 ¤òƱ¤¸°ú¤¿ô¤ÇÆóÅٸƤӽФ·¤Æ¤·¤Þ¤¦¡¢
408 1 ¥Ð¥¤¥È¤À¤±¹Ô¤¤¹¤®¤Æ¤·¤Þ¤¦ (off-by-one ¥Ð¥°) ¤Ê¤É¤¬¤³¤ì¤ËÅö¤¿¤ë¡£
409 ¤·¤«¤·¡¢¤³¤ì¤é¤Î¥¨¥é¡¼¤ÎÁ´¤Æ¤òËɤ°¤³¤È¤¬¤Ç¤¤ë¤ï¤±¤Ç¤Ï¤Ê¤¯¡¢
410 ¤½¤Î¾ì¹ç¤Ë¤Ï¥á¥â¥ê¥ê¡¼¥¯¤¬µ¯¤³¤Ã¤Æ¤·¤Þ¤¦¡£
412 .\"O .B MALLOC_CHECK_
413 .\"O is set to 0, any detected heap corruption is silently ignored;
414 .\"O if set to 1, a diagnostic is printed on \fIstderr\fP;
417 .\"O is called immediately;
418 .\"O if set to 3, a diagnostic message is printed on \fIstderr\fP
419 .\"O and the program is aborted.
421 .\"O .B MALLOC_CHECK_
422 .\"O value can be useful because otherwise
423 .\"O a crash may happen much later, and the true cause for the problem
424 .\"O is then very hard to track down.
426 ¤¬ 0 ¤Ë¥»¥Ã¥È¤µ¤ì¤Æ¤¤¤ë¤È¡¢¥Ò¡¼¥×¤ÎÇ˲õ¤òÌۤäÆ̵»ë¤¹¤ë¡£
427 1 ¤Ë¥»¥Ã¥È¤µ¤ì¤Æ¤¤¤ë¤È¡¢¿ÇÃÇ¥á¥Ã¥»¡¼¥¸¤¬É¸½à¥¨¥é¡¼½ÐÎϤËɽ¼¨¤µ¤ì¤ë¡£
428 2 ¤Ë¥»¥Ã¥È¤µ¤ì¤Æ¤¤¤ë¤È¡¢¤¿¤À¤Á¤Ë
431 3 ¤Ë¥»¥Ã¥È¤µ¤ì¤Æ¤¤¤ë¤È¡¢¿ÇÃÇ¥á¥Ã¥»¡¼¥¸¤¬É¸½à¥¨¥é¡¼½ÐÎϤËɽ¼¨¤µ¤ì¡¢
432 ¥×¥í¥°¥é¥à¤Ï¶¯À©½ªÎ» (abort) ¤µ¤ì¤ë¡£
434 ¤Ë 0 °Ê³°¤ÎÃͤò¥»¥Ã¥È¤·¤ÆÌò¤ËΩ¤Ä¾õ¶·¤È¤·¤Æ¤Ï¡¢
435 ¼ÂºÝ¤Î¥×¥í¥»¥¹¤Î¥¯¥é¥Ã¥·¥å¤¬¤º¤Ã¤È¸å¤Ëµ¯¤³¤ê¡¢
436 ËÜÅö¤Î¸¶°ø¤òõ¤·½Ð¤¹¤Î¤¬Èó¾ï¤Ëº¤Æñ¤Ê¾ì¹ç¤Ê¤É¤¬µó¤²¤é¤ì¤ë¤À¤í¤¦¡£
439 .\"O By default, Linux follows an optimistic memory allocation strategy.
440 ¥Ç¥Õ¥©¥ë¥È¤Ç¤Ï¡¢Linux ¤Ï³Ú´ÑŪ¥á¥â¥êÇÛÃÖÀïά¤òÍѤ¤¤Æ¤¤¤ë¡£
441 .\"O This means that when
443 .\"O returns non-NULL there is no guarantee that the memory really
445 .\"O This is a really bad bug.
446 .\"O In case it turns out that the system is out of memory,
447 .\"O one or more processes will be killed by the infamous OOM killer.
450 ¤¬ NULL ¤Ç¤Ê¤¤ÃͤòÊÖ¤·¤Æ¤â¡¢¤½¤Î¥á¥â¥ê¤¬¼ÂºÝ¤ËÍøÍѲÄǽ¤Ç¤¢¤ë¤³¤È¤¬
451 Êݾڤµ¤ì¤Ê¤¤¡£¤³¤ì¤ÏËÜÅö¤Ë¤Þ¤º¤¤¥Ð¥°¤Ç¤¢¤ë¡£
452 ¥·¥¹¥Æ¥à¤¬¥á¥â¥êÉÔ¾õÂ֤ˤʤ俤Ȥ¡¢°Ì¾¹â¤¤¥á¥â¥êÉÔ²ò·è´ï (OOM killer)
453 ¤Ë¤è¤Ã¤Æ°ì¤Ä¤Þ¤¿¤ÏÊ£¿ô¤Î¥×¥í¥»¥¹¤¬ºï½ü¤µ¤ì¤ë¡£
454 .\"O In case Linux is employed under circumstances where it would be
455 .\"O less desirable to suddenly lose some randomly picked processes,
456 .\"O and moreover the kernel version is sufficiently recent,
457 .\"O one can switch off this overcommitting behavior using a command like:
458 ÆÍÁ³¤¢¤ë¥×¥í¥»¥¹¤¬ºï½ü¤µ¤ì¤ë¤Î¤¬Ë¾¤Þ¤·¤¯¤Ê¤¤¾õ¶·¤Ç»ÈÍѤµ¤ì¤Æ¤¤¤Æ¡¢
459 ¤·¤«¤â¥«¡¼¥Í¥ë¤Î¥Ð¡¼¥¸¥ç¥ó¤¬½½Ê¬¤ËºÇ¶á¤Î¤â¤Î¤Ç¤¢¤ì¤Ð¡¢¤³¤Î¥á¥â¥ê¤ò
460 ³ä¤êÅö¤Æ²á¤®¤ëÆ°ºî (overcommitting behavior) ¤ò°Ê²¼¤Î¥³¥Þ¥ó¥É¤Ç
465 .RB "#" " echo 2 > /proc/sys/vm/overcommit_memory"
469 .\"O See also the kernel Documentation directory, files
470 .\"O .I vm/overcommit-accounting
472 .\"O .IR sysctl/vm.txt .
474 .I vm/overcommit-accounting
481 .BR posix_memalign (3)