简体   繁体   English

没有垃圾回收的JVM

[英]JVM with no garbage collection

I've read in many threads that it is impossible to turn off garbage collection on Sun's JVM. 我在很多线程中都读过,不可能在Sun的JVM上关闭垃圾回收。 However, for the purpose of our research project we need this feature. 但是,出于我们研究项目的目的,我们需要这个功能。 Can anybody recommend a JVM implementation which does not have garbage collection or which allows turning it off? 任何人都可以推荐一个没有垃圾收集或允许关闭它的JVM实现吗? Thank you. 谢谢。

I wanted to find a fast way to keep all objects in memory for a simple initial proof of concept. 我想找到一种快速的方法来将所有对象保存在内存中,以获得简单的初始概念证明。

The simple way to do this is to run the JVM with a heap that is so large that the GC never needs to run. 执行此操作的简单方法是使用非常大的堆运行JVM,以至于GC永远不需要运行。 Set the -Xmx and -Xms options to a large value, and turn on GC logging to confirm that the GC doesn't run for the duration of your test. -Xmx -Xms选项设置为较大的值,并打开GC日志记录以确认GC在测试期间未运行。

This will be quicker and more straightforward than modifying the JVM. 这比修改JVM更快,更直接。


(In hindsight, this may not work. I vaguely recall seeing evidence that implied that the JVM does not always respect the -Xms setting, especially if it was really big. Still, this approach is worth trying before trying some much more difficult approach ... like modifying the JVM.) (事后看来,这可能不起作用。我模糊地回忆起看到的证据表明JVM并不总是尊重-Xms设置,特别是如果它真的很大。但是,这种方法在尝试一些更难的方法之前值得尝试。 ..比如修改JVM。)

Also, this whole thing strikes me as unnecessary (even counter-productive) for what you are actually trying to achieve. 而且,对于你实际想要达到的目标而言,这一切对我来说都是不必要的(甚至适得其反)。 The GC won't throw away objects unless they are garbage. GC不会丢弃对象,除非它们是垃圾。 And if they are garbage, you won't be able to use them. 如果它们是垃圾,你将无法使用它们。 And the performance of a system with GC disabled / negated is not going to indicative of how a real application will perform. 具有GC禁用/否定的系统的性能不会指示真实应用程序将如何执行。

Depending on your needs this could perhaps work: 根据您的需要,这可能会起作用:

Using the -Xbootclasspath option you may specify your own implementation of API classes. 使用-Xbootclasspath选项,您可以指定自己的API类实现。 You could then for instance override the implementation of Object, and add to the constructor, a globalList.add(this) to prevent the objects from being garbage collected. 然后,您可以例如覆盖Object的实现,并向构造函数添加globalList.add(this)以防止对象被垃圾回收。 It's a hack for sure, but for simple case-study it's perhaps sufficient. 这肯定是一个黑客,但对于简单的案例研究,它可能已经足够了。

Another option is to take an open source jvm and comment out the parts that initiate garbage collection. 另一个选择是采用开源jvm并注释掉启动垃圾收集的部分。 I would guess it is not that complicated. 我猜它并不复杂。

Sun's JVM has no such option. Sun的JVM没有这样的选择。 AFAIK, no other JVM has this option either. AFAIK,没有其他JVM也有此选项。

You did not state what it is that you are exactly trying to achieve but you have one of two options: either use a profiler and see exactly what the GC is doing, that way you can take its effects into consideration. 您没有说明您正在尝试实现的目标,但您有两种选择之一:使用分析器并确切了解GC正在做什么,这样您就可以考虑其影响。 The other is to compile one of the JVMs from source, and disable GC from there. 另一种是从源代码编译其中一个JVM,并从那里禁用GC。

There actually exists a dirty hack to temporarily pause GC. 实际上存在暂时停止GC的脏黑客攻击。 First create a dummy array in Java. 首先在Java中创建一个虚拟数组。 Then, in JNI, use GetPrimitiveArrayCritical function to get hold of the pointer to the array. 然后,在JNI中,使用GetPrimitiveArrayCritical函数来获取指向数组的指针。 The Sun JVM will disable GC to ensure that the array is never moved and the pointer stays valid. Sun JVM将禁用GC以确保永远不会移动数组并且指针保持有效。 To re-enable GC, you can call the ReleasePrimitiveArrayCritical function on the pointer. 要重新启用GC,可以在指针上调用ReleasePrimitiveArrayCritical函数。 But this is very implementation specific since other VM impl may pin the object instead of disabling GC entirely. 但这是非常具体的实现,因为其他VM impl可能会固定对象而不是完全禁用GC。 (Tested to work on Oracle Jdk 7 & 8) (经测试可用于Oracle Jdk 7和8)

Maybe you could try making your VM's available memory sufficient for GC never to be run. 也许您可以尝试使VM的可用内存足以让GC永远无法运行。

