How much memory foot print does a normal thread takes in java. Assuming that there is no object associated with it.
Asked
Active
Viewed 7,774 times
14
-
2A good profiler can help with that. – Brady Jun 14 '12 at 05:56
2 Answers
15
The amount of memory allocated for a thread stack is specific to your JVM Version + Operating System. It is configured with the -XX:ThreadStackSize option (-Xss on older versions.) Anecdotally 512KB is "normal", although it is 1024 on 64-bit linux which is probably the platform it's most commonly critical (one guy's opinion anyway)

Affe
- 47,174
- 11
- 83
- 83
-
As u said it is the ThreadStackSize which is by default 512KB(for 32bit), is this memory reserved for a tread at the time of creation or dynamically consumed as the method calls are nested? – Ranger Jun 14 '12 at 06:05
-
It is reserved. What that means is also somewhat platform dependant, but fundamentally if the operating system cannot guarantee it is able to provide the memory, an OOM exception will happen at the time the thread is created. (able to provide the memory may include forcing application data into swap space of course.) This is related to the contract of the underlying malloc operation. This also all assumes modern JVM with native-bound threads of course. What happens in the interpreter is JVM's business. – Affe Jun 14 '12 at 06:10
-
1Also remember linux has a user-thread cap independent of memory limitations. Java's "Out Of Memory: Unable to create native thread." Does not necessarily indicate the problem was actually a lack of physical RAM. – Affe Jun 14 '12 at 06:11
-
Java reserves the maximum size of virtual memory for the heap, thread stacks etc when the heap and threads are created. The real memory is assigned when it is used. In a 32-bit application the virtual memory can run out fairly quickly as it can be as small as 1.5 GB on some OSes. In 64-bit applications the virtual memory can be 48-bit on many platforms and you only have to worry about the real memory. – Peter Lawrey Jun 14 '12 at 07:11
4
Each thread in a Java application has its own stack. The stack is used to hold return addresses, function/method call arguments, etc And by default stack size is 512KB. You can change by -Xss
jvm command.
java -Xss128k

Subhrajyoti Majumder
- 40,646
- 13
- 77
- 103