APP进入后台后才会释放内存
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了APP进入后台后才会释放内存相关的知识,希望对你有一定的参考价值。
我正在开发一个带侧边菜单的应用程序,在ContainController中使用两个ViewController。当关闭侧面菜单ViewController(调用deinit方法)后,内存就不会被释放(大概是23 MiB),直到应用程序进入后台。在Xcode 9中构建的“Leaks”工具表示根本没有内存泄漏......任何人都可以告诉我什么是错的?非常感谢提前!
import UIKit
import CoreData
enum MenuState {
case Collapsed
case Expanding
case Expanded
}
class ContainerViewController: UIViewController {
weak var managedObjectContext: NSManagedObjectContext!
weak var mainViewController: MasterViewController!
weak var menuViewController: MenuViewController?
var currentState = MenuState.Collapsed {
didSet {
let shouldShowShadow = currentState != .Collapsed
showShadowForMainViewController(shouldShowShadow: shouldShowShadow)
}
}
override func viewDidLoad() {
super.viewDidLoad()
mainViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "mainView") as! MasterViewController
view.addSubview(mainViewController.view)
DispatchQueue.global(qos: .userInteractive).async {
self.mainViewController.managedObjectContext = self.managedObjectContext
self.addChildViewController(self.mainViewController)
self.mainViewController.didMove(toParentViewController: self)
}
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action:#selector(self.handlePanGesture(_:)))
self.mainViewController.view.addGestureRecognizer(panGestureRecognizer)
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action:#selector(self.handleTapGesture))
self.mainViewController.view.addGestureRecognizer(tapGestureRecognizer)
}
@objc func handlePanGesture(_ recognizer: UIPanGestureRecognizer) {
switch(recognizer.state) {
case .began:
let dragFromLeftToRight = (recognizer.velocity(in: view).x > 0)
if (currentState == .Collapsed && dragFromLeftToRight) {
currentState = .Expanding
addMenuViewController()
}
case .changed:
let positionX = recognizer.view!.frame.origin.x + recognizer.translation(in: view).x
recognizer.view!.frame.origin.x = positionX < 0 ? 0 : positionX
let boundX = mainViewController.view.frame.width - menuViewExpandedOffset
if positionX > 0 {
recognizer.view!.frame.origin.x = positionX > boundX ? boundX : positionX
}
recognizer.setTranslation(.zero, in: view)
case .ended:
let boundX = mainViewController.view.frame.width - menuViewExpandedOffset
let hasMovedhanHalfway = (recognizer.view!.frame.origin.x > boundX * 0.3)
animateMainView(shouldExpand: hasMovedhanHalfway)
default:
break
}
}
@objc func handleTapGesture() {
if currentState == .Expanded {
animateMainView(shouldExpand: false)
}
}
//ADD MENUVC
func addMenuViewController() {
if (menuViewController == nil) {
menuViewController = UIStoryboard(name: "Main", bundle: nil)
.instantiateViewController(withIdentifier: "menuView")
as? MenuViewController
view.insertSubview(menuViewController!.view, at: 0)
addChildViewController(menuViewController!)
menuViewController!.didMove(toParentViewController: self)
}
}
let menuViewExpandedOffset: CGFloat = 480
func animateMainView(shouldExpand: Bool) {
if (shouldExpand) {
currentState = .Expanded
animateMainViewXPosition(targetPosition: mainViewController.view.frame.width -
menuViewExpandedOffset)
}
else {
animateMainViewXPosition(targetPosition: 0) { finished in
self.currentState = .Collapsed
self.menuViewController?.view.removeFromSuperview()
self.menuViewController?.removeFromParentViewController()
self.menuViewController = nil
}
}
}
func animateMainViewXPosition(targetPosition: CGFloat,
completion: ((Bool) -> Void)! = nil) {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1.0,
initialSpringVelocity: 0, options: .curveEaseOut, animations: {
self.mainViewController.view.frame.origin.x = targetPosition
}, completion: completion)
}
func showShadowForMainViewController(shouldShowShadow: Bool) {
if (shouldShowShadow) {
mainViewController.cameraBGimage.layer.shadowOpacity = 0.6
mainViewController.cameraBGimage.layer.shadowOffset = CGSize(width: 0, height: 1)
} else {
mainViewController.cameraBGimage.layer.shadowOpacity = 0.0
}
}
}
答案
可以说你的应用程序没有任何问题。 ios系统将以其认为最佳的方式管理内存。
如果你的对象应该被取消分配,那么你没事。如果您想更好地了解视图何时被取消分配,您可以随时使用Allocations
的de Instruments
工具并按应用程序的名称对其进行过滤。
以上是关于APP进入后台后才会释放内存的主要内容,如果未能解决你的问题,请参考以下文章
Diycode开源项目 如何解决InputMethodManager造成的内存泄漏问题
java线程中的 sleep() wait() yield()