Why does java allow only dynamic linking?
Asked Answered
A

3

11

I was following a tutorial video on compilers on YouTube, when I came across the fact that the C Programming Language is faster because it allows both static linking and dynamic linking, but Java allows only dynamic linking and that is why C is much faster than Java.

My question is, if static linking makes a program run faster, why was it not included in Java? I know there must be some real good reason why this decision was taken by the developers of Java to not include static linking, I just want to know what are the reasons.

Note : I do not know if this question already has an answer on SO, but since I could not find one, so I posted. If the answer does already exist, please provide a link to it.

Note : The link to the tutorial provided is in Hindi Language. Sorry about that.

Airlia answered 4/12, 2015 at 9:28 Comment(15)
that is why C is much faster than Java - really?Streamline
Apart from what Andreas said - why would the support of static linking make a programming language "faster" overall?Implead
@AndreasFester, that is what the tutorial that I am following says? Is it a wrong statement?Airlia
@mikolak, am I really following a wrong tutorial with wrong information? :OAirlia
Related: #1993890Tamratamsky
Performance of C vs. Java is much discussed and highly disputed, so I would not classify that as a statement but as an opinion that has to be looked at in the context it was made.Kriegspiel
@SajibAcharya : hard to say, I don't know Hindi :). But it sounds like the tutorial might misrepresent some facts and/or confuse some definitions. The best judge on that is yourself, see e.g. this related answer on the benefits of static vs dynamic linking in C++ and decide for yourself.Implead
@SajibAcharya The general answer to performance related question is: "it depends.". There are much more aspects which affect performance of an application than static versus dynamic linking (like proper selection of algorithms and data structures). Certainly dynamic linking is slower since it requires additional lookups, but when does it matter?Streamline
@mikolak, sorry about the language problem, what the tutor really is saying as translated in english would be - "Since C supports static linking, the memory consumption at runtime would be more but will run faster, but since Java supports dynamic linking, it would run slower since the linking occurs at runtime." Then he concludes several times that "Thus C is much faster than Java". Is this statement wrong?Airlia
@AndreasFester, so should I take the statement that "Java is slower than C because it uses dynamic linking" to be wrong?Airlia
"Thus C is much faster than Java". Is this statement wrong? - yes, as it stands it is definitely wrong. You can not make such a statement without additional information about the concrete scenario. What he probably meant is that the startup phase is faster - but again, first that might be correct for simple applications but different for complex ones, and second you need to decide whether it matters (as startup only occurs once when you launch the application). Then, this might be an issue for Desktop applications, but less an issue for server applications.Streamline
See also https://mcmap.net/q/1014786/-java-faster-than-c-closed, https://mcmap.net/q/1014787/-java-faster-than-c-duplicateStreamline
Don't use quote formatting for text that isn't quoted. Don't use code formatting for text that isn't code.Sergei
@AndreasFester, thank you for the information. :) So dynamic linking is used in Java just for the "Code once, run everywhere" feature?Airlia
@EJP, right. will keep that in mind. sorry about that.Airlia
K
13

Java does not include a linker step at compile time. With Java 9 there will be a tool (jlink: JEP 275, JavaOne Talk on Project Jigsaw) which will create an image that will have the dependencies linked in.

One of the main goals of Java when it was created was "Code once, run everywhere". Statically linking environment dependent libraries or code parts in will negate this feature.

Kriegspiel answered 4/12, 2015 at 9:42 Comment(5)
To be pedantic: Java does include a linker step which is what the resolve parameter in class loading indicates ;) In particular, if you write naive code that crosses classloader boundaries you will encounter exceptions from the linker phase of class loading because classloaders also act as namespaces. (Meaning the name com.acme.A in classloader C1 is different from the name come.acme.A in classloader C2.)Counteraccusation
So, the only reason to go for dynamic linking is for the "Code once, run everywhere" feature, or is there something else too? Just asking.Airlia
@SajibAcharya No, it's just plain wrong. Conceptually, nothing prevents anybody from "statically linking" Java programs. Or if something does, this answer does not explain why.Ignorance
I don't understand how static linking negates "Code once, run everywhere". There are languages(Go, Rust etc.) using static linking which can be containerized and run even with distroless or scratch images. Java is dynamically linked, has the slogan of "Code once, run everywhere", but it still requires JRE to run.Lumbering
@Lumbering I'm not a compilers expert so this is just my reasoning here - Java is compiled to .class files (so called byte code) and not to assembly (machine code). Those class files could then be distributed and interpret in any JVM in any OS, yielding the same result. On the opposite side the compilation from C code to machine code means translating the source code to a very specific instructions that are not reusable in different OSs or architectures.Tod
T
5

Static linking in Java would force the compiler to add the whole JRE libraries in each application, which would be a waste of memory and it would break the compile once/run anywhere advantage of Java.

Tamratamsky answered 4/12, 2015 at 9:54 Comment(0)
C
5

C is not faster because of static versus dynamic linking. That is a red herring.

Java loading times are typically poor because the VM and standard library are comparatively huge, which means there simply is a lot to load.

Indeed, dynamic linking can be used to speed up initial loading times because with dlopen() and friends you control when that loading happens instead of paying the cost upfront (during loading).

Within this context, in general the one major difference in performance is in memory, in particular the ability to control layout of memory to some extent in C. That can yield substantial benefits because with less fragmentation and by reducing things down to 'cache size' CPU caching and speculative hardware optimisations (like prefetching) work much better.

Counteraccusation answered 4/12, 2015 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.