Why JAVA uses alot more Memory compared to C/CPP/Python ??

Hey Guys,I just successfully submitted my first Solution at first try to a rather very easy Problem.
What I noticed was that my java code took 1273.5 M(units?), where as the code of C/CPP/python were around 1.8 to 8.5 M…this shocked me a bit. So here i am asking you…whats under the hood of JAVA and how can I improve on making it low on memory as low as possible.

P.S: I assure u,that the code i have wrote is optimal.
here’s the link: https://www.codechef.com/viewsolution/14487812
also the link to others code : https://www.codechef.com/status/CDBRKS5

Hey Guys!!

I actually found the answer to my on Question,So I thought I’ll share it here.
I researched and found that Java is purely OO(object oriented)
and every code written in is supposed to be in a class or package. This ensures its OO behavior.
So for even basic functionalities java implicitly imports few packages to keep things working without scaring the programmers away.
One of them is java.lang package and few other packages as well.Also if u dont mention a package name for your piece of code, java implicitly builds it up to a package and includes it under a default package name(Again for the OO).
So actually the size of all these packages must be around 510.073 kb(This is what codechef ide shows for very minimal program).And all my basic programs are around 512.073 kb and 515.073 kb.

So in a way Java uses almost the same amount of memory for the actual code you write as compared to c/cpp/python.It Just does the implicit imports to keep the Java Engine Goin for its pure OO.
(JVM and Java is Godsent!) ;p

Hope this Helps,and if there’s any correction please comment. I’ll be a very happy man to learn something.

1 Like

@stabcode_77, I don’t think that is the correct explanation. As you may know, Java runs on the Java Virtual Machine (JVM) instead of directly on the system. Hence during the execution of the program, the system first sets up the JVM. On Codechef, the JVM is configured to have a maximum heap size of 1280 MB according to this answer here. You can see the -Xmx1280M compiler flag being used to do this. You can refer to this documentation to verify what -Xmx does.
So this is why the memory usage of Java programs is usually around 1280 MB. Hope this answers your query :slight_smile:

2 Likes

Ohhh Damn!!
Thanks Alot man…i like this under the hood stuff.
And Thanks For all these links!!
(i dont have enought rep to up vote your answer,but consider a thanks):slight_smile:

stabcode, you can “accept” his answer to provide him points by clicking on the “tick-in-circle”. Accepting answers also gives you points for analyzing and accepting someone else’s answer.

ohh thanks for the tip!

@stabcode_77, you’re welcome! I was confused by the same thing once :slight_smile: