将调用System.exit(0);来自主运行垃圾收集之外的对象? [英] Will calling System.exit(0); from an object outside of main run garbage collection?

查看:132
本文介绍了将调用System.exit(0);来自主运行垃圾收集之外的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划使用一个由我的main方法调用的对象来退出整个程序。该对象有一个方法,它只运行一个 System.exit(0)。我的问题是,这是一件安全的事情吗? 如果我从另一个对象运行 System.exit(0),垃圾收集是否仍然会从内存中清除整个程序,或者我在清理调用类时遇到问题从内存中?我的想法是,因为JVM将被终止,调用类将被垃圾回收,或者我可能有问题清洗调用类从内存中,因为对象的堆栈框架在主栈之上帧。这主要是一个我还没有足够了解Java的问题......感谢您的帮助!

I plan to use an object which is called by my main method to exit the entire program. The object has a method which just runs a System.exit(0). My question is, is this a safe thing to do? If I run System.exit(0) from another object, will garbage collection still clean out the entire program from memory, or will I have issues cleaning the calling class from memory? My thoughts are that either since the JVM will be terminated, the calling class will be garbage-collected, or that I might have issues cleaning the calling class from memory since the object's stack frame is above the main stack frame. It's mainly an issue of me not yet knowing enough about Java... Thanks for any help!

推荐答案

System.exit()是一个静态函数,因此它在哪里调用并不重要。效果与终止Java虚拟机的效果相同。 JVM在终止时使用的任何资源都会返回给操作系统。

System.exit() is a static function therefore it doesn't matter where you call it. The effects are the same which is terminating the Java Virtual Machine. Any resources used by the JVM upon termination are given back to the OS.

来源: http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#退出(int)

这篇关于将调用System.exit(0);来自主运行垃圾收集之外的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