Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

malloc_size to get zone size

52 views
Skip to first unread message

ben

unread,
Jan 17, 2004, 7:45:38 PM1/17/04
to
i don't understand why i'm getting the same return value from
malloc_size from one tiny bit of code and one not so tiny bit of code.

tiny bit of code:

#import <Foundation/Foundation.h>
#include </usr/include/objc/malloc.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSLog(@"%u", malloc_size( NSDefaultMallocZone() ) ); // **

[pool release];
return 0;
}


and then i put the line marked ** on the end of a piece of code that
creates loads of objects - both print outs are 638. i'm trying to get
the size of the default allocated zone. what am i doing wrong?

thanks, ben.

Reinder Verlinde

unread,
Jan 18, 2004, 4:46:49 AM1/18/04
to
In article <180120040045387779%x@x.x>, ben <x@x.x> wrote:

> i don't understand why i'm getting the same return value from
> malloc_size from one tiny bit of code and one not so tiny bit of code.
>
> tiny bit of code:

>[...]


> NSLog(@"%u", malloc_size( NSDefaultMallocZone() ) ); // **

>[...]


> and then i put the line marked ** on the end of a piece of code that
> creates loads of objects - both print outs are 638. i'm trying to get
> the size of the default allocated zone. what am i doing wrong?

I think you do not understand what malloc_size does. 'man malloc_size'
states:

The malloc_size() function returns the size of the memory block that
backs the allocation pointed to by ptr. The memory block size is
always at least as large as the allocation it backs, and may be larger.

So, if you pass a pointer of identical size, it is no surprise that it
returns the same number.

Reinder

ben

unread,
Jan 18, 2004, 7:26:34 AM1/18/04
to
In article <reinder-954C7B...@reader11.wxs.nl>, Reinder
Verlinde <rei...@verlinde.invalid> wrote:

it's what the pointer points to, rather than the size of the pointer
malloc_size gives - that's what it sounds like to me from the manual
page.

yes, both cases are measuring the default memory zone, but two
different default memory zones. one memory zone from a tiny app that
hardly uses the memory zone at all, and one memory zone from a not so
tiny app that uses the default memory zone to put lots of objects in.
and both of the readings from both of those memory zones come out the
same - something's seems wrong there to me.

i'm expecting the default memory zone to be different sizes for
different apps depending on their memory usage. - am i incorrect in
thinking that?

thanks, ben.

Reinder Verlinde

unread,
Jan 18, 2004, 7:50:30 AM1/18/04
to

That's not true. The return value of 'NSDefaultMallocZone' is an object
describing a memory allocation zone. That object probably contains one
or more pointers to the actual memory in the zone, but those do not
affect the size of the object. This is similar to

char * p = "A string that I made up";
char * q = "";

sizeof( p) == sizeof( q) in this case.

If you are after the memory use of your application, you will have to
find another way; there do not seem to be accessors on NSZone that give
you that information.

Reinder

Michael Ash

unread,
Jan 18, 2004, 7:54:32 AM1/18/04
to
In article <180120041226340125%x@x.x>, ben <x@x.x> wrote:

> yes, both cases are measuring the default memory zone, but two
> different default memory zones. one memory zone from a tiny app that
> hardly uses the memory zone at all, and one memory zone from a not so
> tiny app that uses the default memory zone to put lots of objects in.
> and both of the readings from both of those memory zones come out the
> same - something's seems wrong there to me.

You're not measuring the memory zone itself. You're measuring a small
data structure which *describes* the memory zone. There's a layer of
indirection in there which malloc_size will not follow. You're basically
doing the equivalent of this:

id array = [NSMutableArray new];
printf("%d\n",malloc_size(array));
for(i = 0; i < 10000; i++)
[array addObject:[NSNull null]]; // just to pick an object
printf("%d\n",malloc_size(array));

The block of memory pointed to by 'array' will not change in size. The
storage for the array is in another block of memory, and 'array'
contains pointers to it. Likewise with a zone.

ben

unread,
Jan 18, 2004, 8:52:38 AM1/18/04
to

> You're not measuring the memory zone itself. You're measuring a small
> data structure which *describes* the memory zone. There's a layer of
> indirection in there which malloc_size will not follow.

&

> The return value of 'NSDefaultMallocZone' is an object
> describing a memory allocation zone. That object probably contains one
> or more pointers to the actual memory in the zone, but those do not
> affect the size of the object.

oh. i see. that wasn't clear to me, obviously, from what the manual
said.

thanks very much for explaining that to me.


here's the struct from /usr/include/objc/malloc.h :

typedef struct _malloc_zone_t {
/* Only zone implementors should depend on the layout of this
structure;
Regular callers should use the access functions below */
void *reserved1;
void *reserved2;
size_t (*size)(struct _malloc_zone_t *zone, const void *ptr); /*
returns the size of a block or 0 if not in this zone; must be fast,
especially for negative answers */
void *(*malloc)(struct _malloc_zone_t *zone, size_t size);
void *(*calloc)(struct _malloc_zone_t *zone, size_t num_items,
size_t size); /* same as malloc, but block returned is set to zero */
void *(*valloc)(struct _malloc_zone_t *zone, size_t size); /* same
as malloc, but block returned is set to zero and is guaranteed to be
page aligned */
void (*free)(struct _malloc_zone_t *zone, void *ptr);
void *(*realloc)(struct _malloc_zone_t *zone, void *ptr, size_t
size);
void (*destroy)(struct _malloc_zone_t *zone); /* zone is destroyed
and all memory reclaimed */
const char *zone_name;
void *reserved3;
void *reserved4;
struct malloc_introspection_t *introspect;
void *reserved5;
} malloc_zone_t;


this part of it:

size_t (*size)(struct _malloc_zone_t *zone, const void *ptr); /*
returns the size of a block or 0 if not in this zone; must be fast,
especially for negative answers */

is that dealing with the size of the actual zone, or is it again size
of a structure that isn't the zone?

thanks, ben

Reinder Verlinde

unread,
Jan 18, 2004, 10:17:59 AM1/18/04
to
In article <180120041352388594%x@x.x>, ben <x@x.x> wrote:

> this part of it:
>
> size_t (*size)(struct _malloc_zone_t *zone, const void *ptr); /*
> returns the size of a block or 0 if not in this zone; must be fast,
> especially for negative answers */
>
> is that dealing with the size of the actual zone, or is it again size
> of a structure that isn't the zone?
>

I would (and you probably should) guess that it returns the size of a
block in the zone. What else is that "const void *ptr" argument good for?

The "introspect" member looks more promising, though. It is a pointer to
a function that returns zone statistics:

malloc_statistics_t stats;
aZone->introspect->statistics( aZone, &stats);

could return the information you want (WARNING: I did not even try to
compile that) malloc_statistics_t is defined as:

typedef struct malloc_statistics_t {
unsigned blocks_in_use;
size_t size_in_use;
size_t max_size_in_use;/* high water mark of touched memory */
size_t size_allocated; /* reserved in memory */
} malloc_statistics_t;

Reinder

ben

unread,
Jan 19, 2004, 7:32:19 AM1/19/04
to
In article <reinder-2FF3EC...@reader08.wxs.nl>, Reinder
Verlinde <rei...@verlinde.invalid> wrote:

right, i'll look into that. thanks very much.

0 new messages