简体   繁体   English

在垃圾收集语言中实现垃圾收集器是否有意义

[英]Does it make sense to implement a Garbage Collector in a Language with Garbage Collection

I want to implement a garbage collector (GC) in Java or Haskell, but does this make sense? 我想在Java或Haskell中实现垃圾收集器(GC),但这有意义吗?

Will I be able to control when my own implementation of the GC kicks in and not the GC of the implementation language? 我是否能够控制我自己的GC实现何时启动而不是GC的实现语言?

I guess that there can be three kinds of answers for this question: 我想这个问题可以有三种答案:

  1. Set a flag when running the virtual machine (VM) that disables the GC of the implementation language. 在运行禁用实现语言的GC的虚拟机(VM)时设置标志。
  2. Use a special construct that lets me manage my own memory, like using continuation-passing style (CPS) to manage my own evaluation strategy. 使用一个特殊的构造,让我管理自己的记忆,比如使用延续传递方式(CPS)来管理我自己的评估策略。
  3. No, use a language with no GC. 不,使用没有GC的语言。

Looking at these: 看看这些:

  1. I prefer the second option since I will be able to use that construct in all languages. 我更喜欢第二种选择,因为我将能够在所有语言中使用该构造。
  2. I don't like the first option because I'll have to manage the memory of my interpreter as well. 我不喜欢第一个选项,因为我还必须管理我的解释器的内存。
  3. (And I don't prefer the third option neither, but I can't control that) (而且我不喜欢第三种选择,但我无法控制它)

This is not a duplicate of I build a interpreter on a language with a garbage collector. 这与我在带有垃圾收集器的语言上构建解释器并不重复 I need a garbage collector for the interpreter? 我需要一个翻译垃圾收集器? as I do not want to bootstrap my interpreter with the underlying GC. 因为我希望我的引导与解释底层GC。

As so often in IT, the answer is: it depends . 正如在IT中经常这样,答案是: 它取决于

When your interpreter keeps control of all "objects" within your context; 您的口译员在您的语境中控制所有“对象”时; then of course, your interpreter will probably keep a list of all "its" objects. 当然,你的翻译可能会保留所有“它”对象的列表。 So even when those objects would somehow be visible to the JVM, they would all be reachable, thus alive; 因此,即使这些对象以某种方式对JVM可见,它们也都可以访问,因此它们是活的; so they could not be subject to JVM gc. 所以他们不能接受JVM gc。

But when you somehow "embed" the objects that your interpreter deals with into the surrounding java context - then the JVM gc could be responsible for them. 但是当你以某种方式“将”解释器处理的对象“嵌入”到周围的java上下文中时 - 那么JVM gc可能会对它们负责。

Coming from there, the answer would go into the direction: yes, this could be possible. 从那里开始,答案就会朝着这个方向发展:是的,这可能是可能的。 But beyond that; 但除此之外; it really depends on your goal and driving requirements. 这实际上取决于你的目标和驾驶要求。 Are you implementing this for pure education purposes; 您是否为纯粹的教育目的实施此项目; or are you interested in creating some sort of "real" product that is of "real" value to other people? 或者您是否有兴趣创建某种对其他人具有“真正”价值的“真实”产品?

If the later is the case, then you probably want to embed your interpreter very tightly with the JVM - in order to benefit from the massive investment that turned the JVM into the great platform it is today. 如果是后一种情况,那么您可能希望将解释器与JVM紧密地嵌入 - 以便从将JVM变为今天的优秀平台的大量投资中获益。 You see, almost 20 years of research went into current JVM JIT compiler and GC technology. 你看,近20年的研究进入了当前的JVM JIT编译器和GC技术。 Do you want to take advantage of that in order to "do your own thing" ... 你想利用它来“做你自己的事情”......

So, as outlined: it very much depends on your "real" goals. 因此,如上所述:它在很大程度上取决于您的“真实”目标。

Finally: you might find this SE-Radio podcast on JRuby and the JVM platform helpful in the context of your question. 最后:您可能会发现JRuby和JVM平台上的这个SE-Radio 播客对您的问题有帮助。 The JRuby folks were simply not happy with the performance of the standard Ruby engine; JRuby人员对标准Ruby引擎的性能不满意; and they choose the JVM as platform to build a better Ruby engine ... 他们选择JVM作为平台来构建更好的 Ruby引擎......

You've clearly already made up your mind about this question. 你已经明确地已经决定了这个问题。 It is possible, remotely, that a GC scheme one comes up with would somehow be better than what the JVM supplies already. 从远程来看,GC方案可能会以某种方式比JVM提供的方案更好。 But consider this: of all the languages that have been ported to the JVM, by all the brilliant developers out there, how many have not leveraged the JVM, including its GC? 但请考虑一下:在所有已经移植到JVM的语言中,有多少人没有利用JVM,包括它的GC?

Still, this is a matter of opinion and experimentation. 不过,这是一个意见和实验问题。 Hopefully we'll all have the wisdom to cast an idea as an hypothesis and seek to disprove it before spinning in a potentially wrong direction. 希望我们都有智慧将一个想法作为一个假设,并试图在一个潜在的错误方向旋转之前反驳它。 If it withstands disproof attempts, then go for it 如果它能够承受不合适的尝试,那就去吧

I recommend actually programming different approaches with a relatively robust example application of your own devise. 我建议使用您自己设计的相对强大的示例应用程序实际编写不同的方法。 Figure out a metric for GC effectiveness. 找出GC效率的指标。 Figure out how much effort it takes you to write your own, and how it affects the metric for good or ill. 弄清楚你自己编写的工作量,以及它如何影响好或坏的指标。 Your own experience then will guide you. 您自己的经验将指导您。

Makes sense for what? 对什么有意义? If you want to implement your own GC just to learn the algorithms involved, it doesn't matter. 如果您想要实现自己的GC只是为了学习所涉及的算法,那没关系。 A GC basically just manages resources that reference each other, until they don't. GC基本上只管理相互引用的资源,直到它们没有。 That's perfectly doable in Java and other GCed languages. 这在Java和其他GCed语言中是完全可行的。 Instead of freeing the memory, you'd just call another method on the object and remove it from a list of 'living' objects. 您只需在对象上调用另一个方法并将其从“生命”对象列表中删除,而不是释放内存。

For a shipping product? 对于运输产品? Not sure. 不确定。 You can take advantage of existing mechanisms in the implementation language, but a GC in particular usually needs control over when memory should be relinquished. 您可以利用实现语言中的现有机制,但特别是GC通常需要控制何时应该放弃内存。 And if the implementation language prevents that, that's a problem. 如果实现语言阻止了这一点,那就是一个问题。

If you write a program in Java, GC of JVM will automatically manage memory for your program. 如果用Java编写程序,JVM的GC将自动管理程序的内存。 You now want implement a new GC for another programming language, but it is also a program. 您现在想要为另一种编程语言实现一个新的GC,但它也是一个程序。 So GC of JVM will manage memory for your own GC, then your own GC will manage memory for target programming language. 因此,JVM的GC将为您自己的GC管理内存,然后您自己的GC将管理目标编程语言的内存。

The question is, if the GC of JVM stop the world, it will stop your own GC, your own GC will also stop the world for your target programming language. 问题是,如果JVM的GC停止了全世界,它将停止您自己的GC,您自己的GC也将停止您的目标编程语言的世界。

So, yes, you can implement your own GC in Java or other language with GC, but it not necessary. 所以,是的,您可以使用Java或其他语言使用GC实现自己的GC,但这不是必需的。 If your own GC will not be managed by other GC, that's ok. 如果您自己的GC不会被其他GC管理,那没关系。

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

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