java->系统.gc();这个调用是否打开一个新线程?

发布于 2024-08-29 05:57:43 字数 202 浏览 5 评论 0原文

比如我这样的代码

... 获取一些内存并丢失所有指向该内存的指针,以便 System.gc();可以收藏一下。

调用 System.gc();

做一些其他任务;


这里做的是“做一些其他任务;”和“System.gc();”并行工作或“执行其他一些任务;”等待“System.gc();”被处决

谢谢

For example I such a code

...
get some memory and lose all the pointers to that memory so that System.gc(); can collect it.

call System.gc();

do some other tasks;


Here does the "do some other tasks;" and "System.gc();" works in paralel or does the "do some other tasks;" waits for "System.gc();" to be executed

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

酷到爆炸 2024-09-05 05:57:43

您可能不应该使用 System.gc()。 C/C++ 用户转向 java 的一个常见误解是,他们认为需要告诉虚拟机何时可以执行垃圾回收。现实情况是,垃圾收集器经过高度优化,并在认为最好时自行执行此任务。通常不建议调用 System.gc()。

如果您确实调用 System.gc(),它会“推荐”系统执行垃圾收集。它实际上可能不会执行收集,尽管它通常会执行。是否在另一个线程中运行取决于实际的收集算法。默认情况下会在运行时阻止所有内容。因此它可能在单独的线程中运行,但它会阻止您当前的执行。

You probably shouldn't be using System.gc(). A common misconception with C/C++ users coming over to java is they think they need to tell the virtual machine when it can perform garbage collection. The reality is that the garbage collector is highly optimized and performs this task by itself when it feels it is best to do so. Calling System.gc() is not usually recommended.

If you do call System.gc() though, it will 'recommend' to the system to perform garbage collection. It may not actually perform a collection though it usually does. If it runs in another thread depends on the actual collection algorithm. The default one will block everything while it runs. So it may run in separate threads but it will block your current execution when it does.

╭ゆ眷念 2024-09-05 05:57:43

这里的确切行为完全取决于 JVM 实现。在规范中(这是正确的 JVM 实现需要为您提供的),它可以并行发生,它可以在代码执行之前首先发生,或者根本不会发生。

在实践中,我对 JVM 的观察是,它立即在单独的线程中运行。然而,在某些情况下,多个调用会产生多个线程,有时它会在一个线程上对请求进行排队。启动的垃圾收集始终是“stop-the-world”类型(即它非常完整并减慢或暂停了应用程序)。

但是,鉴于您对 @Chris Dail 的评论,您的根本问题不是 System.gc() 调用的行为。调用 System.gc() 可以有一些用途。它可以使用清晰的内存,这样您就可以了解应用程序当前实际占用的空间有多大。它还可以用作确保更早发生“停止世界”垃圾收集的策略,以便“停止世界”的时间更短,因为需要清除的内存更少。 (我应该指出,随着 JVM 变得越来越复杂,这种事情越来越没有必要,而且实际上会适得其反)。

然而,它并没有以任何方式解决 OutOfMemoryError。 JVM 不会向您提供 OutOfMemoryError,直到它尽最大努力进行垃圾收集。调用 System.gc 不会改变这一点。如果出现 OutOfMemoryError 错误,很可能是因为您以并不真正需要的方式持有对对象的引用,但这会阻止回收这些对象的内存。

The exact behavior here depends entirely on the JVM implementation. In spec (that is what a proper JVM implementation needs to provide you), it can happen in parallel, it can happen first before your code executes, or it can not happen at all.

In practice, my observation on JVMs I have happened to observe is that it runs immediately in a separate thread. However, in some cases multiple calls spawn multiple threads, sometimes it queues the requests on one thread. The Garbage collection launched was always a "stop-the-world" type (that is it was very complete and slowed or paused the application).

However, given your comment to @Chris Dail, your underlying problem isn't the behavior of a System.gc() call. Calling System.gc() can have some uses. It can be used clear memory so you can get a sense of how large the footprint of the application actually is currently. It can also be used as a strategy to ensure that a stop-the-world garbage collection happens earlier so that it "stops the world" for a shorter amount of time because there is less memory to clear. (I should note that as JVM's get more and more sophisticated this kind of thing is less and less necessary, and actually becomes counter-productive).

What it does not do however, is in any way solve an OutOfMemoryError. The JVM will not give you an OutOfMemoryError until it has garbage collected to the best of its ability. Calling System.gc does not change that. If you have an OutOfMemoryError it is likely because you are holding reference to objects in ways that you don't really need, but that are preventing those Objects memory from being reclaimed.

云淡风轻 2024-09-05 05:57:43

它通常是同步的,但不能保证。

实际上,这两者都不能保证回收会有效发生,因为 JVM 将决定它是否有意义以及使用哪种类型的垃圾回收。

