0

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.

Assafs
  • 3,257
  • 4
  • 26
  • 39
Mustafa
  • 253
  • 1
  • 7
  • 15
  • 1
    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. – Fogmeister Mar 10 '17 at 10:51
  • 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 ? – Mustafa Mar 10 '17 at 10:53
  • 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"). – Fogmeister Mar 10 '17 at 10:54
  • 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 crash – Mustafa Mar 10 '17 at 10:56
  • Read my first comment... – Fogmeister Mar 10 '17 at 10:57
  • thank you so if this bug how can i fix it , or must be i report it to apple? – Mustafa Mar 10 '17 at 10:59
  • 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. – Fogmeister Mar 10 '17 at 11:00
  • ok thank you very much my friend – Mustafa Mar 10 '17 at 11:03
  • OK, tested and verified but I'm not sure why the memory increases. Looks more like an issue with CoreAnimation than with the tableview. – Fogmeister Mar 10 '17 at 11:11
  • i don't know i follow example on the youtube for leaking memory issue the cells is dealloc in that example at the end , https://www.youtube.com/watch?v=cR4Wc4JGOMg , but in my project not dealloc – Mustafa Mar 10 '17 at 11:27
  • The cells will dealloc when the tableview is deallocd. Provide a timestamp. I'm not watching a 45min video :) – Fogmeister Mar 10 '17 at 11:28
  • 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 that – Mustafa Mar 10 '17 at 11:32
  • 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. Bye – Fogmeister Mar 10 '17 at 11:35
  • 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. – Fogmeister Mar 10 '17 at 11:36
  • ok my friend i will test it and back to you , thank you very much – Mustafa Mar 10 '17 at 11:38

1 Answers1

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