Quick revision
The short answer
A class loader per plugin, with the shared API in a parent loader, isolates dependencies cheaply but gives no memory, CPU or crash isolation and leaks easily on unload. JPMS module layers add checked configuration and allow different versions of a module in different layers, but only for modular plugins. Running plugins out of process behind an RPC contract is the only option that isolates resources and failures, and since the Security Manager is permanently disabled from JDK 24 it is also the only place for untrusted code.
What to remember
In this answer, we separated isolation into dependencies, resources, failures, security and lifecycle. A loader per plugin isolates dependencies cheaply but shares everything else, module layers add checked configuration and per-layer module versions for modular plugins, and out-of-process plugins give real resource and crash isolation at the cost of latency and operations. With the Security Manager disabled from JDK 24, untrusted code has to run out of process, in-process designs must manage loader leaks carefully, and every design needs an explicitly versioned, compatibility-tested API.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A class loader per plugin, with the shared API in a parent loader, isolates dependencies cheaply but gives no memory, CPU or crash isolation and leaks easily on unload. JPMS module layers add checked configuration and allow different versions of a module in different layers, but only for modular plugins. Running plugins out of process behind an RPC contract is the only option that isolates resources and failures, and since the Security Manager is permanently disabled from JDK 24 it is also the only place for untrusted code.
Why this question is asked
Platforms that let many teams extend them, such as integration hubs, rule engines and IDE-like tools, all face the same choice: load the extensions into the host JVM, or run them somewhere else. The choice decides how fast plugins can be called, how much one bad plugin can hurt everyone, and how much operational work the platform team carries. Interviewers ask it at senior level because a good answer weighs mechanisms against failure modes rather than picking a favourite. In this answer, we will set out what isolation actually means, look at each of the three designs and what it costs, compare them side by side, and then go through the concerns that apply whatever we choose: unloading leaks, security and API versioning.
1What Isolation Has to Cover
Isolation is not one property but several. Dependency isolation means plugin A can use library version 1 while plugin B uses version 2. Memory and CPU isolation means a plugin that allocates without limit or spins in a loop cannot starve the others. Failure isolation means a plugin that crashes, deadlocks or triggers an OutOfMemoryError does not take the host down. Security isolation means a plugin cannot read files, secrets or other tenants' data it should not see. And lifecycle isolation means we can install, upgrade and remove one plugin without restarting the host.
2Option 1: A Class Loader per Plugin
The classic design gives each plugin its own loader, for example a URLClassLoader over the plugin's jars, with the host's loader as parent. The plugin API, a set of interfaces and data types, lives only in the host, so delegation makes every plugin see the same API types and the host can call plugins directly. Each plugin's own libraries live only in its loader, so two plugins can carry different versions of the same JSON library.
This is cheap and fast: one JVM, direct method calls, shared JIT-compiled host code. But all plugins share one heap, one set of threads and one process. A plugin that fills the heap causes OutOfMemoryError for everyone, a plugin that calls System.exit or crashes native code ends the process, and there is no per-loader memory or CPU limit in the JVM. Unloading also depends on every reference to the loader disappearing, which, as we will see, is fragile.
3Option 2: JPMS Module Layers
The Java module system lets us create a ModuleLayer, a set of modules resolved together and mapped to class loaders, on top of the boot layer. Each plugin, or each group of plugins, gets its own layer.
Compared with raw loaders, layers add reliable configuration: resolution fails at install time if a required module is missing or two modules contain the same package, instead of failing later with NoClassDefFoundError. Encapsulation is enforced, so a plugin can reach only packages the host's API module exports. And because a package may appear only once per layer but layers are separate, two layers can each hold a different version of the same module. The cost is that plugins and their libraries must be proper modules, or at least work as automatic modules, and many libraries still have split packages or reflection habits that fight strong encapsulation. Layers still share one process, so the resource and failure picture is the same as option 1.
4Option 3: Plugins Out of Process
The third design runs each plugin, or each team's plugins, in a separate JVM or container, and the host talks to it over a contract such as gRPC or HTTP with a versioned schema. Now each plugin has its own heap limit (-Xmx and a container memory limit), its own CPU quota, its own crash domain and its own file system view. A plugin that leaks memory is restarted by the orchestrator without touching the host, and an upgrade is a normal deployment.
The costs are real. A call that took nanoseconds now takes a network round trip, typically somewhere around 0.2 to 2 milliseconds inside one data centre, plus serialization. Plugins can no longer share object graphs with the host, so the API must be designed around messages. And the platform team now operates many processes, with health checks, scaling, logging and version skew between host and plugin.
| Property | Loader per plugin | Module layers | Out of process |
|---|---|---|---|
| Dependency versions | isolated | isolated, checked at resolve | isolated |
| Memory and CPU limits | none | none | per process or container |
| Crash and OOM isolation | none | none | yes |
| Untrusted code | not safe | not safe | safe with OS sandboxing |
| Call cost | method call | method call | RPC, about 0.2 to 2 ms |
| Unload | fragile, leaks | fragile, leaks | stop the process |
| Plugin requirements | jars | modular jars | a service and a contract |
5Security Without the Security Manager
In the past, in-process plugins could be sandboxed with the Security Manager, which checked permissions for file, network and reflection access. It was deprecated for removal in JDK 17 and is permanently disabled from JDK 24, so there is no longer any JVM mechanism to stop in-process code from reading files, opening sockets or using reflection on host objects. Module encapsulation stops accidental access to internals but is not a security boundary, because a plugin can still do anything the process can do with the JDK's public APIs.
The consequence is simple: untrusted or customer-supplied code belongs out of process, inside an operating system sandbox such as a container with a restricted user, no secrets mounted and limited network access. In-process loading is acceptable only for code we trust as much as the host itself.
6Unloading Leaks in In-Process Designs
Whether we use loaders or layers, unloading a plugin works only if nothing reachable still refers to its loader, classes or objects. In practice, plugins leak through threads they started and never stopped, ThreadLocal values left on host pool threads, listeners and caches registered in static host registries, JDBC drivers registered in DriverManager, shutdown hooks, MBeans, and logging frameworks that cache classes. Each leak keeps a whole loader with all its classes in metaspace, so a platform that upgrades plugins often slowly runs out of metaspace.
A platform that unloads in-process must therefore own the lifecycle: give each plugin a context object through which it registers listeners and executors, so the host can remove them all on stop; run plugin work on host-managed executors; and test unloading by loading and unloading a plugin many times in CI while watching the loader count.
7Versioning the Plugin API and Choosing
Whichever design we choose, the plugin API is a public contract. We version it explicitly, keep old versions working for a documented period, add rather than change methods (default methods help for in-process interfaces, optional fields for RPC schemas), and run a compatibility suite that loads last year's plugins against each new host release.
For a platform where many internal teams ship plugins into one long-running JVM, I would start with a loader or layer per plugin only for trusted, performance-sensitive plugins with a strict lifecycle API, and run everything else, and anything third-party, out of process. If the platform's main risk is one team's bug taking down everyone, that alone argues for out of process as the default, with in-process as an exception that must be justified by measured latency needs.
8Follow-up questions
How would you limit a misbehaving in-process plugin's memory use?
The JVM has no per-loader heap limit, so there is no hard limit inside one process. What we can do is soften the risk: run plugin work on dedicated executors so we know which threads belong to which plugin, use com.sun.management.ThreadMXBean.getThreadAllocatedBytes to measure allocation per plugin thread and disable plugins that exceed a budget, bound queues and caches in the API we expose, and set timeouts on calls. For a hard limit, the plugin must move to its own process or container, where -Xmx and the container memory limit apply.
Can a plugin crash the host even with separate loaders?
Yes. An OutOfMemoryError, a StackOverflowError in a shared thread, a call to System.exit, a native crash in a JNI library, or simply a deadlock on a host lock all affect the whole process. Separate loaders separate types, not failures.
9Conclusion
In this answer, we separated isolation into dependencies, resources, failures, security and lifecycle. A loader per plugin isolates dependencies cheaply but shares everything else, module layers add checked configuration and per-layer module versions for modular plugins, and out-of-process plugins give real resource and crash isolation at the cost of latency and operations. With the Security Manager disabled from JDK 24, untrusted code has to run out of process, in-process designs must manage loader leaks carefully, and every design needs an explicitly versioned, compatibility-tested API.
Open with
I would first ask what kind of isolation we need: just dependency versions, or also memory, CPU, crashes and security. A class loader per plugin, with the API in a parent loader shared by all, gives dependency isolation cheaply and calls are plain method calls, but all plugins share one heap and process, so one plugin can exhaust memory, block threads or crash the JVM for everyone.
Then, if they want more
JPMS module layers improve on that with reliable configuration, so missing modules or duplicate packages fail at install time, with real encapsulation, and with different versions of a module in different layers, but plugins must be modular, and resources and failures are still shared.
Running plugins out of process, in separate JVMs or containers behind a versioned RPC contract, gives real memory, CPU and crash isolation and clean upgrades, at the cost of a network hop per call and more to operate.
Security settles part of it: the Security Manager is permanently disabled from JDK 24, so the JVM cannot sandbox in-process code, and anything untrusted must run out of process.
For in-process plugins I would own the lifecycle so unloading does not leak through threads, ThreadLocals, static registries or JDBC drivers, and I would version the plugin API explicitly with a compatibility test suite. My default would be out of process, with in-process only for trusted, latency-critical plugins.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 24, Java 17
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design plugin isolation for a Java platform where many teams ship plugins into one...": https://scrivio-interview-library.netlify.app/java/class-loading/cf2d36ca0e/