0

Code like this:

NSData *data = [NSMutableData dataWithLength:20*1024*1024];
UIPasteboard* genePasteboard = [UIPasteboard generalPasteboardSwizzle];
genePasteboard.items = @[@{@"test":data}];        //P1
NSLog(@"%d", (int)[genePasteboard.items count]);  //P2
NSArray<NSDictionary<NSString *, id> *> *items = genePasteboard.items  //P3

At P1 the memory usage increases 20MB, it's easy to understand, however, P2 and p3 will increase 20MB too. i printed the address of items and genePasteboard.items, those two are not same, it means every time I ref to genePasteboard.items, it will copy whole items?

Another question I have is: when I use code like this to clear UIPasteboard:

genePasteboard.items = @[@{}];

The memory usage will not decrease immediately, only when I continue the process, it will release at sometime(guess in the next main loop?), so, How can i know when the genePasteboard.items memory is release or can I release it immediately?

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
waitianlou
  • 563
  • 3
  • 15
  • Use autorelease pool to clear the memory immediately. https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html – Sachin Vas Feb 17 '17 at 02:52
  • Thanks, i try use ```autorelease pool```, but still not work on ```genePasteboard.items = @[@{}]``` – waitianlou Feb 17 '17 at 02:58
  • An autoreleasepool wouldn't do anything for you here. What's `generalPasteboardSwizzle`? Are you swizzling methods on the pasteboard? That's likely a pretty bad idea, and is almost certainly related to this memory usage. Where is that coming from? – Itai Ferber Feb 17 '17 at 04:44
  • Yes, i swizzling methods on the pasteboard for test the memory useage, i try not swizzling it before, it still alloc memory when i ref to genePasteboard.items – waitianlou Feb 17 '17 at 06:39

0 Answers0