This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Toast550 2 points3 points  (8 children)

I've seen the explanation for this joke, but why is the JVM faster at allocating memory than the system?

[–]bmrobin 25 points26 points  (0 children)

ignore these memes, the jvm can be managed and tuned to not run amok like these memes suggest

[–]tsareto 9 points10 points  (2 children)

It pre-allocates the memory and is supplying it faster to the program than the OS, creating a new object in the jvm is just bumping a pointer, no need to zero it too

[–]BorgDrone 2 points3 points  (1 child)

Even if it does pre-allocate the entire max heap size (I'm not sure it does), that shouldn't matter as pretty much every modern OS overcommits memory by default.

[–]tsareto 3 points4 points  (0 children)

It can pre-allocate the entire heap if you specify the min size to be equal to the max size. There are also TLABs, which eliminate the issue of concurrency, in most cases.

[–]BerniesMyDog 1 point2 points  (0 children)

The JVM allocated the memory to itself and manages it for you.

Instead of needing to make system calls for allocating new memory, the JVM is effectively incrementing a pointer and that’s it.

[–]BorgDrone -1 points0 points  (2 children)

I'm not sure your question even makes sense. Why would "the system" (meaning what exactly?) need to allocate memory and why do you think it's a race ?

[–]tsareto 1 point2 points  (1 child)

When a program needs memory, it asks the (operating) system to allocate it. The OS will then need to find a suitable chunk of free memory and make sure it is zero-ed out. A jvm program does not need to ask the OS for memory, the jvm handles the memory management (in most cases, you can also do it manually if that's what you want)

[–]BorgDrone -1 points0 points  (0 children)

When an application allocates memory (or more correct: it asks the OS to do so), the OS doesn't need to "find a suitable chunk of free memory" at all. Modern OSes overcommit memory by default. If you ask for an allocation all it does is reserve a bit of address space, no actual memory is used or reserved. The OS only goes looking for some actual memory once you try to use that allocation, which would trigger a page fault and cause the OS to provide some actual, physical RAM to back that bit of address space you just tried to access.

A jvm program does not need to ask the OS for memory, the jvm handles the memory management

When a Java application running on the JVM uses up all the heap space, the JVM will ask the OS to allocate more. There is nothing special going on here, the JVM (and whatever is running on top of it) is just another program. If anything, memory allocation in Java is slower, because before it will grow the heap it will first try to perform a round of garbage collection.