How much of a jar file gets loaded into the memory
Asked Answered
G

1

8

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?

Genome answered 28/12, 2018 at 11:46 Comment(0)
T
5

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

Trovillion answered 28/12, 2018 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.