在ruby中运行手动GC是否是一种好习惯 [英] Is it good practice to run Manual GC in ruby

查看:122
本文介绍了在ruby中运行手动GC是否是一种好习惯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google上读到很多文章,提到ruby不会向操作系统释放内存,我也理解这一点,因为从操作系统分配内存是很昂贵的.

I read quite a lot on Google that ruby does not release back memory to the OS and I understand the as well because allocating memory from OS is a costly fair.

是什么促使我问这个的?

Which has led me to ask this

如果开发人员希望Ruby从ruby将内存释放回OS,他们将如何做.

If a developer want the Ruby to release the memory back to OS from ruby how do they do it.

-我猜答案是在Ruby中手动触发GC.

- I guess the answer is manually triggering a GC in Ruby.

但是建议在生产应用程序上执行(运行手动GC).

But is it advisable to do (run manual GC) on a production application.

我正在考虑在赛璐oid中创建一个单独的线程(或Actor)以每2小时启动一个GC.

I'm thinking of creating a separate thread(or Actor) in Celluloid to start a GC at every 2 hours time.

任何人都可以建议.以上解决方案适合生产吗?

Can anyone suggest. Is the above solution advisable for production.

注意:我想我也知道GC.start(意味着用Ruby停止整个世界)

Note: I think I also know GC.start (means stop the whole world in Ruby)

注意:Ruby版本2.2.2 MRI Centos 4G RAM具有4核.

Note: Ruby Version 2.2.2 MRI Centos 4G RAM with 4 core.

这是出现问题的原因 http://pastebin.com/rYdYwxLJ

推荐答案

我在Google上读到很多文章,提到ruby不会向操作系统释放内存,我也理解这一点,因为从操作系统分配内存是很昂贵的.

I read quite a lot on Google that ruby does not release back memory to the OS and I understand the as well because allocating memory from OS is a costly fair.

那是不对的. Ruby语言完全没有透露将内存释放回OS的任何内容.实际上,Ruby语言并没有说明内存,周期.

That's simply not true. The Ruby Language doesn't say anything at all about releasing memory back to the OS. In fact, the Ruby Language doesn't say anything about memory, period.

Ruby有许多不同的实现,它们具有许多不同的内存分配器和许多不同的垃圾收集器.有些将内存释放回操作系统,有些不释放,某些运行在托管运行时之上,甚至根本不管理内存.

There are a lot of different implementations of Ruby with a lot of different memory allocators and a lot of different garbage collectors. Some release memory back to the OS, some don't, some run on top of a managed runtime and don't even manage memory at all.

例如,在J9上运行的JRuby会将内存释放回操作系统.在具有G1 GC,ParallelScavenge GC或ParallelOld GC的HotSpot上运行的JRuby会将内存释放回操作系统.

For example, JRuby running on J9 will release memory back to the OS. JRuby running on HotSpot with the G1 GC, ParallelScavenge GC, or ParallelOld GC will release memory back to the OS.

如果开发人员希望Ruby从ruby将内存释放回OS,那么他们将如何做.

If a developer want the Ruby to release the memory back to OS from ruby how do they do it.

你不知道.该实现要么将内存释放回OS,在这种情况下您无需执行任何操作,或者也不会将内存释放回OS,因此您无法执行任何操作.

You don't. Either the implementation releases memory back to the OS, in which case you don't need to do anything, or it doesn't release memory back to the OS, in which you cannot do anything.

-我猜答案是在Ruby中手动触发GC.

如果实现未将内存释放回操作系统,则不会将内存释放回操作系统.期间.

If the implementation doesn't release memory back to the OS, then it doesn't release memory back to the OS. Period.

如果实现确实将内存释放回操作系统,那么无论您是否手动触发GC,它都会这样做.

If the implementation does release memory back to the OS, then it will do that whether you manually trigger a GC or not.

顺便说一句:您无法在Ruby中手动触发GC.您可以(使用GC::start)建议一个GC,但不能触发它.

By the way: you cannot manually trigger a GC in Ruby. You can suggest a GC (using GC::start), but not trigger it.

但是建议在生产应用程序上执行(运行手动GC).

But is it advisable to do (run manual GC) on a production application.

几乎没有.无论是现在还是以后运行,GC都比您更了解.

Pretty much never. The GC knows better than you do whether it makes sense to run now or at a later time.

注意:我想我也知道GC.start(意味着用Ruby停止整个世界)

Note: I think I also know GC.start (means stop the whole world in Ruby)

否,GC::start只是建议进行GC.它不会强制GC.此外,GC并不意味着世界末日.例如.当在带有Metronome GC的J9 JVM上运行JRuby或使用无暂停GC的Azul Zing JVM上运行JRuby时,永远不会停止运行.

No, GC::start simply suggests to do a GC. It does not force a GC. Also, GC does not mean stop-the-world. E.g. when running JRuby on the J9 JVM with the Metronome GC or running JRuby on the Azul Zing JVM with the Pauseless GC will never stop-the-world.

注意:Ruby版本2.2.2

Note: Ruby Version 2.2.2

那不是有趣的地方.有趣的问题是您正在使用哪种实现,哪种垃圾回收器以及哪种操作系统. (当然还有所有这些的版本和配置设置.)

That's not the interesting bit. The interesting question is which implementation you are using, which garbage collector, and which OS. (And of course the versions and configuration settings for all of those.)

这篇关于在ruby中运行手动GC是否是一种好习惯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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