What's the definition of “during the operation of the Java Virtual Machine”?

风流意气都作罢 提交于 2019-12-10 21:55:29

问题


From JVMS chapter 6.3:

[...] any of the VirtualMachineError subclasses defined below [InternalError, OutOfMemoryError, StackOverflowError, UnknownError] may be thrown at any time during the operation of the Java Virtual Machine

How does the JVMS define the phrase "at any time during the operation of the Java Virtual Machine"?

How do current JVMs interpret that phrase?

Specifically, does it mean that the four errors, java.lang.InternalError, java.lang.OutOfMemoryError, java.lang.StackOverflowError, and java.lang.UnknownError, may be thrown between statements? :

// ....
A(); B(); C();
try {
     // nothing
} catch (InternalError | OutOfMemoryError | StackOverflowError | UnknownError e) {
     // may occur?
}
D(); E(); F();
try {
     ; // semi-colon
} catch (InternalError | OutOfMemoryError | StackOverflowError | UnknownError e) {
     // may occur?
}
G(); H(); I();
try {
     ; ; ;;  ;;;;; ; ; ;;; ;; ;; ;; ;; ; ;; ; ;; // ... semi-colons
} catch (InternalError | OutOfMemoryError | StackOverflowError | UnknownError e) {
     // may occur?
}
J(); K(); L();
// ....

回答1:


The Java Virtual Machine consists of many parts. For example, the garbage collector runs as a persistent background Thread. It might throw one of those Exception and it might certainly appear to occur at any time (especially if your own code is stopped due to gc)!

From Java Garbage Collection Basics

What is Automatic Garbage Collection?

Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. An in use object, or a referenced object, means that some part of your program still maintains a pointer to that object. An unused object, or unreferenced object, is no longer referenced by any part of your program. So the memory used by an unreferenced object can be reclaimed.

tl;dr

Yes. They could be thrown between statements.



来源:https://stackoverflow.com/questions/27679158/whats-the-definition-of-during-the-operation-of-the-java-virtual-machine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!