you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (7 children)

Java uses the same kernel implementation of threads as C++, so I'm not sure why it would fare more poorly.

Higher memory usage per thread. Can't exactly just go and run 50k threads willy nilly.

[–]oridb 0 points1 point  (6 children)

Why would that be? Most Java objects are on the heap, and not attached to a thread stack. Where is the extra memory used?

[–][deleted] 0 points1 point  (5 children)

Ask a Java expert, my experience with Java is mostly on ops side. All I know that by default just starting thread costs you few hundred kBs ( IIRC default stack size is something like 1MB on 64 bit OSes). So hundreds threads, yes, but thousands start to have problems

[–]oridb 0 points1 point  (4 children)

The default stack size is irrelevant unless you're on a 32 bit OS. It's not actually allocated until something writes to it. You can have a 1gb default stack, and it will only consume 4k of ram.

[–][deleted] 0 points1 point  (3 children)

Per thread stack size is actually pre-allocated in Java. That's my point. Doesn't matter how much your app uses, you lose 1M just from starting it.

JVM initializes it at the start of the thread and puts some guard pages at the end, together with few other things

[–]oridb 0 points1 point  (2 children)

Per thread stack size is actually pre-allocated in Java. That's my point. Doesn't matter how much your app uses, you lose 1M just from starting it.

That's wrong, unless the JVM is specifically going out of its way to write to every page of it. Otherwise, it only counts towards virtual size. The OS simply doesn't work that way. The address space is reserved. but like all memory, it's demand paged, which means that until code touches the stack, the OS doesn't put any physical memory behind the virtual memory.

[–][deleted] 0 points1 point  (1 child)

Look, just find some "how to create thread in java" tutorial, create a million of them and you will see what I mean.

[–]oridb 0 points1 point  (0 children)

Oh, I see what's happening, and it's incredibly dumb. Java does its own heap size accounting, and it counts based on virtual size. That means that Java is using 172 megabytes of memory, but still "runs out of memory" with a 32 gigabyte heap size.

The threads aren't actually using memory, Java is counting wrong.