简体   繁体   English

如何在Clojure中泄漏内存?

[英]How can I leak memory in Clojure?

For a presentation at the Bay Area Clojure Meetup on Thursday I am compiling a list of ways to leak memory in Clojure. 周四在Bay Area Clojure Meetup上的一个演示文稿中,我正在编写一个在Clojure中泄漏内存的方法列表。

So far I have: 到目前为止,我有:

  • hold onto the head of an infinite sequence 抓住无限序列的头部
  • creating lots of generic classes by calling lambda in a loop (is this still a problem) 通过在循环中调用lambda来创建许多泛型类(这仍然是一个问题)
  • holding a reference to unused data 持有对未使用数据的引用
  • ... ...

What else? 还有什么?

By keeping a reference to a seq on a large collection. 通过在大型集合上保留对seq的引用。 eg: 例如:

(drop 999990 (vec (range 1000000))) 

returns a seq of ten elements that holds a reference to the whole vector! 返回一个包含十个元素的seq,它们包含对整个向量的引用!

Another obvious way is to use any Java library that leaks memory. 另一个显而易见的方法是使用泄漏内存的任何Java库。 (eg Qt Jambi ) (例如Qt Jambi

About lambdas, read here and here and here . 关于lambdas, 在这里这里这里阅读。 I think this is fixed in the latest versions of Clojure. 我认为这是在最新版本的Clojure中修复的。

There is the intern call as well. 还有实习生电话。

Note that your examples are not leaking memory in the common sense of the word. 请注意,您的示例不会在常识中泄漏内存。 You can still access the objects (not sure about the classes -- I assume one can re-find them via some API), ie they haven't been lost. 您仍然可以访问对象(不确定类 - 我假设可以通过某些API重新找到它们),即它们没有丢失。 With certain things like the classes and interned strings it is just impossible to forget the data so the effect is the same. 对于像类和实习字符串这样的某些东西,忘记数据是不可能的,所以效果是一样的。

Clojure memory leaks will usually be very similar to Java memory leaks. Clojure内存泄漏通常与Java内存泄漏非常相似。 However the fact that collections are "persistent" means that if you add something into a collection and don't realize that you retained a reference to the old version of the collection as well as the new value means that memory is consumed to keep the old version hanging around. 但是,集合是“持久的”这一事实意味着,如果您将某些内容添加到集合中并且没有意识到您保留了对旧版本集合的引用以及新值意味着使用内存来保留旧内容版本闲逛。

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

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