search

Home  >  Q&A  >  body text

iOS ARC 内存一直不停的增加

ringa_leeringa_lee2771 days ago616

reply all(4)I'll reply

  • PHP中文网

    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

    reply
    0
  • 天蓬老师

    天蓬老师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.

    reply
    0
  • PHPz

    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

    reply
    0
  • 大家讲道理

    大家讲道理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

    reply
    0
  • Cancelreply