Tuesday, September 30, 2014

JDK 8: Thread Stack Size Tuning

When you upgrade JDK, you should re-examine all JVM options you have set in your Java applications.  For example, let's look at thread stack size tuning specifically.  As suggested in [1], it states:
  • In most applications, 128k happens to be enough for the Java thread stack.
However, after setting that, we have run into the following fatal exception:
The stack size specified is too small, Specify at least 228k
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
In this article, we will discuss thread stack size tuning in JDK 8 (i.e., HotSpot VM).

Default Thread Stack Size


When a new thread is launched, the Java virtual machine creates a new Java stack for the thread. As mentioned earlier, a Java stack stores a thread's state in discrete frames.[3] The Java virtual machine only performs two operations directly on Java Stacks: it pushes and pops frames.

The default thread stack size varies with JVM, OS and environment variables. To find out what your default ThreadStackSize is on your platform, use:[1]
java -XX:+PrintFlagsFinal -version
A typical value is 512k. It is generally larger for 64bit JVMs because references are 8 bytes rather than 4 bytes in size (but, you can compress oops or class pointers if you choose).[2] For example,[4]
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.

In JDK 8, every time the JVM creates a thread, the OS allocates some native memory to hold that thread’s stack, committing more memory to the process until the thread exits. Thread stacks are fully allocated (i.e., committed, not just reserved) when they are created.

This means that if your application spawns a lot of threads, this can consume a significant amount of memory which could otherwise be used by your application or OS (or it can eventually leads to OutOfMemoryError).

You can reduce your stack size by running with the -Xss option. For example:
java -server -Xss256k
or
java -server -XX:ThreadStackSize=256 
Note that if you have installed a 64-bit VM binary for Linux, you can omit -server option.[5]

Virtual Memory Map


In JDK 8, HotSpot installation comes with a feature named Native Memory Tracking (default: disabled).  To enable it, use:
-XX:NativeMemoryTracking=[off|detail|summary]

After enabling NMT, you can examine the memory footprint taken by either Thread or Thread Stack using:
jcmd <pid> VM.native_memory [summary | detail | baseline | summary.diff | detail.diff | shutdown] [scale= KB | MB | GB]

 For example, on a 64-bit Linux platform, here is the thread stack size before and after setting -Xss256k:

Before

 Virtual memory map:

[0x0000000040049000 - 0x000000004014a000] reserved and committed 1028KB for Thread Stack from
    [0x00002aec741ca5e4] JavaThread::run()+0x24
    [0x00002aec74083268] java_start(Thread*)+0x108

After

Virtual memory map:

[0x0000000040078000 - 0x00000000400b9000] reserved and committed 260KB for Thread Stack from
    [0x00002b02c69156e4] JavaThread::run()+0x24
    [0x00002b02c67ce338] java_start(Thread*)+0x108


Conclusions


The thread stack is used to push stacks frames in nested method calls. If the nesting is so deep that the thread runs out of space, the thread dies with a StackOverflowError.[8] If your applications use lots of recursive algorithms or if your applications are built on top of a framework utilizing MVC design pattern such as Oracle ADF, you may want to leave StackThreadSize as defaults.

However, thread stacks are quite large, particularly for a 64-bit JVM.  In [9], Scott Oaks has advised:
  • As a general rule, many applications can actually run with a 128 KB stack size in a 32-bit JVM, and a 256 KB stack size in a 64-bit JVM.
  • In a 64-bit JVM, there is usually no reason to set this value unless the machine is quite strained for physical memory and the smaller stack size will prevent applications from running out of native memory. 
  • On the other hand, using a smaller (e.g., 128 KB) stack size on a 32-bit JVM is often a good idea, as it frees up memory in the process size and allows the JVM to utilize a larger heap.

Finally, the total footprint of the JVM has a significant effect on its performance. So, footprint is one aspect of Java performance that should be commonly monitored.

