threads and malloc/free on freebsd 8.0

Dan Nelson dnelson at
Fri Jun 11 20:45:21 UTC 2010

In the last episode (Jun 11), Vikash Badal said:
> > -----Original Message-----
> > From: owner-freebsd-questions at [mailto:owner-freebsd-
> > The fix is to remove your second call to malloc_usable_size(z)).  Then
> > neither version will crash.  Also, a useful habit to start is to
> > explicitly zero the pointer you just free'd, to prevent it from being
> > used accidentally later.
> Made this change:
> <CODE>-----------
>    LogMessage(DEBUG_0, "allocated %ld", malloc_usable_size(inst));
>    free(inst);
>    free(inst);
>     return 0;
> -----------</CODE>
> Still no seg fault.
> The reason im am doing this is that from top I can see the memory grow as
> I connect to this app.  When I disconnect, the memory used ( as displayed
> from top ) does not decrease.

For performance reasons, the malloc library may not immediately release free
memory back to the OS.  Also, depending on the position of the memory block
being freed, it may not be releasable back to the OS (if malloc used sbrk
for allocations, it can only free memory at the far end of the address
space, for example).  Setting the environment variable MALLOC_OPTIONS to
"dM10f" might help (disable use of sbrk(), enable use of mmap(), don't cache
any free pages), but you still may not see any change according to top.

	Dan Nelson
	dnelson at

More information about the freebsd-questions mailing list