16

Why does the jvm require around 10 MB of memory for a simple hello world but the clr doesn't. What is the trade-off here, i.e. what does the jvm gain by doing this?

Let me clarify a bit because I'm not conveying the question that is in my head. There is clearly an architectural difference between the jvm and clr runtimes. The jvm has a significantly higher memory footprint than the clr. I'm assuming there is some benefit to this overhead otherwise why would it exist. I'm asking what the trade-offs are in these two designs. What benefit does the jvm gain from it's memory overhead?

4
  • 1
    How do you know it's overhead? Did you make a skeleton program in each and run them? I can only get the JVM to take around 7 mb at startup (according to Task Manager), even with -Xmx256m -Xms256m.
    – Michael Myers
    May 12, 2009 at 21:58
  • Can you give us a test class you used in both? This will help to reproduce the problem and therefore give good answers. May 14, 2009 at 19:16
  • I don't think my question was clear so I just edited it. For a test class you a simple HelloWorld app.
    – jshen
    May 14, 2009 at 23:31
  • @mmyers A simple hello world uses 13mb in java on my machine which is significantly more than any other language I use. Ruby for example uses 1.4mb and common lisp is something like 3mb. I don't have mono on this machine, but it was only a couple of mb. I'm assuming that the jvm uses so much more memory for a reason and am asking what that reason is.
    – jshen
    May 14, 2009 at 23:43

6 Answers 6

6
+150

I guess one reason is that Java has to do everything itself (another aspect of platform independence). For instance, Swing draws it's own components from scratch, it doesn't rely on the OS to draw them. That's all got to take place in memory. Lots of stuff that windows may do, but linux does not (or does differently) has to be fully contained in Java so that it works the same on both.

Java also always insists that it's entire library is "Linked" and available. Since it doesn't use DLLs (they wouldn't be available on every platform), everything has to be loaded and tracked by java.

Java even does a lot of it's own floating point since the FPUs often give different results which has been deemed unacceptable.

So if you think about all the stuff C# can delegate to the OS it's tied to vs all the stuff Java has to do for the OS to compensate for others, the difference should be expected.

I've run java apps on 2 embedded platforms now. One was a spectrum analyzer where it actually drew the traces, the other is set-top cable boxes.

In both cases, this minimum memory footprint hasn't been an issue--there HAVE been Java specific issues, that just hasn't been one. The number of objects instantiated and Swing painting speed were bigger issues in these cases.

5

I don't know if initial memory footprint or a footprint of a Hello World application is important. A difference might be due to the number and sizes of the libraries that are loaded by the JVM / CLR. There can also be an amount of memory that is preallocated for garbage collection pools.

Every application that I know off, uses a lot more then Hello World functionality. That will load and free memory thousands of times throughout the execution of the application. If you are interested in Memory Utilization differences of JVM vs CLR, here are a couple of links with good information

http://benpryor.com/blog/2006/05/04/jvm-vs-clr-memory-allocation/

Memory Management Case study (JVM & CLR)

Memory Management Case study is in Power Point. A very interesting presentation.

2
  • I'm clearly not stating my question clearly. I'm not interested in memory utilization differences. I'm trying to find out why the jvm requires significantly more memory to start up than essentially every other programming language runtime I've used. I'm assuming there is a reason for it, and I'm curious what it is.
    – jshen
    May 15, 2009 at 5:30
  • I doubt that was too intentional. The real power of a virtual machine based language is shown when the application implements business logic and is ran over a period of time. The startup parameters (Initial Memory Utilization and Startup Time) are generally considered secondary importance. One thing I can think of is the initial number of libraries that are loaded on startup is different between JVM and CLR mightbe different. JVM might load more upfront vs CLR loading more on demand. In the longer time, that doesn't make much difference May 15, 2009 at 17:59
5

Seems like java is just using more virtual memory.

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
amwise   20598  0.0  0.5  22052  5624 pts/3    Sl+  14:59   0:00 mono Test.exe
amwise   20601  0.0  0.7 214312  7284 pts/2    Sl+  15:00   0:00 java Program

I made a test program in C# and in Java that print the string "test" and waits for input. I believe that the resident set size (RSS) value more accurately shows the memory usage. The virtual memory useage (VSZ) is less meaningful.

As I understand it applications can reserve a ton of virtual memory without actually using any real memory. For example you can ask the VirtualAlloc function on Windows to either reserve or commit virtual memory.

EDIT:

Here is a pretty picture from my windows box: alt text http://awise.us/images/mem.png

Each app was a simple printf followed by a getchar. Lots of virtual memory usage by Java and CLR. The C version depends on just about nothing, so it's memory usage is tiny relatively.

I doubt it really matters either way. Just pick whichever platform you are more familiar with and then don't write terrible, memory-wasting code. I'm sure it will work out.

EDIT:

This VMMap tool from Microsoft might be useful in figureing out where memory is going.

3
  • 2
    RSS of an idle process isn't a great measure either. May 12, 2009 at 22:50
  • I'm not trying to decide which platform is better for me to use. I'm curious why they made the design decision they did on the jvm side, and what they gained from it.
    – jshen
    May 14, 2009 at 23:46
  • All memory usage in a process space is virtual (at least in windows). On a 32bit OS the address space is divided into 2GB Code and 2GB data. Total Private bytes refers to the amount allocated in the data address space of the application. The OS manages how this allocation relates to physical memory. May 16, 2009 at 1:01
2

The JVM counts all its shared libraries whether they use memory or not.

Task manager is rather unreliable when it comes to reporting the memory consumption of programs. You should take it as a guide.

1
  • 1
    The other day my 4 GB Vista machine ran out of memory despite appearing to show less than 1 GB used by applications. Feb 5, 2009 at 23:13
2

JVM loads lots of unnecessary core classes on each run from rt.jar. Unfortunately, the inner-cross dependencies (java.lang <-> java.io) of java packages make it hard to do a partial runtime init. Not to mention the rt.jar itself is over 40MB, needs lots of time for lookup and decompress.

Post Java 6u10 seems to load things a bit smarter (it has a jqs.exe = java quick starter service to keep necessary data in memory and do a faster startup), still Java 7 is told to be better.

The Process Explorer in Windows reports the Private Bytes correctly (Private bytes are those memory regions, which are not shared by any dll).

A slightly bigger annoyance is that after 10 years, JVM still defaults to 64MB memory usage. It is really annoying to use -Xmx almost every time and cannot run demanding programs in jars with a simple double click (unless I alter the file extension assignment's command).

1

CLR is counted as part of the OS so the task manager doesn't report it's memory consumption under the application process.

6
  • If I use mono on a unix it's still uses only a few megs compared to 30 for the jvm
    – jshen
    Feb 5, 2009 at 19:53
  • 2
    The CLR starts inside each process, so I doubt this answer is correct.
    – Austin
    May 12, 2009 at 21:51
  • 1
    Austin: There is a CLR instance in each of process. With both CLR and JRE read-only code is shared between processes. The question is: Is that code counted for each process, or for no processes. May 12, 2009 at 22:11
  • Ah, I have heard about the code in a DLL being loaded into memory once and then used by several processes. When they are mapped into the process they would defiantly count against the process's virtual memory usage.
    – Austin
    May 12, 2009 at 22:21
  • 1
    I don't see why a DLL being part of the operating system would change the memory usage. They get loaded with LoadLibrary just like any other DLL.
    – Austin
    May 13, 2009 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.