# Discourse 内存占用过高问题

**URL:** <https://www.isharkfly.com/t/discourse/15795>\
**Category:** Discourse\
**Tags:** discourse\
**Created:** [May 25, 2024, 12:26pm UTC](https://www.isharkfly.com/t/discourse/15795 "2024-05-25T12:26:21Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [May 25, 2024, 1:06pm UTC](https://www.isharkfly.com/t/discourse/15795/21 "2024-05-25T13:06:23Z")

</div>

> [@CAKE\_NICE](#):
>
> discourse只支持S3储存吗

是可以支持 Google 的。

文章在：[Set up image uploads to Google Cloud Storage - Sysadmins - Discourse Meta](https://meta.discourse.org/t/set-up-image-uploads-to-google-cloud-storage/117655)

用 S3 也是因为我们比较熟悉，S3 对开发人员比较友好，SDK 内容丰富。

Google 的云服务实在是太难用了。

---

<div class="post-metadata">

**Author:** ![CAKE\_NICE](https://www.isharkfly.com/user_avatar/www.isharkfly.com/cake_nice/32/14319_2.png) [@CAKE\_NICE](https://www.isharkfly.com/u/CAKE_NICE)\
**Post date:** [May 25, 2024, 1:07pm UTC](https://www.isharkfly.com/t/discourse/15795/22 "2024-05-25T13:07:07Z")

</div>

![image](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/2/2faa3e5bc58600622762b32500eac38a402a044b.png)  
静默状态 自己上升到80%

---

<div class="post-metadata">

**Author:** ![CAKE\_NICE](https://www.isharkfly.com/user_avatar/www.isharkfly.com/cake_nice/32/14319_2.png) [@CAKE\_NICE](https://www.isharkfly.com/u/CAKE_NICE)\
**Post date:** [May 25, 2024, 1:08pm UTC](https://www.isharkfly.com/t/discourse/15795/23 "2024-05-25T13:08:50Z")

</div>

![image](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/b/bc0e0c8d0b349b80fc1a09305ea2138d77bf9209.png)  
**还原备份以后…**

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [May 25, 2024, 1:11pm UTC](https://www.isharkfly.com/t/discourse/15795/25 "2024-05-25T13:11:22Z")

</div>

内存能排序下，看那个进程用的内存最多吗？

开始看到用到 500 多M 的是 Ruby，这个是 Discourse 的语言。

现在看不到你的那个进程用的内存最多。

---

<div class="post-metadata">

**Author:** ![CAKE\_NICE](https://www.isharkfly.com/user_avatar/www.isharkfly.com/cake_nice/32/14319_2.png) [@CAKE\_NICE](https://www.isharkfly.com/u/CAKE_NICE)\
**Post date:** [May 25, 2024, 1:14pm UTC](https://www.isharkfly.com/t/discourse/15795/26 "2024-05-25T13:14:38Z")

</div>

![image](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/c/c89a511c41d9c98f22c01f0af59b31749f5132b0.jpeg)

htop

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [May 25, 2024, 1:21pm UTC](https://www.isharkfly.com/t/discourse/15795/27 "2024-05-25T13:21:49Z")

</div>

找一下截图中最后一个，我没有看到，是占了 1378MB 吗？这个进程是容器吗？

找下 Htop 上占内存最多的几个进程看看。

nzaha-agent 占了你 710M，这里差不多是 1 个 G 了。我们的安装中没有这个进程

rsyslogd，这个是日志回滚进程，也占了 215M。

---

<div class="post-metadata">

**Author:** ![CAKE\_NICE](https://www.isharkfly.com/user_avatar/www.isharkfly.com/cake_nice/32/14319_2.png) [@CAKE\_NICE](https://www.isharkfly.com/u/CAKE_NICE)\
**Post date:** [May 25, 2024, 1:25pm UTC](https://www.isharkfly.com/t/discourse/15795/28 "2024-05-25T13:25:44Z")

</div>

![image](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/5/5e376475ea448b50d7644d7f7f70c3b981082344.jpeg)

**现在按照占用大小 排行了**

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [May 25, 2024, 1:31pm UTC](https://www.isharkfly.com/t/discourse/15795/29 "2024-05-25T13:31:04Z")

</div>

你的这个内存消耗和我们的差不多。

应该启动了差不多是 5 个。

 ![2024-05-25_09-27-11](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/e/e423c2d0dfa6c9645695c03e995823a93b82a094.png)

从这里分开往下：

 ![2024-05-25_09-28-45](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/c/c34423d86c66dd28f520500456f585401db52d8c.png)

我们已经没有进程使用的内存单位是 MB 的了。

你看看从这里往下，你还有什么进程使用的内存是以 MB 为单位的？

感觉不像是 Discourse 程序的问题，可能是你机器上启动的其他服务也占了内存。

我们的机器就启动了一个防火墙和 Discourse 进程，没有装任何其他附加的程序，比如说宝塔，cpanel 这种。

---

<div class="post-metadata">

**Author:** ![CAKE\_NICE](https://www.isharkfly.com/user_avatar/www.isharkfly.com/cake_nice/32/14319_2.png) [@CAKE\_NICE](https://www.isharkfly.com/u/CAKE_NICE)\
**Post date:** [May 25, 2024, 1:34pm UTC](https://www.isharkfly.com/t/discourse/15795/30 "2024-05-25T13:34:44Z")

</div>

![image](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/f/fca80bf48572a7b36c5b3f7dcea3fd7904c4a26f.jpeg)  
这个是倒数的了

---

<div class="post-metadata">

**Author:** ![CAKE\_NICE](https://www.isharkfly.com/user_avatar/www.isharkfly.com/cake_nice/32/14319_2.png) [@CAKE\_NICE](https://www.isharkfly.com/u/CAKE_NICE)\
**Post date:** [May 25, 2024, 1:38pm UTC](https://www.isharkfly.com/t/discourse/15795/32 "2024-05-25T13:38:52Z")

</div>

![image](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/b/b4fb4266ba04fa0c93d17292b7df663d2f43c2f8.png)  
感觉这个更加直观

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [May 25, 2024, 1:38pm UTC](https://www.isharkfly.com/t/discourse/15795/33 "2024-05-25T13:38:55Z")

</div>

还有一个办法。

把 Discourse 的进程停掉，看看在这个情况下你服务器的内存消耗是多少？

从上面 Discourse 的容器内存请求来看，对服务器的请求是正常的，没有看出来和我们的有多大的差异。

最关键的进程不是最小的，是 Discourse 进程后面的，在从上往下的图中，往下拉一下，看看 Discourse 进程后面的有那些。

---

<div class="post-metadata">

**Author:** ![CAKE\_NICE](https://www.isharkfly.com/user_avatar/www.isharkfly.com/cake_nice/32/14319_2.png) [@CAKE\_NICE](https://www.isharkfly.com/u/CAKE_NICE)\
**Post date:** [May 25, 2024, 1:41pm UTC](https://www.isharkfly.com/t/discourse/15795/34 "2024-05-25T13:41:51Z")

</div>

docker停掉容器吗

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [May 25, 2024, 1:44pm UTC](https://www.isharkfly.com/t/discourse/15795/35 "2024-05-25T13:44:06Z")

</div>

这个图片上，你的 Unicorn 启了 7 个。

我们的 Unicorn 只启动了 2 个，你比我们多了 5 个。

平均一个 Unicorn 在 400MB 左右，7 个 再加上其他一些乱七八糟的，差不多已经 3GB 左右了。

---

<div class="post-metadata">

**Author:** ![CAKE\_NICE](https://www.isharkfly.com/user_avatar/www.isharkfly.com/cake_nice/32/14319_2.png) [@CAKE\_NICE](https://www.isharkfly.com/u/CAKE_NICE)\
**Post date:** [May 25, 2024, 1:44pm UTC](https://www.isharkfly.com/t/discourse/15795/36 "2024-05-25T13:44:33Z")

</div>

![image](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/b/bc10403463ac7c4a1068cae0b56be8806a164dbd.png)  
 ![image](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/a/a9eafe45c9adae49e8c7a8b684769b0b2003e8a0.png)  
停掉以后

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [May 25, 2024, 1:49pm UTC](https://www.isharkfly.com/t/discourse/15795/37 "2024-05-25T13:49:28Z")

</div>

> [@CAKE\_NICE](#):
>
> 停掉以后

正常你服务器运行需要 500MB，然后你启了 7 个 unicorns，乱七八糟加起来也差不多 3.x GB 了。

如果你的 app.yml 没有修改的话，unicorns 应该默认是启 2 个。[Rescaling the server, which configs need to be changed? - unicorn workers, memory, etc - Installation - Discourse Meta](https://meta.discourse.org/t/rescaling-the-server-which-configs-need-to-be-changed-unicorn-workers-memory-etc/252788)

但容器认为系统有多余资源可以用的话，貌似是会自动添加 unicorns。

---

<div class="post-metadata">

**Author:** ![CAKE\_NICE](https://www.isharkfly.com/user_avatar/www.isharkfly.com/cake_nice/32/14319_2.png) [@CAKE\_NICE](https://www.isharkfly.com/u/CAKE_NICE)\
**Post date:** [May 25, 2024, 1:51pm UTC](https://www.isharkfly.com/t/discourse/15795/38 "2024-05-25T13:51:22Z")

</div>

我的app.yaml是默认的

```auto
## this is the all-in-one, standalone Discourse Docker container template
##
## After making changes to this file, you MUST rebuild
## /var/discourse/launcher rebuild app
##
## BE *VERY* CAREFUL WHEN EDITING!
## YAML FILES ARE SUPER SUPER SENSITIVE TO MISTAKES IN WHITESPACE OR ALIGNMENT!
## visit http://www.yamllint.com/ to validate this file as needed

templates:
  - "templates/postgres.template.yml"
  - "templates/redis.template.yml"
  - "templates/web.template.yml"
  ## Uncomment the next line to enable the IPv6 listener
  #- "templates/web.ipv6.template.yml"
  - "templates/web.ratelimited.template.yml"
  ## Uncomment these two lines if you wish to add Lets Encrypt (https)
  - "templates/web.ssl.template.yml"
  - "templates/web.letsencrypt.ssl.template.yml"

## which TCP/IP ports should this container expose?
## If you want Discourse to share a port with another webserver like Apache or nginx,
## see https://meta.discourse.org/t/17247 for details
expose:
  - "80:80" # http
  - "443:443" # https

params:
  db_default_text_search_config: "pg_catalog.english"

  ## Set db_shared_buffers to a max of 25% of the total memory.
  ## will be set automatically by bootstrap based on detected RAM, or you can override
  db_shared_buffers: "1024MB"

  ## can improve sorting performance, but adds memory usage per-connection
  #db_work_mem: "40MB"

  ## Which Git revision should this container use? (default: tests-passed)
  #version: tests-passed

env:
  LC_ALL: en_US.UTF-8
  LANG: en_US.UTF-8
  LANGUAGE: en_US.UTF-8
  # DISCOURSE_DEFAULT_LOCALE: en

  ## How many concurrent web requests are supported? Depends on memory and CPU cores.
  ## will be set automatically by bootstrap based on detected CPUs, or you can override
  UNICORN_WORKERS: 8

  ## TODO: The domain name this Discourse instance will respond to
  ## Required. Discourse will not work with a bare IP number.
  DISCOURSE_HOSTNAME: 

  ## Uncomment if you want the container to be started with the same
  ## hostname (-h option) as specified above (default "$hostname-$config")
  #DOCKER_USE_HOSTNAME: true

  ## TODO: List of comma delimited emails that will be made admin and developer
  ## on initial signup example 'user1@example.com,user2@example.com'
  DISCOURSE_DEVELOPER_EMAILS: 

  ## TODO: The SMTP mail server used to validate new accounts and send notifications
  # SMTP ADDRESS, username, and password are required
  # WARNING the char '#' in SMTP password can cause problems!
  DISCOURSE_SMTP_ADDRESS: 
  DISCOURSE_SMTP_PORT: 587
  DISCOURSE_SMTP_USER_NAME: 
  DISCOURSE_SMTP_PASSWORD:
  #DISCOURSE_SMTP_ENABLE_START_TLS: true # (optional, default true)
  DISCOURSE_SMTP_DOMAIN: 
  DISCOURSE_NOTIFICATION_EMAIL: 

  ## If you added the Lets Encrypt template, uncomment below to get a free SSL certificate
  LETSENCRYPT_ACCOUNT_EMAIL: 

  ## The http or https CDN address for this Discourse instance (configured to pull)
  ## see https://meta.discourse.org/t/14857 for details
  #DISCOURSE_CDN_URL: https://discourse-cdn.example.com

  ## The maxmind geolocation IP account ID and license key for IP address lookups
  ## see https://meta.discourse.org/t/-/173941 for details
  #DISCOURSE_MAXMIND_ACCOUNT_ID: 123456
  #DISCOURSE_MAXMIND_LICENSE_KEY: 1234567890123456

## The Docker container is stateless; all data is stored in /shared
volumes:
  - volume:
      host: /var/discourse/shared/standalone
      guest: /shared
  - volume:
      host: /var/discourse/shared/standalone/log/var-log
      guest: /var/log

## Plugins go here
## see https://meta.discourse.org/t/19157 for details
hooks:
  after_code:
    - exec:
        cd: $home/plugins
        cmd:
          - git clone https://github.com/discourse/docker_manager.git

## Any custom commands to run after building
run:
  - exec: echo "Beginning of custom commands"
  ## If you want to set the 'From' email address for your first registration, uncomment and change:
  ## After getting the first signup email, re-comment the line. It only needs to run once.
  #- exec: rails r "SiteSetting.notification_email='info@unconfigured.discourse.org'"
  - exec: echo "End of custom commands"

```

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [May 25, 2024, 1:55pm UTC](https://www.isharkfly.com/t/discourse/15795/39 "2024-05-25T13:55:03Z")

</div>

这一行，你的 UNICORN\_WORKS ，参数是配的 8。

难怪你的服务器会启动到 7。

 ![2024-05-25_09-53-12](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/c/cd5d71c8672758f2255a73b6ceb683b654de903f.png)

把这行参数改成 2，然后重构 Discourse。

我觉得为了保证其他进程可用，你的的服务器应该可以设置到 4。

---

<div class="post-metadata">

**Author:** ![CAKE\_NICE](https://www.isharkfly.com/user_avatar/www.isharkfly.com/cake_nice/32/14319_2.png) [@CAKE\_NICE](https://www.isharkfly.com/u/CAKE_NICE)\
**Post date:** [May 25, 2024, 1:56pm UTC](https://www.isharkfly.com/t/discourse/15795/40 "2024-05-25T13:56:49Z")

</div>

这是什么意思呢?官方默认就是8 害 我重构一下

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [May 25, 2024, 2:04pm UTC](https://www.isharkfly.com/t/discourse/15795/41 "2024-05-25T14:04:16Z")

</div>

> [@CAKE\_NICE](#):
>
> 这是什么意思呢?

Unicorn 是 **Ruby Web 应用中的一款应用服务器** ，提供两个功能： 为Rack 应用（使用Ruby 编程语言开发的Web 服务器和Web 应用程序之间的模块化接口）提供HTTP 服务能力 为Web 应用实现高并发能力。

Unicorn 的数量大小决定了内存的消耗。

对于高性能服务器，上面的参数决定了可以占用多少内存。

配置文件是 Discourse 根据你的机器性能在按照的时候算出来可以最多分配到多少 Unicorn。

Unicorn 数量越多，并行处理能力越强，但占用内存和 CPU 就越多。

上面的数字决定了 Discourse 运行时候可以启用 Unicorn 数量的上限，不管怎么样都不会超过这个数字。但是如果机器性能不够，那就有可能会低于这个数字。

所以你的上限是 8，但实际上只启动到 7，机器就不行了。

## 为什么会这样

具体原因不非常清楚为什么会这样。

我的感觉就是机器数据和实际运行能力上出现了虚标。

因为计算是按照机器处理能力参数进行计算的，比如说内存多大，CPU 性能多少，具体怎么算我也不太清楚。

算出来的时候 Discourse 觉得你的机器可以配置到 8，但实际上能到 7 已经是极限了。

我的机器也是 4G 的内存，算出来也只能建议配置到 2 个Unicorn。所以建议你还是把这个参数改低到 3 左右的样子。

---

<div class="post-metadata">

**Author:** ![CAKE\_NICE](https://www.isharkfly.com/user_avatar/www.isharkfly.com/cake_nice/32/14319_2.png) [@CAKE\_NICE](https://www.isharkfly.com/u/CAKE_NICE)\
**Post date:** [May 25, 2024, 2:05pm UTC](https://www.isharkfly.com/t/discourse/15795/42 "2024-05-25T14:05:57Z")

</div>

我就说怎么2g和4g 用起来占用都那么多

 ![image](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/6/6becfb5cd4e817f975595e5ed3ee87d4dea4a101.png)  
现在这个是我调成2的样子

[Previous page](https://www.isharkfly.com/t/discourse/15795.md?page=1)

[Next page](https://www.isharkfly.com/t/discourse/15795.md?page=3)
