看nginx源代码ngx_debug_init函数,本意是打开FreeBSD内存调试的功能:
void
ngx_debug_init()
{
#if (NGX_DEBUG_MALLOC)
#if __FreeBSD_version >= 500014
_malloc_options = "J";
#else
malloc_options = "J";
#endif
ngx_freebsd_debug_malloc = 1;
#else
char *mo;
mo = getenv("MALLOC_OPTIONS");
if (mo && ngx_strchr(mo, 'J')) {
ngx_freebsd_debug_malloc = 1;
}
#endif
对'J'有些不解,上网查一下资料,查看环境变量MALLOC_OPTIONS:
TUNING
Once, when the first call is made to one of these memory allocation rou-
tines, various flags will be set or reset, which affect the workings of
this allocator implementation.
The ``name'' of the file referenced by the symbolic link named
/etc/malloc.conf, the value of the environment variable MALLOC_OPTIONS,
and the string pointed to by the global variable _malloc_options will be
interpreted, in that order, character by character as flags.
Most flags are single letters, where uppercase indicates that the behav-
ior is set, or on, and lowercase means that the behavior is not set, or
off.
A All warnings (except for the warning about unknown flags being
set) become fatal. The process will call abort(3) in these
cases.
H Use madvise(2) when pages within a chunk are no longer in use,
but the chunk as a whole cannot yet be deallocated. This is pri-
marily of use when swapping is a real possibility, due to the
high overhead of the madvise() system call.
J Each byte of new memory allocated by malloc(), realloc() will be
initialized to 0xa5. All memory returned by free(), realloc()
will be initialized to 0x5a. This is intended for debugging and
will impact performance negatively.
K Increase/decrease the virtual memory chunk size by a factor of
two. The default chunk size is 1 MB. This option can be speci-
fied multiple times.
N Increase/decrease the number of arenas by a factor of two. The
default number of arenas is four times the number of CPUs, or one
if there is a single CPU. This option can be specified multiple
times.
P Various statistics are printed at program exit via an atexit(3)
function. This has the potential to cause deadlock for a multi-
threaded process that exits while one or more threads are execut-
ing in the memory allocation functions. Therefore, this option
should only be used with care; it is primarily intended as a per-
formance tuning aid during application development.
Q Increase/decrease the size of the allocation quantum by a factor
of two. The default quantum is the minimum allowed by the archi-
tecture (typically 8 or 16 bytes). This option can be specified
multiple times.
S Increase/decrease the size of the maximum size class that is a
multiple of the quantum by a factor of two. Above this size,
power-of-two spacing is used for size classes. The default value
is 512 bytes. This option can be specified multiple times.
U Generate ``utrace'' entries for ktrace(1), for all operations.
Consult the source for details on this option.
V Attempting to allocate zero bytes will return a NULL pointer
instead of a valid pointer. (The default behavior is to make a
minimal allocation and return a pointer to it.) This option is
provided for System V compatibility. This option is incompatible
with the ``X'' option.
X Rather than return failure for any allocation function, display a
diagnostic message on stderr and cause the program to drop core
(using abort(3)). This option should be set at compile time by
including the following in the source code:
_malloc_options = "X";
Z Each byte of new memory allocated by malloc(), realloc() will be
initialized to 0. Note that this initialization only happens
once for each byte, so realloc() call do not zero memory that was
previously allocated. This is intended for debugging and will
impact performance negatively.
The ``J'' and ``Z'' options are intended for testing and debugging. An
application which changes its behavior when these options are used is
flawed.
哪位路过的大侠详细解释一下
本文解析了FreeBSD系统中内存调试选项'J'的作用,该选项用于初始化新分配的内存为特定值,并在释放内存时设置另一组特定值,有助于调试过程中发现内存使用错误。
3623

被折叠的 条评论
为什么被折叠?