My (allbeit limited) experience leads me to suggest that the VM is, by default, extremely lazy and extremely reluctant to run GC. 我的(尽管有限)经验使我建议默认情况下,VM非常懒惰并且非常不愿意运行GC。

giving -Xmx 16384M (or some such) and making sure that your research subject stays well below that limit, might give you the environment you wish to obtain, allthough even then it will obviously not be guaranteed. 给-Xmx 16384M(或其他一些)并确保你的研究对象远远低于这个限制,可能会给你你想要获得的环境,尽管如此,它显然不会得到保证。

Can you get an open source JVM and disable its GC, for example Sun's Hotspot ? 你能获得一个开源JVM并禁用它的GC,例如Sun的Hotspot吗?

If there was no Garbage Collection what would you expect to be the semantics of code like this? 如果没有垃圾收集,你会期望像这样的代码的语义?

public myClass {

      public void aMethod() {

            String text = new String("xyz");

      }

}

In the absence of GC any item newed and with a stack scoped reference could never be reclaimed. 在没有GC的情况下,任何新建的项目和堆栈范围的引用都无法回收。 Even if your own classes could decide not to use local variables like this, or to use only primitive types I don't see how you would safely use any standard Java library. 即使你自己的类决定不使用这样的局部变量,或者只使用原始类型,我也看不到你如何安全地使用任何标准Java库。

I'd be interested to hear more about your usage scenario. 我有兴趣了解有关您的使用场景的更多信息。

Take a look at Oracle's JRockit JVM . 看看Oracle的JRockit JVM I've seen very good near-deterministic performance on Intel hardware with this JVM and you can prod and poke the runtime using the Mission Control utility to see how well it's performing. 我已经在使用此JVM的英特尔硬件上看到了非常好的接近确定性的性能,您可以使用任务控制实用程序来刺激和戳戳运行时,以查看它的执行情况。

Though you can't turn GC off completely, I believe that you can use the -Xnoclassgc option to disable the collection of classes. 虽然您无法完全关闭GC,但我相信您可以使用-Xnoclassgc选项来禁用类的集合。 The GC can be tuned to minimize latency at the expense of leaving memory consumption to grow. 可以调整GC以最小化延迟 ,但代价是留下内存消耗增长。 You may need a license to drop the latency as low as you need if you're going this route. 如果您要使用此路线,您可能需要许可证才能将延迟降低到所需的最低水平。

There is also a Realtime version of the JRockit JVM available but I don't think that there is a free-to-developers version of this available. 还有一个可用的JRockit JVM的实时版本,但我认为没有可用的免费开发人员版本。

You can only turn off the GC if its not actually needed (otherwise your application would run out of memory) and if you didn't need to GC, it shouldn't run anyway. 你只能关闭GC,如果它实际上不需要(否则你的应用程序将耗尽内存),如果你不需要GC,它不应该运行。

The simplest option would be to not discard any objects, this will avoid GC being performed (And set the max memory very high so you don't run out). 最简单的选择是不丢弃任何对象,这将避免执行GC(并设置最大内存非常高,因此您不会用完)。

You may find that you get GCs on startup and you may consider a no-GC when running acceptable. 您可能会发现在启动时获得GC,并且在运行时可以考虑使用无GC。

the question is old but for those who might be interested, there is a proposal to 问题是陈旧的,但对于那些可能感兴趣的人,有一个建议

Develop a GC that only handles memory allocation, but does not implement any actual memory reclamation mechanism. 开发一个只处理内存分配但不实现任何实际内存回收机制的GC。 Once available Java heap is exhausted, perform the orderly JVM shutdown. 一旦可用的Java堆耗尽,就执行有序的JVM关闭。

JEP draft: Epsilon GC: The Arbitrarily Low Overhead Garbage (Non-)Collector JEP草案:Epsilon GC:任意低开销垃圾(非)收集器

If I had this problem I would get IBM's Jikes Research Virtual Machine because: 如果我有这个问题,我会得到IBM的Jikes Research虚拟机,因为:

  • The run-time system is written in Java itself (with special extensions) 运行时系统是用Java编写的(带有特殊扩展)
  • The whole thing was designed as a research vehicle and is relatively easy to tweak. 整个事情被设计为研究工具,相对容易调整。

You can't turn off GC forever, because Java programs do allocate and eventually you'll run out of memory, but it's quite possible that you can delay GC for the duration of your experiment by telling the JVM not to start collecting until the heap gets really big. 你无法永远关闭GC,因为Java程序确实会分配并最终耗尽内存,但是很可能你可以通过告诉JVM在堆之前不开始收集来延迟GC的实验期间变得非常大。 (That trick might work on other JVMs as well, but I wouldn't know where to find the knobs to start twirling.) (这个技巧也可能适用于其他JVM,但我不知道在哪里找到旋钮开始旋转。)

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

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