java thread destory,DestroyJavaVM线程始终在运行

When profiling my application I came across a weird behavior - the DestroyJavaVM thread is ALWAYS running - 100% of the time.

o8POf.png

After doing a little research on the subject, on which there's hardly any valuable information online, all I understood is that this thread is supposed to unload the JVM upon exit.

If that's the case, why is this thread in RUNNING state 100% of the time from the very first moment I start my application? Doesn't it consume valuable resources and therefore may cause an OutOfMemoryError (like I sometimes get)?

Is there any official reference to what this thread actually does and what triggers its initialization?

Thanks

解决方案

This happens because most applications are run in threads.

All POJO apps start by invoking the main method. In it's most simple case this method will do all of the work, creating objects, calling methods etc. Once main completes, the JVM is told to shut down using a DestroyJavaVM thread which waits for all non-daemon threads to complete before doing it's work. This is to ensure that any non-daemon threads you create run to completion before the JVM is torn down.

An app with a GUI, however, normally runs as a number of threads. One for watching for system events such as keyboard or mouse events. One for maintaining the windows and display etc. The main method of this kind of app will probably just start up all the required threads and exit. It still creates the DestroyJavaVM thread but now all that does is wait for all of your created threads to finish before tearing down the VM.

As a result, any app that creates threads and relies solely on their functionality will allways have a DestroyJavaVM thread waiting for it to finish. Since all it is doing is joining all other running threads it does not consume any resources.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值