42 comments:

for IT the said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

Priya Kannan said...

Great post! I am see the programming coding and step by step execute the outputs.I am gather this coding more information. It's helpful for me my friend. Also great blog here with all of the valuable information you have.
Java Training in Chennai

Blogger said...

BlueHost is ultimately one of the best hosting provider with plans for all of your hosting requirements.

SRI said...

Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..

rpa Training in Chennai

rpa Training in bangalore

rpa Training in pune

blueprism Training in Chennai

blueprism Training in bangalore

blueprism Training in pune

rpa online training

digi mark said...

The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.


rpa Training in annanagar

blue prism Training in annanagar

automation anywhere Training in annanagar

iot Training in annanagar

rpa Training in marathahalli

blue prism Training in marathahalli

automation anywhere Training in marathahalli

blue prism training in jayanagar

automation anywhere training in jayanagar













shalinipriya said...

Useful information.I am actual blessed to read this article.thanks for giving us this advantageous information.I acknowledge this post.and I would like bookmark this post.Thanks

Data Science Training in Chennai
Data science training in bangalore
Data science online training
Data science training in pune
Data science training in kalyan nagar
selenium training in chennai

simbu said...

Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
java training in chennai | java training in bangalore

java online training | java training in pune

java training in chennai | java training in bangalore

ragini. sai said...

This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
python training in chennai
python training in Bangalore

shethal said...

This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
DevOps online Training|DevOps Training in USA

param jothi said...

Some us know all relating to the compelling medium you present powerful steps on this blog and therefore strongly encourage contribution from other ones on this subject while our own child is truly discovering a great deal. Have fun with the remaining portion of the year.
Blueprism training in Chennai

Blueprism training in Bangalore

Blueprism training in Pune

Amar G said...

UiPath Training in Bangalore by myTectra is one the best UiPath Training. myTectra is the market leader in providing Robotic Process Automation on UiPath
robotic process automation training in bangalore

Nadeem Malik said...

nice work keep it up thanks for sharing the knowledge.Thanks for sharing this type of information.
digital marketing company in delhi

Nadeem Malik said...

nice work keep it up thanks for sharing the knowledge.Thanks for sharing this type of information, it is so useful.
block adhesive manufacturer in delhi

chitra pragya said...

All the points you described so beautiful. Every time i read your i blog and i am so surprised that how you can write so well.
angularjs

Training in chennai


angularjs-Training in chennai

angularjs Training in chennai

angularjs-Training in tambaram

angularjs-Training in sholinganallur

Vikash Kumar said...

Laminated Doors manufacturer in hubli

MOUNIKA said...

nice work keep it up thanks for sharing the knowledge.Thanks for sharing this type of information, it is so useful.
Exchange Server Training
Powershell Training
Windows Server Training

shivam enterprises said...

outdoor led wall lights in delhi

vimal kumar said...

I am really happy with your blog because your article is very unique and powerful for new reader.
Click here:
Selenium Training in Chennai | Selenium Training in Bangalore | Selenium Training in Pune | Selenium online Training

sandhya kits said...

Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
AWS Training Self Placed Videos

Golden Gate Training Self Placed Videos

sandhya kits said...

I found this informative and interesting blog so i think so its very useful and knowledge able.I would like to thank you for the efforts you have made in writing this article.
SAP PP Training institute

SAP Abap On Hana Training institute

sachin.ogeninfo said...

fridge repair in gurgaon

cynthia williams said...

Learned a lot from your blog. Good creation and hats off to the creativity of your mind. Share more like this.
RPA Training in Chennai
Robotics Process Automation Training in Chennai
RPA courses in Chennai
ccna Training institute in Chennai
AWS Certification in Chennai
DevOps course in Chennai

akhilapriya404 said...

I have read your blog its very attractive and impressive. I like your blog core Java online training Bangalore

Pankaj Singh said...

Thank you much for this tutorial; this is an informative and valuable blog. Visit for
Maldives Honeymoon Packages

Vikky cmd said...

I am really happy with your blog because your article is very unique and powerful for new reader.
Click here:
selenium training in chennai
selenium training in bangalore
selenium training in Pune
selenium training in pune
Selenium Online Training

mouni yoga said...

I think you have a long story to share and i am glad after long time finally you cam and shared your experience.
python course in btm | python Course in marathahalli| python training institute in jayanagar

Sri Nithya said...

Thanks for sharing this details admin, it gives lots of information to me. Continue sharing more like this.
Blue Prism Training Chennai
Blue Prism Training Institute in Chennai
Blue Prism course in Chennai
Blue prism course
Blue Prism course in Tambaram
Blue Prism course in Adyar

Nadeem Malik said...

digital marketing company in delhi

Shiva Shakthi said...

The blog you had post is verymuch useful for us to know about the Web designing. thanks for your information sharing with us.
Web Designing Institute in Coimbatore
Web Designing Course
Web Design Classes
Website Design Course
Learning Web Design

Vicky Ram said...

Nice post. I learned some new information. Thanks for sharing.

Education
Technology

Aruna Ram said...

I am reading your blog regularly, what a post very interesting and great content. Thank you for your post!!!
CCNA Training Center in Bangalore
Best CCNA Training Institute in Bangalore
CCNA Certification in Bangalore
CCNA Training in Vadapalani
CCNA Course in Tambaram
CCNA Training in Saidapet

Anbarasan14 said...

Thanks for this kind of worthy information. this was really very helpful to me. keep continuing.

Spoken English Classes in Bangalore
Spoken English Class in Bangalore
Spoken English Training in Bangalore
Spoken English Course near me
Spoken English Classes in Chennai
Spoken English Class in Chennai
Spoken English in Chennai

dinesh said...

This is really too useful and have more ideas and keep sharing many techniques. Eagerly waiting for your new blog keep doing more.
Aws Training in Bangalore
Aws Course in BangaloreAndroid Institute in Bangalore
Android Coaching in Bangalore
Android Coaching Center in Bangalore
Best Android Course in Bangalore

sharmi chithra said...

It is very excellent blog and useful article thank you for sharing with us, keep posting.

Primavera Training in Chennai
Primavera Course in Chennai
Primavera Software Training in Chennai
Best Primavera Training in Chennai
Primavera p6 Training in Chennai
Primavera Coaching in Chennai
Primavera Course

nanadhini chandran said...

feeling so good to read your information's in the blog.
thanks for sharing your ideas with us and add more info.
Salesforce Courses in T nagar
Salesforce Training Institutes in T nagar
Salesforce Course in Anna Nagar
Best Salesforce Training Institute in Anna nagar

sharmi chithra said...

Nice post. By reading your blog, I get inspired .. Thank you for posting.

Informatica Training chennai
Informatica Training institutes in Chennai
Best Informatica Training Institute In Chennai
Best Informatica Training center In Chennai
Informatica institutes in Chennai
Informatica courses in Chennai
Informatica course
Informatica MDM Training in Chennai

vishnu said...

I have found your blog very useful and attractive. Keep posting more like this.

Best Linux Training Institute in Chennai
Best Linux Training in Chennai
Learn Linux
Linux Training in Adyar
Linux Course in Velachery
Best Linux Training Institute in Tambaram

https://amantoothtown.blogspot.com/ said...


Great..thanks for sharing this information..
Dentist in Vasant Vihar

Veav by nimisha priya said...

Very nice post.
buy tops for girls

Y and H Cargo India said...
This comment has been removed by the author.
Y and H Cargo India said...

Great..!!thanks for sharing with us.
Shipping Company in India

gowthunan said...

She noticed a wide variety of pieces, with the inclusion of what it is like to have an awesome helping style to have the rest without hassle grasp some grueling matters.
iosh course in chennai