Firebase听众占用大量内存 [英] Firebase listeners take up huge amount of memory

查看:126
本文介绍了Firebase听众占用大量内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Heroku的应用程序旁边运行了一个节点工作器,它监听我的Firebase数据库上的某些路径。问题是,听这些路径似乎占用了大量的内存。如果我在下面的路径中听到13000个项目的变化,如果在我的Heroku服务器上占用了总共147MB的内存:

  setInterval => 
@ ref.log_memory('Listener interval')
,1000

@ ref.firebaseClient.child('listings')。'child_changed',(snap)=> ;
@ ref.log('child_changed')

输出:

  22:39:07 worker.1 |信息:内存:35mb总计 -  66mb rss  -  23mb heapUsed 
22:39:08 worker.1 |信息:内存:总共36mb - 67mb rss - 18mb heapUsed
22:39:09 worker.1 |信息:内存:总共37mb - 69mb rss - 23mb heapUsed
22:39:10 worker.1 |信息:内存:总共54mb - 72mb rss - 25mb heapUsed
22:39:11 worker.1 |信息:内存:总共54mb - 82mb rss - 33mb heapUsed
22:39:13 worker.1 |信息:内存:总共147MB - 186mb rss - 94mb heapUsed
22:39:14 worker.1 |信息:内存:总共147MB - 186mb rss - 94mb heapUsed
22:39:15 worker.1 |信息:内存:总共147MB - 186mb rss - 94mb heapUsed
22:39:15 worker.1 | info:child_changed
22:39:16 worker.1 |信息:内存:总共147MB - 186mb rss - 95mb heapUsed
22:39:17 worker.1 | info:内存:147MB总共 - 186MB rss - 95mb heap使用

内存多了一个它听的路径?是因为它获取路径中的所有项目并收听每个项目?有什么方法可以解决这个问题吗?解决方案

Firebase同步您听的位置(或查询中)的数据上。它会保存内存中所有活动数据的副本。



为了减少内存使用量,请使用数据量较少的位置。或者使用一个查询(例如用 limitToLast())来减少活动的数据量。

在使用NoSQL数据库时,将活动数据与历史数据分开通常是一个好主意。通过保持活动的数据集小,您可以减少许多操作的资源使用情况。在您的情况下,它不仅会减少您的Heroku服务器的内存使用量,而且还会减少Firebase服务器上所需的内存/ CPU,这也将加速操作。


I have a node worker running along side my app on Heroku, that listens on certain paths on my Firebase database. Issue is that listening to these paths seem to take up a huge amount of memory. If I listen for changes on a path like the one below with 13000 items in it, if takes up 147mb of total memory on my Heroku server:

setInterval =>
  @ref.log_memory('Listener interval')
, 1000

@ref.firebaseClient.child('listings').on 'child_changed', (snap) =>
  @ref.log('child_changed')

Output:

22:39:07 worker.1 | info:    Memory: 35mb total - 66mb rss - 23mb heapUsed
22:39:08 worker.1 | info:    Memory: 36mb total - 67mb rss - 18mb heapUsed
22:39:09 worker.1 | info:    Memory: 37mb total - 69mb rss - 23mb heapUsed
22:39:10 worker.1 | info:    Memory: 54mb total - 72mb rss - 25mb heapUsed
22:39:11 worker.1 | info:    Memory: 54mb total - 82mb rss - 33mb heapUsed
22:39:13 worker.1 | info:    Memory: 147mb total - 186mb rss - 94mb heapUsed
22:39:14 worker.1 | info:    Memory: 147mb total - 186mb rss - 94mb heapUsed
22:39:15 worker.1 | info:    Memory: 147mb total - 186mb rss - 94mb heapUsed
22:39:15 worker.1 | info:    child_changed
22:39:16 worker.1 | info:    Memory: 147mb total - 186mb rss - 95mb heapUsed
22:39:17 worker.1 | info:    Memory: 147mb total - 186mb rss - 95mb heapUsed

If it normal that Firebase would take up so much memory for a path it listens to? Is it because it fetches all the items in the path and listens to each item? Is there any way to get around this?

解决方案

Firebase synchronizes the data at the location (or in the query) that you listen on. It keeps a copy of all the active data at that location in memory.

To reduce the memory usage, listen to a location with less data. Or use a query (e.g. with limitToLast()) to reduce the amount of data that is active.

It is typically a good idea to separate your active data from your historical data when using a NoSQL database. By keeping the active data set small, you can reduce the resource usage for many operation. In your case not only would it reduce memory usage in your Heroku server, but it would also reduce the memory/CPU required on the Firebase servers, which will speed up operation as well.

这篇关于Firebase听众占用大量内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