From 8f266b1f88505b2c3b15220d0bd2eef0e8ff9a07 Mon Sep 17 00:00:00 2001 From: Eric Henderson Date: Tue, 12 Aug 2014 16:27:34 -0400 Subject: [PATCH] hopefully reduce memory leaks and add a mechanism for relaunching MemoryTamer when it starts using up too much memory --- README.md | 2 ++ Rakefile | 4 ++-- app/app_delegate.rb | 31 +++++++++++++++++++++++++++++++ appcast.xml | 9 +++++++++ vendor/mem_info/mem_info.h | 1 + vendor/mem_info/mem_info.m | 13 +++++++++++++ 6 files changed, 58 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1e1b8cf..61a6d04 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ A RubyMotion application for keeping memory usage in check. Shows up in the men * **v0.9.3:** Auto-threshold has changed. My original design for it didn't turn out well, so I changed it to set the thresholds to a certain percent of post-freeing memory on a full freeing. Hopefully this will work better. * **v0.9.4:** fix a refreshing issue with auto-threshold * **v0.9.5:** hopefully work around a memory leak by using a bundled copy of growlnotify; also, new icon +* **v0.9.6:** hopefully reduce memory leaks and add a mechanism for relaunching MemoryTamer when it starts using up too much memory ###Versions (code-signed with developer ID): * **v0.3:** (Mavericks-only) @@ -66,3 +67,4 @@ A RubyMotion application for keeping memory usage in check. Shows up in the men * **v0.9.3:** * **v0.9.4:** * **v0.9.5:** +* **v0.9.6:** diff --git a/Rakefile b/Rakefile index 317b9ad..8cfd284 100644 --- a/Rakefile +++ b/Rakefile @@ -35,8 +35,8 @@ Motion::Project::App.setup do |app| app.icon = 'Icon.icns' app.info_plist['CFBundleIconFile'] = 'Icon.icns' app.name = 'MemoryTamer' - app.version = '0.9.5' - app.short_version = '0.9.5' + app.version = '0.9.6' + app.short_version = '0.9.6' app.identifier = 'us.myepg.MemoryTamer' app.info_plist['NSUIElement'] = 1 app.info_plist['SUFeedURL'] = 'https://raw.githubusercontent.com/henderea/MemoryTamer/master/appcast.xml' diff --git a/app/app_delegate.rb b/app/app_delegate.rb index fb28e82..f987e0c 100644 --- a/app/app_delegate.rb +++ b/app/app_delegate.rb @@ -4,6 +4,32 @@ def to_weak end end +class NSApplication + # -(void) relaunchAfterDelay : (float) seconds + # { + # NSTask *task = [[[NSTask alloc] init] autorelease]; + # NSMutableArray *args = [NSMutableArray array]; + # [args addObject: @ "-c"]; + # [args addObject: [NSString stringWithFormat: @ "sleep %f; open \"%@\"", seconds, [[NSBundle mainBundle] bundlePath]]]; + # [task setLaunchPath: @ "/bin/sh"]; + # [task setArguments: args]; + # [task launch]; + # + # [self terminate : nil]; + # } + def relaunchAfterDelay(seconds) + task = NSTask.alloc.init + args = [] + args << '-c' + args << ('sleep %f; open "%s"' % [seconds, NSBundle.mainBundle.bundlePath]) + task.launchPath = '/bin/sh' + task.arguments = args + task.launch + + self.terminate(nil) + end +end + class AppDelegate attr_accessor :free_display_title @@ -113,9 +139,14 @@ def applicationDidFinishLaunching(notification) GrowlApplicationBridge.setGrowlDelegate(self) NSLog "Starting up with memory = #{dfm}; pressure = #{Persist.store.pressure}" Thread.start { + @start_time = NSDate.date @last_free = NSDate.date - 30 @last_trim = NSDate.date loop do + if MemInfo.getMTMemory > (200 * (1024 ** 2)) && (NSDate.date - @start_time) > 300 + NSLog "MemoryTamer is using #{format_bytes(MemInfo.getMTMemory, true)}; restarting" + NSApp.relaunchAfterDelay(1) + end cfm = get_free_mem @statusItem.setTitle(Persist.store.show_mem? ? format_bytes(cfm.to_weak).to_weak : '') if Persist.store.update_while? || !@freeing diff = (NSDate.date - @last_free) diff --git a/appcast.xml b/appcast.xml index 56d51ea..9974ad7 100755 --- a/appcast.xml +++ b/appcast.xml @@ -5,6 +5,15 @@ https://raw.githubusercontent.com/henderea/MemoryTamer/master/appcast.xml Most recent changes with links to updates. en + + Version 0.9.6 + + http://releases.io/henderea/MemoryTamer/0.9.6?heading=true + + Tue, 12 Aug 2014 16:30:00 -0400 + + 10.7 + Version 0.9.5 diff --git a/vendor/mem_info/mem_info.h b/vendor/mem_info/mem_info.h index 847189c..e71ddb8 100644 --- a/vendor/mem_info/mem_info.h +++ b/vendor/mem_info/mem_info.h @@ -10,4 +10,5 @@ + (int) getPagesInactive; + (int) getMemoryPressure; + (int) getTotalMemory; ++ (long long) getMTMemory; @end \ No newline at end of file diff --git a/vendor/mem_info/mem_info.m b/vendor/mem_info/mem_info.m index cd53c16..9d97dbd 100644 --- a/vendor/mem_info/mem_info.m +++ b/vendor/mem_info/mem_info.m @@ -52,4 +52,17 @@ + (int) getMemoryPressure { + (int) getTotalMemory { return read_sysctl_int("hw.memsize"); } + ++ (long long) getMTMemory { + struct task_basic_info info; + mach_msg_type_number_t size = sizeof(info); + kern_return_t kerr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size); + if( kerr == KERN_SUCCESS ) { + //NSLog(@"rs: %lld ; vs: %lld", (long long)info.resident_size, (long long)info.virtual_size); + return info.resident_size; + } else { + NSLog(@"Error with task_info()"); + return -1; + } +} @end \ No newline at end of file