Skip to content

Commit

Permalink
Simulate low memory warning on devices
Browse files Browse the repository at this point in the history
  * Add + (void)simulateLowMemoryWarning in NIDeviceInfo.
  * Simulate low memory warning when tapping on NIOverviewMemoryPageView
  • Loading branch information
senpo committed Nov 19, 2012
1 parent 2023ba0 commit b84f416
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/overview/src/NIDeviceInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ NSString* NIStringFromBytes(unsigned long long bytes);
*/
+ (unsigned long long)bytesOfTotalMemory;

/**
* Simulate low memory warning
*
* Don't use this in production because it uses private API
*/
+ (void)simulateLowMemoryWarning;

#pragma mark Disk Space /** @name Disk Space */

Expand Down Expand Up @@ -120,4 +126,5 @@ NSString* NIStringFromBytes(unsigned long long bytes);
*/
+ (void)endCachedDeviceInfo;


@end
18 changes: 18 additions & 0 deletions src/overview/src/NIDeviceInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,24 @@ + (unsigned long long)sVMStats.wire_count)
}


///////////////////////////////////////////////////////////////////////////////////////////////////
+ (void)simulateLowMemoryWarning
{
SEL memoryWarningSel = NSSelectorFromString(@"_performMemoryWarning");
if ([[UIApplication sharedApplication] respondsToSelector:memoryWarningSel]) {
NIDINFO(@"Simulate low memory warning");
// Supress the warning. -Wundeclared-selector was used while ARC is enabled.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[[UIApplication sharedApplication] performSelector:memoryWarningSel];
#pragma clang diagnostic pop
} else {
// UIApplication no loger responds to _performMemoryWarning
exit(1);
}
}


///////////////////////////////////////////////////////////////////////////////////////////////////
+ (unsigned long long)bytesOfFreeDiskSpace {
if (!sIsCaching && ![self updateFileSystemAttributes]) {
Expand Down
12 changes: 12 additions & 0 deletions src/overview/src/NIOverviewPageView.m
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ - (id)initWithFrame:(CGRect)frame {
self.pageTitle = NSLocalizedString(@"Memory", @"Overview Page Title: Memory");

self.graphView.dataSource = self;

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap:)];
// We still want to be able to drag the pages.
tap.cancelsTouchesInView = NO;
[self addGestureRecognizer:tap];
}
return self;
}
Expand All @@ -288,6 +293,13 @@ - (void)update {
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didTap:(UIGestureRecognizer *)gesture {
// Simulate low memory warning while tapping on NIOverviewMemoryPageView
[NIDeviceInfo simulateLowMemoryWarning];
}


///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
Expand Down

0 comments on commit b84f416

Please sign in to comment.