Adding an associated reference is memory expensive (was: Adding an observer is memory expensive)
Adding an associated reference is memory expensive (was: Adding an observer is memory expensive)
- Subject: Adding an associated reference is memory expensive (was: Adding an observer is memory expensive)
- From: Torsten Radtke <email@hidden>
- Date: Fri, 11 Sep 2009 12:13:58 +0200
Is this when the first observer is added to each of the 100k objects?
Yes, it happens for the first observer I add.
But I expressed the problem not good enough. Adding an observer is
realized by adding an associated reference (with
objc_addAssociatedObject(), new in Snow Leopard) to the observed
object. So the main problem is that adding a single associative
reference to an object results in an allocation of 2048 byte block. If
I do this to my 100.000 objects, about 200MB are allocated (in
comparison to about 3MB for the objects). These blocks seem to be
scanned memory, as I also noticed significant delays as the garbage
collector scanned and collected.
This amount of memory seems to be allocated on purpose (see
the ::reserve method on top of the stack trace shown below) but seems
way too high for the general case of adding a single associative
reference!
0 libauto.dylib
std::vector<__gnu_cxx::_Hashtable_node<std::pair<void* const, void*>
>*, Auto::AuxAllocator<__gnu_cxx::_Hashtable_node<std::pair<void*
const, void*> >*> >::reserve(unsigned long)
1 libauto.dylib Auto::Zone::set_associative_ref(void*, void*, void*)
2 AssociativeReferences -[Object setName:]
3 AssociativeReferences main
4 AssociativeReferences start
The Object class is implemented as follows:
static char name_key;
@implementation Object
- (NSString*)name {
return objc_getAssociatedObject(self, &name_key);
}
- (void)setName:(NSString*)aName {
objc_setAssociatedObject(self, &name_key, aName,
OBJC_ASSOCIATION_COPY);
}
@end
Torsten
_______________________________________________
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