Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much of a jar file gets loaded into the memory

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?

like image 976
CodeChief Avatar asked Dec 28 '18 11:12

CodeChief


People also ask

Are JAR files compressed?

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.

What is the contents of a JAR file?

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.

How do I know if a jar is loaded?

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.


1 Answers

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

like image 165
nap.gab Avatar answered Sep 22 '22 18:09

nap.gab