javajarclassloaderdynamic-loadingdynamic-class-loaders

Does the default classloader in java keep everything in memory or leave in file system?


I wonder when you have a jar, dynamically loaded in your running program, do all the content (classes, resources) in the jar be stored somewhere in the VM memory?

So that everytime you need to access the class or a text resource file (e.g. via getClass().getResourceAsStream), it can be pulled quickly from memory rather than to access the jar in file system again? i.e. you can delete the jar after that.

Sorry but if I am asking a wrong question, please change my question into explaining the classloader process as I may misunderstood it, in particular for the resources packed in the jar. Thank you.


Solution

  • No, normally resources are not loaded from disk until you access them.

    Classes are loaded into memory and stay in memory after their first use. Resources are not cached at the JVM or JRE level - every time you call getResourceAsStream you get a new stream from disk.