From cefd7bacacb7ed089cecf5c2ea464a5fb82952ab Mon Sep 17 00:00:00 2001 From: rfm Date: Sat, 17 Jun 2006 17:20:22 +0000 Subject: [PATCH] Help avoid programs getting huge memory footprints when they don't need to. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@23077 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 3 ++ Source/NSAutoreleasePool.m | 68 ++++++++++++++------------------------ Source/NSRunLoop.m | 5 +++ 3 files changed, 32 insertions(+), 44 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9539b6547..9a4092107 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,9 @@ 2006-06-17 Richard Frith-Macdonald * Source/GSString.m: Fix memory leak. + * Source/NSAutoreleasePool.m: Optimise -emptyPool method. + * Source/NSRunLoop.m: Empty autorelease pool after firing each + timer/event so that huge memory footprints are less likely to occur. 2006-06-16 Richard Frith-Macdonald diff --git a/Source/NSAutoreleasePool.m b/Source/NSAutoreleasePool.m index 1f28bd1f3..dc615f651 100644 --- a/Source/NSAutoreleasePool.m +++ b/Source/NSAutoreleasePool.m @@ -319,6 +319,30 @@ static IMP initImp; - (void) dealloc { struct autorelease_thread_vars *tv = ARP_THREAD_VARS; + + [self emptyPool]; + + /* + * Remove self from the linked list of pools in use. + * We already know that we have deallocated our child (if any), + * but we may have a parent which needs to know we have gone. + */ + if (tv->current_pool == self) + { + tv->current_pool = _parent; + } + if (_parent != nil) + { + _parent->_child = nil; + } + + /* Don't deallocate ourself, just save us for later use. */ + push_pool_to_cache (tv, self); + GSNOSUPERDEALLOC; +} + +- (void) emptyPool +{ unsigned i; Class classes[16]; IMP imps[16]; @@ -384,50 +408,6 @@ static IMP initImp; released = released->next; } } - - /* - * Remove self from the linked list of pools in use. - * We already know that we have deallocated our child (if any), - * but we may have a parent which needs to know we have gone. - */ - if (tv->current_pool == self) - { - tv->current_pool = _parent; - } - if (_parent != nil) - { - _parent->_child = nil; - } - - /* Don't deallocate ourself, just save us for later use. */ - push_pool_to_cache (tv, self); - GSNOSUPERDEALLOC; -} - -- (void) emptyPool -{ - struct autorelease_array_list *released; - - if (_child) - { - [_child dealloc]; - } - - released = _released_head; - while (released) - { - unsigned int i; - - for (i = 0; i < released->count; i++) - { - id anObject = released->objects[i]; - released->objects[i] = nil; - [anObject release]; - } - released->count = 0; - released = released->next; - } - _released = _released_head; } - (void) _reallyDealloc diff --git a/Source/NSRunLoop.m b/Source/NSRunLoop.m index 85f688ddf..5e7af174f 100644 --- a/Source/NSRunLoop.m +++ b/Source/NSRunLoop.m @@ -405,6 +405,7 @@ static NSComparisonResult tSort(GSIArrayItem i0, GSIArrayItem i1) - (void) _checkPerformers: (GSRunLoopCtxt*)context { + CREATE_AUTORELEASE_POOL(arp); if (context != nil) { GSIArray performers = context->performers; @@ -462,9 +463,11 @@ static NSComparisonResult tSort(GSIArrayItem i0, GSIArrayItem i1) { [array[i] fire]; RELEASE(array[i]); + IF_NO_GC([arp emptyPool]); } } } + RELEASE(arp); } /** @@ -793,6 +796,7 @@ extern IMP wRetImp; && ([timerDate(t) timeIntervalSinceReferenceDate] <= now)) { [t fire]; + IF_NO_GC([arp emptyPool]); now = GSTimeNow(); } @@ -831,6 +835,7 @@ extern IMP wRetImp; RELEASE(min_timer); } GSNotifyASAP(); /* Post notifications. */ + IF_NO_GC([arp emptyPool]); } _currentMode = savedMode; }