table view cell issue with dealloc from memory
Asked Answered
A

1

1

I know my question is simple and I know many question on that topic but I can't find any solution to it.

I have simple table view only have the label for 100 rows. When I scroll the memory is increasing every time I scroll using the instrument tool in the Xcode although I use the dequeuing cell as apple documentation my question why the cells not releasing from memory (free from memory) every time I scroll it's creating new cell.

The following my code:

import UIKit

class ViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate{

    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = "Table View Cell! "

        return cell

    }


}

You can check the instruments tools and you can see the memory increasing every time you scroll.

Please, how can I release cells from memory when I'm scrolling?

Thanks a lot.

Archipenko answered 10/3, 2017 at 10:47 Comment(15)
Is this your only code? If so it may be a bug as this shouldn't cause a memory increase after the first 10 or so cells are loaded.Headrest
only this is my code one page and i using the instruments tools i see the cell not transient you can check it i don't know why this is new project the cell have no object so why not releasing ?Archipenko
The cells won't be released. They are dequeued. When they go off screen they are put into an array and reused instead of creating more and more cells. In your example there will only ever be about 10 cells actually created. Those 10 are then reused. (Hence, "reuse identifier" and "reusable cell").Headrest
so why the memory is increasing ? if not creating new cell? i get the increasing memory every time i scrolling and then if the memory still increasing i get crashArchipenko
Read my first comment...Headrest
thank you so if this bug how can i fix it , or must be i report it to apple?Archipenko
If it's a bug it will be in Swift and not in your code (if this is actually all your code). I am testing currently too. If it is then you can file a radar at bugreport.apple.com. This will then go into the black hole that is Apple and you will never hear from them about it. But it may be fixed at some point in the next decade.Headrest
ok thank you very much my friendArchipenko
OK, tested and verified but I'm not sure why the memory increases. Looks more like an issue with CoreAnimation than with the tableview.Headrest
i don't know i follow example on the youtube for leaking memory issue the cells is dealloc in that example at the end , youtube.com/watch?v=cR4Wc4JGOMg , but in my project not deallocArchipenko
The cells will dealloc when the tableview is deallocd. Provide a timestamp. I'm not watching a 45min video :)Headrest
if i dealloc the table view when i scrolling every time i get the crash only the solution is if i scrolling must the cells which is go in the screen off dealloc and reuse it again i think thatArchipenko
If you dealloc the tableview while scrolling then you will cause a crash. Don't do that. Why would you do that? I have told you how cells work. You have told me I am wrong multiple times. ByeHeadrest
Go into the profiler... look at what the memory is. It isn't cells. If it was cells it would grow significantly faster than it is doing. Besides, you can literally inspect the allocated memory and see that it is coming from CoreAnimation... NOT the cells. There are only enough cells created to cover the height of the screen. You can test this by using a custom cell and putting a log into the init method. Please test it and see what is happening.Headrest
ok my friend i will test it and back to you , thank you very muchArchipenko
E
2

Simply you can deallocate memory from view controller's table view cell

When Back press clicked

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    if self.isMovingFromParent {
        AllProductList.removeAll() 
        self.view = nil
    }
}

deinit {
    print("deinit called")
}
  1. Remove all array data model
  2. Remove parent view
Examinee answered 22/6, 2022 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.