10

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
Sajib Acharya
  • 1,666
  • 5
  • 29
  • 54
  • 8
    `that is why C is much faster than Java` - really? – Andreas Fester Dec 04 '15 at 09:31
  • 2
    Apart from what Andreas said - why would the support of static **linking** make a programming language "faster" **overall**? – mikołak Dec 04 '15 at 09:31
  • @AndreasFester, that is what the `tutorial` that I am following says? Is it a wrong statement? – Sajib Acharya Dec 04 '15 at 09:32
  • @mikolak, am I really following a wrong tutorial with wrong information? :O – Sajib Acharya Dec 04 '15 at 09:33
  • 1
    Related: http://stackoverflow.com/questions/1993390/static-linking-vs-dynamic-linking – Pablo Lozano Dec 04 '15 at 09:35
  • 4
    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. – hotzst Dec 04 '15 at 09:36
  • 1
    @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++](http://stackoverflow.com/questions/1993390/static-linking-vs-dynamic-linking) and decide for yourself. – mikołak Dec 04 '15 at 09:36
  • @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? – Andreas Fester Dec 04 '15 at 09:38
  • @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? – Sajib Acharya Dec 04 '15 at 09:39
  • @AndreasFester, so should I take the statement that "Java is slower than C **because** it uses dynamic linking" to be wrong? – Sajib Acharya Dec 04 '15 at 09:41
  • 4
    `"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. – Andreas Fester Dec 04 '15 at 09:43
  • See also http://stackoverflow.com/questions/29662971/java-faster-than-c, http://stackoverflow.com/questions/18834092/java-faster-than-c – Andreas Fester Dec 04 '15 at 09:46
  • Don't use quote formatting for text that isn't quoted. Don't use code formatting for text that isn't code. – user207421 Dec 04 '15 at 09:51
  • @AndreasFester, thank you for the information. :) So dynamic linking is used in Java just for the "Code once, run everywhere" feature? – Sajib Acharya Dec 04 '15 at 10:18
  • @EJP, right. will keep that in mind. sorry about that. – Sajib Acharya Dec 04 '15 at 10:19

3 Answers3

12

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.

hotzst
  • 7,238
  • 9
  • 41
  • 64
  • 1
    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.) – user268396 Dec 04 '15 at 09:51
  • 1
    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. – Sajib Acharya Dec 04 '15 at 10:22
  • 1
    @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. – cubuspl42 Oct 30 '18 at 19:08
  • 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. – Hmerac Nov 05 '21 at 15:38
4

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.

Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
4

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.

user268396
  • 11,576
  • 2
  • 31
  • 26