简体   繁体   English

Clojure中无限循环中的内存泄漏

[英]Memory leak in infinite loop in Clojure

I have the following function: 我有以下功能:

(defn- clock-process [every f]
  (while true
  (f)
  (Thread/sleep every)))

And I'm spinning it off in to its own thread, to avoid blocking the main thread: 我将其旋转到自己的线程中,以避免阻塞主线程:

(future
  (clock-process 3600000 ;; fire every hour
    #(intensive-fn ...)))

On a Heroku dyno, total memory use is substantially growing at each hour interval: 在Heroku dyno上,每个小时间隔的总内存使用量基本上在增加:

在此处输入图片说明

Where the memory drops off is where the application is restarted. 内存丢失的地方是重新启动应用程序的地方。

Can anyone help me understand what's happening? 谁能帮助我了解发生了什么事?

I don't know enough to help you understand why this is happening, but I can suggest some options for mitigating the problem. 我所掌握的知识不足以帮助您了解为什么会这样,但是我可以建议一些缓解问题的方法。

tl;dr: Lowering you heap size is probably the best thing to do. tl; dr:减小堆大小可能是最好的方法。

First, set up the Memory logging agent . 首先,设置内存日志记录代理 This will periodically print some messages to the logs like: 这将定期将一些消息打印到日志中,例如:

source=web.1 measure.mem.jvm.heap.used=33M measure.mem.jvm.heap.committed=376M measure.mem.jvm.heap.max=376M
source=web.1 measure.mem.jvm.nonheap.used=19M measure.mem.jvm.nonheap.committed=23M measure.mem.jvm.nonheap.max=219M
source=web.1 measure.threads.jvm.total=21 measure.threads.jvm.daemon=11 measure.threads.jvm.nondaemon=1 measure.threads.jvm.internal=9

From this, you'll be able to determine where growth occurs (ie heap or non-heap). 由此,您将能够确定增长发生的位置(即堆或非堆)。

If you find that the growth is all heap, you should try setting your Maximum heap size (ie -Xmx ). 如果发现增长全部是堆,则应尝试设置最大堆大小(即-Xmx )。 lower than the defaults. 低于默认值。 Something like -Xmx300m should be sufficient, but you can probably go lower. -Xmx300m东西就足够了,但您可能会降低价格。 You could also generate some heap dumps, and analyze them with a tool like Eclipse MAT if you want to pinpoint the source. 您还可以生成一些堆转储,并使用Eclipse MAT之类的工具来分析它们,如果您要查明源的话。

If you find that growth is in non-heap, then use some of the other steps described in the troubleshooting guide to determine if it's Metaspace or something else. 如果发现增长没有增长,请使用故障排除指南中描述的其他一些步骤来确定它是元空间还是其他。 If it is not in Metaspace, then you may have a native memory leak, which can result from leaving file handles or buffers open. 如果不在Metaspace中,则可能会发生本机内存泄漏,这可能是由于文件句柄或缓冲区处于打开状态造成的。

Have you tried to reproduce the problem locally? 您是否尝试过在本地重现该问题? If you do so, if will be easier to dig a little deeper using tools like VisualVM and jmap . 如果这样做,可以使用VisualVM和jmap类的工具更轻松地深入研究。

Finally, you can open a support ticket with Heroku . 最后,您可以使用Heroku打开支持凭单 They can look at things like smaps to help with non-heap/native-memory leaks. 他们可以查看smaps类的smaps以帮助解决非堆/本机内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM