Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread stack memory vs heap memory  RSS feed

 
Silvia Wong
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would like to ask:
If i have a server box of 2G RAM,
then I have tune:
-Xms 1.6G -Xmx 1.6G
-Xss 1024.

From my understanding, each of my Java thread creation will be allocated 1 stack (which ate up 1024). If my threads creation hit 2000, does that mean that I will not have any more space for my heap (i set it to be 1.6G)?

Thanks in advance. Hope somebody will enlighten me soon.
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From my limited understanding.

The Xms and Xmx control the heap size, which is where new Objects are placed. The Xss controls the amount of stack allocated for each thread. It is not unusual for the total amount of memory to exceed the Xmx, because the VM will allocate memory for other things such as a stack for each thread.

Each thread in the VM get's a stack. The stack size will limit the number of threads that you can have, too big of a stack size and you will run out of memory as each thread is allocated more memory than it needs. If the stack space is too small, eventually you will see an exception like java.lang.StackOverflowError.

I hope this helps. I am sure there is a member, who can elaborate more on the subject.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the sun JVM implementation is a stack based implementaiton. If you are using Sun then yes each thread will take up so much memory. No you likely wont run out of ram. The OS will just start giving you virtual memory which will slow your program possibly, or possibly not since only a single thread will be running at a time depending on how many CPUs you have.

Xms and Xmx are program memory, not heap memory. IIRC it includes both thread and heap and everything else.

there are some lesser known switches to directly control heap stuff i believe.
 
Silvia Wong
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are saying -Xss and -Xmx is merely a program memory, then how we determine our total JVM memory that allocate to our program?

Thanks.
 
Silvia Wong
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And actually what is the standard stack size that I can allocate to my thread without exception occured? I maybe will have more than 3000 thread. I have a 2G machine with dual processors.

Thanks a lot.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Silvia Wong:
You are saying -Xss and -Xmx is merely a program memory, then how we determine our total JVM memory that allocate to our program?

Thanks.


You can determine the total memory used by the JVM with -Xms and -Xmx. Only 1 program can run in a jvm at a time so all this memory is being used by your program's processes + JVM processes (garbage collection , etc.)

JVM memory will be divided into stack and heap memory on Sun JVMs. If you think you need bigger stacks you can configure that. What are you trying to do? If anything you should increase your -Xmx. If your running a server increase you -Xms to match your -Xmx and turn on the incremental GC. You dont need to mess with the stack unless you are doing some shady sequential style coding, such as recursive functions.
[ August 10, 2005: Message edited by: CL Gilbert ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!