Reputation: 3581
My app uses firebase's observeSingleEventOfType quite a fair bit and I came to realise that my app's memory increase over time. I have commented out all my code except a test button that calls the following function.
func loadPostsTest() {
FIRDatabase.database().reference().child("posts").observeSingleEventOfType(.Value, withBlock: { (snapshot: FIRDataSnapshot) in
print(snapshot.value)
})
}
When the program starts, I hit the test button at a fast speed approximately 2,3 times a second and observed the memory graph as shown below. The memory goes up and doesn't come back down after the request. This issue is effecting my app quite a fair bit in the long run as my app's memory would grow from 70mb to 150+mb because of that. Any reason for this?
Note, the short five second rest was me stopping to ensure that all "snapshots" are printed out.
Note 2... When I stop pressing the button, the memory remains at the same level as shown in the "short rest section". Just incase you think that it grows by itself indefinitely
-------UPDATE----------
To further confirm the problem, I have create a brand new project with nothing in it but firebase import, a button in the story board with the following code and simulated on my 6s (Simulating on simulator does not appear to have this problem). Image below proves that there is something fishy going on here with firebase as my memory went from 11.1mb to 17.3mb with 303 requests within a minute or so.
import UIKit
import Firebase
class ViewController: UIViewController {
var count: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func testBtnPressed(sender: AnyObject) {
FIRDatabase.database().reference().child("posts").observeSingleEventOfType(.Value, withBlock: {[weak self] (snapshot: FIRDataSnapshot) in
print(self?.count)
self?.count += 1
})
}
Upvotes: 26
Views: 2372
Reputation: 761
This is likely only happening because you are running on debug build. My results, after ~128 clicks on device with a debug build:
As you can see, the vast majority of memory consumption is due to performance tools related to debugging. You can disable this feature if you'd like to confirm it, by editing your scheme:
And then disabling backtrace recording:
With it disabled, the same ~128 clicks shows essentially no growth:
Granted, you may not want to leave that disabled, as you may find it useful for diagnosing crashes and other issues in your debug builds. It shouldn't be a problem in a release build though!
For reference, this was the code I was running:
import UIKit
import FirebaseDatabase
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
private var count: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func testBtnPressed(sender: AnyObject) {
let db = FIRDatabase.database().reference()
db.child("posts").observeSingleEventOfType(.Value) { [weak self] (snap: FIRDataSnapshot!) in
guard let this = self else { return }
this.count = this.count + 1
this.label.text = "\(this.count)"
}
}
}
Upvotes: 10
Reputation: 34301
I think this way it will help in xcode too. I have done in my android application.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("BLAH_BLAH_STRING");
// Attach a listener to read the data at our posts reference
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Do Some Stuff
ref.removeEventListener(this);
ref = null;
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Or in iOS , I guess they have different methods to be used.
removeAllObservers and removeObserverWithHandle:
Please try with the given two methods above.
Upvotes: 1