简体   繁体   English

调用`System.gc()`是一种在Java应用程序中管理内存的好方法吗?

[英]Is calling `System.gc()` a good way to manage memory in a Java application?

I have a java application that wants to invoke the system.gc(). 我有一个想要调用system.gc()的java应用程序。 Is it a reasonable method to release memory? 释放记忆是一种合理的方法吗? or any other advice? 或任何其他建议? Really appreciate! 万分感激!

Just stop referencing the variable. 只需停止引用变量即可。 You don't need to invoke System#gc() yourself. 您不需要自己调用System#gc() If the JVM is on an edge of an OutOfMemoryError , it will certainly run the GC. 如果JVM是上的边缘OutOfMemoryError ,一定运行GC。

If stopping referencing the variables is not an option because you really need them, then you need to profile your application to fix/clean any memory leaks and/or just give JVM more memory at startup. 如果停止引用变量不是一个选项,因为你真的需要它们,那么你需要分析你的应用程序来修复/清除任何内存泄漏和/或只是在启动时给JVM更多的内存。

In Java it is not necessary to explicitly invoke garbage collection. 在Java中,没有必要显式调用垃圾收集。 It is done automatically by the virtual machine. 它由虚拟机自动完成。

Don't call System.gc() . 不要调用System.gc() Running the garbage collector unnecessarily is a great way to ruin the performance of your program. 不必要地运行垃圾收集器是破坏程序性能的好方法。 The JVM will collect garbage when it needs to. JVM将在需要时收集垃圾。

One of the biggest advantages of the Java platform is successful automated garbage collection. Java平台的最大优势之一是成功的自动垃圾收集。 Use it. 用它。

Only under very specific circumstances and after being proven by rigorous profiling, would it be a good idea to explicitly invoke gc() . 只有在非常特殊的情况下,经过严格的分析证明后,明确调用gc()才是一个好主意。 It should never make a functional difference, but it might have perceived performance gains under very certain scenarios. 它永远不应该产生功能差异,但它可能在非常特定的情况下感知性能提升。

For example, say you were making a video game with Java and every few minutes you come to a break between levels. 例如,假设您正在使用Java制作视频游戏,并且每隔几分钟您就会在各个级别之间休息。 This might prove to be a good place to explicitly invoke the Garbage Collector if it reduces the chance that a GC cycle will occur while playing the next level (which could be disruptive to gameplay). 如果它可以降低在播放下一级别时可能发生GC循环的可能性(这可能会破坏游戏玩法),那么这可能是显式调用垃圾收集器的好地方。

In short, it should be reserved for times where you know better than the runtime when the GC is most desirable, and again, it should only be used after rigorous profiling justifying its inclusion. 简而言之,它应该保留在最需要GC时比运行时更好的时候,并且只有在严格的分析证明其包含之后才能使用它。

Head First Java这本书说每个变量(实例或局部变量)都有一个范围,当范围丢失时,变量就不再存在了,当谈论对象时,它说对象是垃圾收集器的最后一个实时参考消失。

System.gc() will begin Full Collection, which is especially bad if you care about pause times and are using the concurrent or G1 collectors. System.gc()将开始Full Collection,如果你关心暂停时间并且正在使用并发或G1收集器,这将特别糟糕。

Unless you have isolated a specific reason that you that you need different behavior, you shouldn't play with garbage collection... you will more likely hurt performance than see any gains. 除非你已经找出了一个特定的原因,即你需要不同的行为,否则你不应该使用垃圾收集...你会比看到任何收益更有可能损害性能。

Soft & weak references are a great way to hold onto data that you'd like to be in memory but can do without if memory gets tight. 软和弱引用是保存您想要在内存中的数据的好方法,但如果内存变紧,则可以不用。

Normal references are strong, ie an object doesn't get finalized or garbage collected while someone holds a strong reference to it. 正常引用很强,即当某人拥有对它的强引用时,对象不会被最终确定或被垃圾收集。 If you hold a SoftReference to an object, the object will likely stay in memory until an OutOfMemory situation when it will be automatically flushed. 如果对某个对象持有一个SoftReference,该对象可能会在OutOfMemory情况下保留在内存中,直到它将被自动刷新。 A WeakReference is likely to be flushed at any time but it might still be useful for occasions where holding an object saves some disk overhead. WeakReference可能随时刷新,但在持有对象节省一些磁盘开销的情况下它可能仍然有用。

Due to these characteristics references are great for caching data without worrying about having to flush data if the memory starts getting tight. 由于这些特性,引用非常适合缓存数据,而不必担心在内存开始变紧的情况下必须刷新数据。

Easiest way to create a reference is like this: 创建引用的最简单方法是这样的:

Reference<MyClass> r = new SoftReference<MyClass>(new MyClass())

Now you pass the reference around. 现在你传递参考。 When you wish to obtain the object you call Reference.get() 当你想获得对象时,你调用Reference.get()

MyClass myClass = r.get();
if (myClass != null) {
  // do something
}
else {
  // Oh dear class isn't there, go to plan b
}

With gc() you just say to the JRE that it should call the garbage collector. 使用gc(),您只需对JRE说它应该调用垃圾收集器。 Sometimes that suggestion can be useful but in the most cases just setting all the references to the null is the right way. 有时候这个建议很有用,但在大多数情况下,只需设置所有对null的引用就是正确的方法。

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

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