如果您需要其他信息,可以使用 -verbosegc 标志启动程序。

在任何情况下,垃圾收集都是由 JVM 自动发出的,无需任何指定的调用,调用 System.gc () 只是您给出的一个提示,让它知道它可以启动一个集合。

It's usually synchronous, but not guaranteed.

Actually is neither guaranteed that a collection will effectively take place since the JVM will decide if it makes sense and what kind of garbage collection use.

If you need additional info you can launch your program with -verbosegc flag..

In any case the garbage collection is something that it issued automatically by the JVM without any specified calls, invoking System.gc() is just a hint you give to let it know that it may start a collection.

梦醒灬来后我 2024-09-05 05:57:43

正如 Eric Eijkelenboom 指出的那样,调用此方法并不意味着垃圾收集器将立即执行。实际上你根本不知道它是否会被调用......

在你的情况下(从 Amargosh 的评论来看)的答案)在执行需要大量 RAM 的操作之前,您需要释放一些内存,对吗?在这种情况下您无需担心。如果有足够的内存可供垃圾收集,当您尝试分配它时,它会自动完成。

As Eric Eijkelenboom pointed out, calling this method does not mean the the Garbage Collector will execute immediately. Actually you don't know if it will be called at all...

In your case (judging from the comment on Amarghosh's answer) you need to free up some memory before you do something that requires a lot of RAM am i right? In that case you don't need to worry. If there is enough memory to be garbage collected it will be done automatically when you try to allocate it.

趴在窗边数星星i 2024-09-05 05:57:43

垃圾收集的执行方式取决于 JVM 的实现。通常的实现可能会停止所有线程,以全局收集内存,例如仅停止一个线程以收集该线程的本地内存。从 Sun 以及这篇较旧但较旧的文章中了解有关垃圾收集的更多信息好文章

How the Garbage collection is executed is up to the JVM-implementation. Usual implementations may stop all threads, to collect memory globally, stop only one thread to collect memory local for this thread for instance. Read more about garbage collection from Sun and in this older but good article.

各自安好 2024-09-05 05:57:43

“执行其他任务”等待 System.gc() 执行并返回。

"Do some other task" waits for System.gc() to execute and return.

轻拂→两袖风尘 2024-09-05 05:57:43

没有办法知道。调用 System.gc() 并不意味着在那一秒执行垃圾收集,它只是被调度。由 JVM 在适当的时间执行垃圾收集。

There is no way to know. Calling System.gc() does not mean that a garbage collection is performed at that very second, it is merely scheduled. It is up to the JVM to perform the garbage collection at a suitabe time.

稚然 2024-09-05 05:57:43

javadoc 告诉 http:// /java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#gc()
当控制权从方法调用返回时,Java 虚拟机已尽最大努力从所有丢弃的对象中回收空间。

所以我会说“执行一些其他任务;”等待“System.gc();”被处决!

不使用并行性——这对于 Java 应用程序来说并不好。事实上,调用/强制垃圾收集器从来都不是一个好主意。它必须保留给极端情况,没有其他解决方案......

再见,
阿尔班.

the javadoc tells http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#gc() :
"When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects."

So I would say that "do some other tasks;" waits for "System.gc();" to be executed!

No parallelism is used - and that's not good for a Java application. In fact, it's never a good idea to call/force the Garbage Collector. It must be reserve to extreme cases with no other solutions...

Bye,
Alban.

浅沫记忆 2024-09-05 05:57:43

根据 Java 规范,调用 System.gc() 的线程将停止,直到 GC 完成其操作。请注意,这只是一个提示,JVM 可以随意忽略它,并且 Sun 的 JVM 实际上有一个命令行开关(-XX:-DisableExplicitGC)来实现此效果。使用此开关,System.gc() 不执行任何操作并立即返回。

不管怎样,规范中没有任何内容阻止其他线程运行。这取决于GC算法; Sun 的 JVM 包括多种,其中一些能够与应用线程同时运行以完成大部分工作。

正如其他人所指出的,在大多数情况下,显式调用 System.gc() 的最终效果是降低性能。如果您觉得必须调用该方法,那么很可能您做错了什么。

According to the Java specification, the thread which calls System.gc() is stopped until the GC has done its deed. Note that this is just a hint, the JVM is free to ignore it, and Sun's JVM actually has a command-line switch (-XX:-DisableExplicitGC) to that effect. With this switch, System.gc() does nothing and returns immediately.

Either way, nothing in the specification prevents other threads from running. This depends on the GC algorithm; Sun's JVM includes several, some of which being able to run concurrently with the applicative threads for most of their work.

As was pointed out by others, the net effect of calling System.gc() explicitly is, most of the time, to decrease performance. If you feel that you must call that method, then chances are that you are doing something wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文