I have three modules: module-a, module-b, module-c. Module-a and module-b are in boot layer. Layer for module-c I create myself. Module-c has JPMS implementation of the service which interface is in module-a.
This is the way I create layer with module-c in module-b.
ModuleFinder finder = ModuleFinder.of(moduleCPath);
ModuleLayer parent = ModuleLayer.boot();
Configuration cf = parent.configuration().resolve(finder, ModuleFinder.of(), Set.of("module-c"));
ClassLoader scl = ClassLoader.getSystemClassLoader();
ModuleLayer layer = parent.defineModulesWithOneLoader(cf, scl);
Then in module-b I call service from module-c. After service execution completed I don't need module-c and new created layer any more. How to remove it from JVM and release all resources? Is it enough to do layer = null;
?
Defining the Java 9 module. A module is a collection of code, data, and resources. It is a set of related packages and types (classes, abstract classes, interfaces, and more) with code, data files, and some static resources.
module-info. java file. It declares the dependencies within the module system and allows the compiler and the runtime to police the boundaries/access violations between the modules in your application.
xml files and the Maven directory project structure. The module descriptor ( module-info. java ) needs to be located in the src/main/java directory.
The keywords exports , module , open , opens , provides , requires , uses , with , as well as to and transitive , which we introduce later, are restricted keywords. They're keywords only in module declarations and may be used as identifiers anywhere else in your code. requires.
The module layer, the modules in the layer, and class loaders supporting the layer, are eligible to be GC'ed/unloaded when they are no longer reachable.
If you want to prove this to yourself then create a weak reference to the layer
object and you should see that the reference is cleared (and queued if you are using a reference queue) when the layer is GC'ed.
An EMPTY_LAYER
shall solve your use-case(from one of the comments on the question, trying to assign new HashSet<>
as roots) here, wherein the references to other layers no more handled within the layer
that you created :
layer = ModuleLayer.empty();
Returns the empty layer. There are no modules in the empty layer. It has no parents.
On the thought of being able to remove a layer explicitly form the JVM, I would not probably expect such an API exposed publicly since a JVM is supposed to have at least one non-empty layer, the boot layer, that is created when the Java virtual machine is started.
And if such a method is exposed, I wonder if users can try and remove this layer as well. Though I am trying to be technically hypothetical on this part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With