I am programming with scala, when I write a simple "println("hello world")" program and use 'sbt package' to publish it into a jar file the file is only 4kb in size but that doesn't help me since the jar file won't run without the scala library. So after searching online I found sbt-assembly which packages everything including the scala library into one "fat jar" but the resulting jar file is over 13Mb in size So my question is when jar files get executed or accessed does the entire jar file get loaded into memory, or will only the required data and functions get loaded into the memory as needed?
Items stored in JAR files are compressed with the standard ZIP file compression. Compression makes downloading classes over a network much faster. A quick survey of the standard Java distribution shows that a typical class file shrinks by about 40 percent when it is compressed.
The JAR file contains the TicTacToe class file and the audio and images directory, as expected. The output also shows that the JAR file contains a default manifest file, META-INF/MANIFEST. MF, which was automatically placed in the archive by the JAR tool.
There are few ways to do this. Use –verbose:class flag with your java command line. This option enables loading and unloading of classes. It shows the jar file from which the class is loaded.
This behavior does not depend on how the application was packaged, but it is up to the class loader (as the name suggests) to load and unload classes from memory.
The standard behavior of the default class loader is to load a class into memory as soon as it is invoked the first time at runtime and it is unload only when the class loader is garbage collected.
Of course, you can define your custom class loader and do what you want :)
If you want to check loaded classes at runtime, you can use a tool like VisualVM to dump the heap and check what's happening in your memory
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