Armond Mehrabian
Armond Mehrabian

Reputation: 63

Amount of memory useage when creating Java threads

How much memory (roughly) is allocated when a Java thread is instantiated and started?

Here's a code sample:

// 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();

Upvotes: 6

Views: 595

Answers (2)

Martin James
Martin James

Reputation: 24907

IIRC, 32-bit Windows reserves 64K of 'real' physical RAM for the initial thread stack. Maybee the kernel might reserve another page of non-paged memory, but, basically, the only important initial reserve is the stack for the new thread. If this stack gets blown, the virtual memory manager increases it up to a limit read from in the exe header and typically set at link time. IIRC, this limit cannot be reduced below 1MB.

Don't know how Linux-32 behaves. Presumably, somewhat similarly.

Rgds, Martin

Upvotes: 0

Voo
Voo

Reputation: 30235

Well the thread (that is the object) itself needs some space - it does have a dozen or so variables and objects (and I'm way too lazy to count them correctly) but it should be little more than maybe 200byte (you'd basically have to count all primitives and references [trivial, those have fixed sizes - but references depend on your VM] and then compute the size of all objects that are allocated by the class [the hotspot VM has an overhead of 2 words per object (3 if there are no local variables in the object) and allocates on an 8byte boundary])

What really takes space is the thread local stack and that can be affected by the -Xss flag to the VM (although note that every OS has some limitations to the maximum stackspace, you can influence this with -ulimit in linux and surely somehow in windows as well).

The defaults for hotspot are as follows:

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

On Windows, the default thread stack size is read from the binary (java.exe). As of Java SE 6, this value is 320k in the 32-bit VM and 1024k in the 64-bit VM.

Upvotes: 7

Related Questions