scala Scala垃圾收集?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19486562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:46:42  来源:igfitidea点击:

Scala Garbage Collection ?

scalamemory-managementgarbage-collectionjvm

提问by Learner

I'm relatively new to Scala.

我对 Scala 比较陌生。

If I have a construct like this,

如果我有这样的构造,

sampleFile.map(line => line.map {
  var myObj = new MyClass(word); 
  myObj.func();
})

I create an object of MyClassand do something inside a class method (func()). I repeat this for all the lines in a file (through map). So, I create an object at every step of my iteration (for every line). The scope of myObjwill be void when I start next iteration (will they be destroyed at the end of the block, or will they be orphaned out in memory?). My doubt is when does the garbage collection triggered? Also, is it expensive to create an object at every step of the iteration? Does this have any performance implication when the number of lines increases to 1 million?

我创建了一个对象MyClass并在类方法 ( func()) 中执行了一些操作。我对文件中的所有行重复此操作(通过map)。因此,我在迭代的每一步(为每一行)创建一个对象。myObj当我开始下一次迭代时,范围将是无效的(它们会在块的末尾被销毁,还是会在内存中被孤立?)。我的疑问是垃圾收集何时触发?另外,在迭代的每一步都创建一个对象是否很昂贵?当行数增加到 100 万时,这对性能有什么影响吗?

采纳答案by mikera

Your objects should all get garbage collected fairly quickly (assuming myObj.func()does not store a pointer to myObj somewhere else...). On the JVM, any unreferenced objects should get garbage collected - and your last reference to the new object disappears as soon as myObjgoes out of scope.

你的对象都应该很快被垃圾收集(假设myObj.func()没有在其他地方存储指向 myObj 的指针......)。在 JVM 上,任何未引用的对象都应该被垃圾收集 - 一旦myObj超出范围,您对新对象的最后一次引用就会消失。

Garbage collection of short-lived objectsis generally very cheap and efficient, so you probably shouldn't worry about it (at least until you have benchmarks / measured performance problems that prove otherwise....)

短期对象的垃圾收集通常非常便宜和高效,所以你可能不应该担心它(至少在你有基准/测量的性能问题证明不是......)

In particular, since you appear to be doing IO (reading from a sample file?) then I expect the overhead of GC is negligible compared to the cost of your disk IO operations.

特别是,由于您似乎在执行 IO(从示例文件中读取?),那么与磁盘 IO 操作的成本相比,我预计 GC 的开销可以忽略不计。

回答by Chris Martin

Garbage collection is the responsibility of the JVM, not Scala. So the precise details depend on which JVM you're running. There is no defined time at which garbage collection is triggered; the JVM tries to do it when it is opportune or necessary.

垃圾收集是 JVM 的责任,而不是 Scala。因此,精确的细节取决于您正在运行的 JVM。没有定义触发垃圾收集的时间;JVM 会在适当或必要时尝试执行此操作。

Someone more knowledgeable than me on the subject of GC algorithms and JVM tuning could probably give you some concrete explanation to address your performance concerns, but in general I'd say you should just trust that JVMs are pretty good at garbage collecting "intelligently".

在 GC 算法和 JVM 调优方面比我更了解的人可能会给你一些具体的解释来解决你的性能问题,但总的来说,我会说你应该相信 JVM 非常擅长“智能”垃圾收集。