Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Memory pressure event 2 vm 0

Tags:

xcode

swift

I saw a message in the Xcode console which is Received memory pressure event 1 vm pressure 0. I'm not really sure what causes this and why but I do know this prints when I'm in a camera viewcontroller. I'm guessing it's nothing to worry about (yet) but is it pointing to something I should fix asap?

FirstViewController: UIViewController {

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let image = info[UIImagePickerController.InfoKey.editedImage] as! UIImage
        let croppedImage = cropToBounds(image: image, width: 10, height: 10)
        self.productImage.image = croppedImage
        print("size: \(croppedImage.size)")
        print("original size: \(image.size)")
        self.dismiss(animated: true, completion: nil)
    }

    func setupCameraPicker() {
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = .camera;
            imagePicker.allowsEditing = true
            self.present(imagePicker, animated: true, completion: nil)
        }
    }
}
like image 706
maniponken Avatar asked May 21 '19 13:05

maniponken


1 Answers

autoreleasepool

autoreleasepool { 
   let image = info[UIImagePickerController.InfoKey.editedImage] as! UIImage
    let croppedImage = cropToBounds(image: image, width: 10, height: 10)
    self.productImage.image = croppedImage
    print("size: \(croppedImage.size)")
    print("original size: \(image.size)")
    self.dismiss(animated: true, completion: nil)
 }

Try to put your code in autoreleasepool Foundation’s NSAutoreleasePool type, later abstracted to the @autoreleasepool block, is a very old concept in iOS development. During the Obj-C era of iOS, usage of this type was important to prevent your app's memory from blowing up in specific cases. As ARC and Swift came around and evolved, very few people still have to manually play around with memory, making seeing it become a rare occurrence.

like image 142
Om Parhar Avatar answered Nov 15 '22 04:11

Om Parhar