菜鸟教程小白 发表于 2022-12-11 19:32:01

ios - 内存使用量不断增加


                                            <p><p>我创建了一个非常简单的测试应用程序。它包括 3 个 ViewController 。主视图 Controller 有一个表格 View ,它使用自定义单元格。另外两个 ViewController 可以通过主视图 Controller 访问,每个都有 Collection View ,可以回到主视图 Controller 。</p>

<p>这是内存问题。每当我单击 3 个 ViewController 中的任何单元格时,内存使用量都会增加。我在使用“泄漏”分析模板时运行了该应用程序,但没有发现任何泄漏。还使用了'Allocations'分析模板,检查了2个 ViewController (记录了引用计数),我的程序下所有存储的引用计数都被释放了。</p>

<p>我无法使用 Debug Memory Graph,因为它不断使 Xcode 崩溃...</p>

<p>主视图 Controller </p>

<pre><code>import UIKit

class TableViewController: UITableViewController, UISearchBarDelegate {

    @IBOutlet weak var searchForTool: UISearchBar!
    @IBOutlet weak var toolTable: UITableView!

    var searchActive : Bool = false
    var data = [&#34;Alphabetical&#34;, &#34;Numerical&#34;]
    var identities = [&#34;A&#34;, &#34;B&#34;]

    override func viewDidLoad() {
      super.viewDidLoad()

      toolTable.delegate = self
      toolTable.dataSource = self
    }

    override func numberOfSections(in tableView: UITableView) -&gt; Int {
      return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return data.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: &#34;Cell&#34;, for: indexPath) as! CustomCell

      cell.toolLabel.text = data
      return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

      let vcName = identities
      let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
      self.navigationController?.pushViewController(viewController!, animated: true)
    }
}
</code></pre>

<p>其他 ViewController 之一(都相同)</p>

<pre><code>import UIKit

class AlphabeticalViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet weak var collectionView: UICollectionView!

    var labelArray = ()
    var identities = ()

    override func viewDidLoad() {
      super.viewDidLoad()

      labelArray = [&#34;Main&#34;, &#34;Definitions&#34;, &#34;Steps&#34;, &#34;References&#34;, &#34;Other&#34;]

      identities = [&#34;C&#34;, &#34;B&#34;, &#34;B&#34;, &#34;D&#34;, &#34;E&#34;]

      self.navigationController?.setNavigationBarHidden(true, animated: false)

      collectionView.delegate = self
      collectionView.dataSource = self

    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      return labelArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: &#34;cell&#34;, for: indexPath)

      let myLabel = cell.viewWithTag(1) as! UILabel

      myLabel.text = labelArray

      return cell
    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

      let vcName = identities
      let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
      self.navigationController?.pushViewController(viewController!, animated: true)

    }

}
</code></pre>

<p>自定义单元格类</p>

<pre><code>import UIKit

class CustomCell: UITableViewCell {

    @IBOutlet weak var toolLabel: UILabel!

    override func awakeFromNib() {
      super.awakeFromNib()
      // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
      super.setSelected(selected, animated: animated)

      // Configure the view for the selected state
    }

}
</code></pre>

<p>如果需要,我会提供任何其他图片。任何帮助将不胜感激。</p>

<p>我使用分配工具获得的信息。
<a href="/image/CYXJ4.png" rel="noreferrer noopener nofollow"><img src="/image/CYXJ4.png" alt="Possible Memory Leak"/></a>
注释掉的代码只是我不需要atm的搜索功能。</p>

<p> <a href="/image/vWDun.png" rel="noreferrer noopener nofollow"><img src="/image/vWDun.png" alt="Storyboard"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>问题似乎出在 <code>CustomCell</code> 内部,可能是某些资源未初始化。
<code>awakeFromNib()</code> 或 <code>setSelected(...)</code> 中是否有一些代码?</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 内存使用量不断增加,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/43528592/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/43528592/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 内存使用量不断增加