Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Allocate Memory in Objective-C to simulate out of memory conditions
//
// TBMemoryStealer.h
// TrailTracker
//
// Created by Jesse Crocker on 3/30/15.
//
//
#import <Foundation/Foundation.h>
@interface TBMemoryStealer : NSObject
+ (instancetype)sharedInstance;
- (NSUInteger)currentTotal;
- (void)allocateMemory:(NSUInteger)bytes;
@end
//
// TBMemoryStealer.m
// TrailTracker
//
// Created by Jesse Crocker on 3/30/15.
//
//
#import "TBMemoryStealer.h"
@interface TBMemoryStealer ()
@property (nonatomic, strong) NSMutableArray *chunks;
@end
@implementation TBMemoryStealer
static TBMemoryStealer *sharedInstace;
+ (instancetype)sharedInstance {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstace = [[TBMemoryStealer alloc] init];
});
return sharedInstace;
}
- (instancetype)init {
self = [super init];
if(self) {
self.chunks = [NSMutableArray array];
}
return self;
}
- (NSUInteger)currentTotal {
NSUInteger total = 0;
for(NSData *data in self.chunks) {
total += data.length;
}
return total;
}
- (void)allocateMemory:(NSUInteger)bytes {
NSMutableData *data = [[NSMutableData alloc] initWithLength:bytes];
[self.chunks addObject:data];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.