diff --git a/Main/SOGo.h b/Main/SOGo.h index 7c41f1625..677bc8df3 100644 --- a/Main/SOGo.h +++ b/Main/SOGo.h @@ -32,6 +32,7 @@ @interface SOGo : SoApplication { SOGoCache *cache; + NSTimer *timerCheckMemoryLimit; } - (NSDictionary *) currentLocaleConsideringLanguages:(NSArray *)_langs; diff --git a/Main/SOGo.m b/Main/SOGo.m index beef82a47..e1b628f97 100644 --- a/Main/SOGo.m +++ b/Main/SOGo.m @@ -72,6 +72,7 @@ static BOOL hasCheckedTables; static BOOL debugRequests; static BOOL useRelativeURLs; static BOOL trustProxyAuthentication; +static const NSTimeInterval kMemoryCheckTimerInS = 5.0f; #ifdef GNUSTEP_BASE_LIBRARY static BOOL debugLeaks; @@ -138,8 +139,9 @@ static BOOL debugLeaks; /* load products */ [[SOGoProductLoader productLoader] loadAllProducts: YES]; - if (vMemSizeLimit > 0) + if (vMemSizeLimit > 0) { [self logWithFormat: @"All products loaded - current memory usage at %d MB", [[NSProcessInfo processInfo] virtualMemorySize]/1048576]; + } } - (id) init @@ -152,6 +154,12 @@ static BOOL debugLeaks; rm = [[WEResourceManager alloc] init]; [self setResourceManager:rm]; [rm release]; + + timerCheckMemoryLimit = [NSTimer scheduledTimerWithTimeInterval: kMemoryCheckTimerInS + target: self + selector: @selector(checkIfDaemonHasToBeShutdown) + userInfo: nil + repeats:YES]; } return self; @@ -477,14 +485,24 @@ static BOOL debugLeaks; if (vMemSizeLimit > 0) { vmem = [[NSProcessInfo processInfo] virtualMemorySize]/1048576; - if (vmem > vMemSizeLimit) { - [self logWithFormat: - @"terminating app, vMem size limit (%d MB) has been reached" - @" (currently %d MB)", - vMemSizeLimit, vmem]; - [self terminate]; + if (![self isTerminating]) { + if (timerCheckMemoryLimit) { + [timerCheckMemoryLimit invalidate]; + } + + [self logWithFormat: + @"terminating app, vMem size limit (%d MB) has been reached" + @" (currently %d MB)", + vMemSizeLimit, vmem]; + [self terminate]; + } else { + [self logWithFormat: + @"vMem size limit (%d MB) has been reached" + @" (currently %d MB) but process is already terminating", + vMemSizeLimit, vmem]; + } } } } @@ -593,18 +611,6 @@ static BOOL debugLeaks; forKey: @"SOGo-Request-Duration"]; } - if (![self isTerminating]) - { - if (!runLoopModes) - runLoopModes = [[NSArray alloc] initWithObjects: NSDefaultRunLoopMode, nil]; - - // TODO: a bit complicated? (-perform:afterDelay: doesn't work?) - [[NSRunLoop currentRunLoop] performSelector: - @selector (checkIfDaemonHasToBeShutdown) - target: self argument: nil - order:1 modes:runLoopModes]; - } - return resp; }