Memory usage for creating Java threads

How much memory (roughly) is allocated when creating and running a Java thread?

Here is a sample code:

// Definition of the thread class class BasicThread extends Thread { // This method is called when the thread runs public void run() { } } . . . // Create and start the thread Thread thread = new BasicThread(); thread.start(); 
+6
source share
2 answers

Well, the stream (i.e. the object) itself needs some space - it has about a dozen variables and objects (and I'm too lazy to correctly count them), but it should be a little more than maybe 200 bytes (you need it would be to count all the primitives and links [trivial, those have fixed sizes, but the links depend on your virtual machine], and then calculate the size of all objects that are highlighted by the class [the VM hotspot has an overhead of 2 words per object (3 if there are no local variables in the object) and n and the border is 8 bytes])

What really takes up space is the local thread stack, and the -Xss flag can affect it (although note that each OS has some restrictions on the maximum stack space, you can influence this with -ulimit in linux and of course somehow in the windows).

By default for an access point:

In Java SE 6, the default is Sparc 512k on a 32-bit VM and 1024k on a 64-bit virtual machine. On x86, Solaris / Linux is 320k on a 32-bit VM and 1024k on a 64-bit virtual machine.

On Windows, default thread stacks are read from binary (Java.exe). Starting with Java SE 6, this value is 320k in a 32-bit virtual machine and 1024k in a 64-bit virtual machine.

+7
source

IIRC, a 32-bit version of Windows, reserves 64 KB of β€œreal” physical memory for the original thread stack. The Maybee kernel may reserve another page of memory without writing, but basically the only important source reserve is the stack for the new thread. If this stack is reset, the virtual memory manager expands it to the limit read from the exe header, and is usually set for the duration of the link. IIRC, this limit cannot be reduced below 1 MB.

I don’t know how Linux-32 behaves. Presumably somewhat similar.

Rgds, Martin

0
source

Source: https://habr.com/ru/post/890523/


All Articles