Why doesn't the JVM support forced unloading of a class / class?

Disclaimer: I know how classes are loaded in the JVM and how and when they are unloaded. The question is not the current behavior, the question is why the JVM does not support the "forced" unloading of the loader class / class?

It can have the following semantics: when the classloader is “force unloaded”, all loaded classes are marked “unloaded”, which means that no new instances will be created (an exception will be thrown, for example, “ClassUnloadedException”). Then, all instances of such unloaded classes are marked as "unloaded", so each access to them will raise an InstanceUnloadedException (like NullPointerException).

Implementation: I think it could be done while garbage collection. For example, the compacting collector still moves live objects, so it can check whether the class of the current object was "unloaded", and instead of moving the object, the link refers to a protected memory page (access to it will result in the aforementioned InstanceUnloadedException). It will also effectively make the object garbage. Or perhaps this can be done during the GC “label” phase. In any case, I think that it is technically possible, with little overhead when there is no "unloading".

Rationale: Such a "hardcore" mechanism can be useful for runtime, in which a lot of dynamic code reloads, and the failure of a particular application or part of it is acceptable, while the failure of the entire JVM is undesirable. For example, application servers and battery life OSGi.

In my experience, dynamic redistribution in 9 out of 10 cases leads to PermGenSpace due to incorrect cleaning of links (for example, ThreadLocal in a static field filled with a long-living stream, etc.). In addition, the presence of an explicit exception instead of a hard leak can help polish the code, so no links fall into the area of ​​long-lived uncontrolled ones.

What do you think?

+3
source share
1 answer

. , Thread.stop(), .

Thread.stop() - , . , , , . .

, . , . , try, -, . ClassUnloadedException , . , , , - ClassUnloadedException. finally, , ClassUnloadedException , . , , ClassUnloadedException.

, NullPointerException . - . , . ClassCastException, IllegalStateException, IllegalArgumentException RuntimeExceptions (, , ) . , RuntimeException, (, IOException), .

, StackOverflowError, OutOfMemoryError, ExceptionIninitializerError NoClassDefFoundError , , -, . - , . , , , , , . ClassUnloadedException - , ClassUnloadedError. ExceptionIninitializerError NoClassDefFoundError, 99% , , , , , .

- , JVM/Container, , , , , . , . , , , . (.. ), . , , , , , . , ClassUnloadedError . , - , , , , (.. ).

, ClassUnloadedError , , , , . , , .

GC . , , . , , , , /. , , , ​​.

, , . , .

, - Thread.stop() - NoClassdefFoundError, , . - , , . .

+3

Source: https://habr.com/ru/post/1787870/


All Articles