PHP中文网2017-04-17 15:46:11
Do not use the subdataWithRange method of NSData in the NSThread thread
Reposted, you can take a look
天蓬老师2017-04-17 15:46:11
I didn’t give you the complete code, and I didn’t see what operations were done on the data. In addition, calling sleepForTimeInterval on the main thread will block the UI thread.
PHPz2017-04-17 15:46:11
Are you sure about the memory growth caused by these codes? If you are sure, you can add a breakpoint at the entrance of this function, and then debug step by step to see how much memory increases after finishing which line of code
大家讲道理2017-04-17 15:46:11
If there are many loops, you can manually add a release pool:
@autoreleasepool {
your code
}
For example, change your code to:
- (void)viewDidLoad {
[super viewDidLoad];
staitc int EVERBUFFERLEN = 2000;
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"wakeup.pcm" ofType:nil]];
NSMutableData *mData = [NSMutableData dataWithData:data];
int readLength = 0;
while (readLength < mData.length) {
if (mData.length - readLength > EVERBUFFERLEN) {
@autoreleasepool {
NSData *data = [mData subdataWithRange:NSMakeRange(readLength, EVERBUFFERLEN)];
readLength += EVERBUFFERLEN;
[NSThread sleepForTimeInterval:0.01];
data = nil;
}
}
}
Recommended part-time jobs for engineers, click on my avatar for details