Re: Should VM shrink to original when releasing huge memory
Re: Should VM shrink to original when releasing huge memory
- Subject: Re: Should VM shrink to original when releasing huge memory
- From: Jens Alfke <email@hidden>
- Date: Tue, 13 Oct 2009 08:51:20 -0700
On Oct 13, 2009, at 2:14 AM, Nick Rogers wrote:
When my program runs the VM grows from 50MB to around 550MB.
So when I release the memory, should VM shrink to the original 50MB,
in this case it isn't so?
Virtual size is not usually a useful value for telling you "how much
memory am I using". It just says how much of your process's address
space is in use. For example, if you memory-map a 500MB file your
VSIZE will grow by 500MB even though none of the file's been loaded
into RAM yet. And if you link against AppKit your VM size grows by the
size of AppKit's code, even though that code's already being shared by
other processes.
The most useful seat-of-the-pants figure is RPRVT. This is the amount
of resident, private memory. That means (a) it's actually paged into
physical RAM, and (b) it's not shared with other processes.
If you want to know how much memory you've allocated with malloc /
operator new / [NSObject allocate], you can use the 'heap' command-
line tool, whose first line of output tells you how big your heap is
and how much of it is allocated.
On Oct 13, 2009, at 7:26 AM, Clark Cox wrote:
No, in most cases, it won't shrink. Most of the time, when you free
memory, it is returned to a pool within your application for use in
future allocations, but not returned to the OS (so it remains mapped
into your application's VM space).
It depends. Malloc grabs chunks of virtual address space (a few
hundred k at a time, I think) and carves those up for small malloc()
allocations. I believe those stay allocated even after all the blocks
are freed, for later re-use.
But if you call malloc to allocate a block larger than some threshold
size (32k?) then it directly calls vm_allocate to grab a block of
address space. And when large blocks like this are freed, they're
directly released from VM.
So yes, the typical pattern of lots of small allocations will grow
your address space over time, even after they're released. But if you
make big allocations, those will go away out of address space after
they're freed.
—Jens_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden