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.

437 comments:

«Oldest   ‹Older   401 – 437 of 437
BK-25 said...
This comment has been removed by the author.
BK-25 said...

Interesting blog thank you for sharing.

Best software training institute in Chennai.
best azure training in chennai
RPA Training in Chennai
DevOps Training in Chennai
Cloud-computing Training in Chennai
Ui-Path Training in Chennai
PHP Training in Chennai
Blue-Prsim Training in Chennai

ArjanTech Solutions said...

Thanks for sharing such posts with us. This is really very interesting topic for your readers to grab more knowledge about it

web development Company in Varanasi India
App development Company in Varanasi India
CMS and Ecommerce development Company in Varanasi India
Digital Marketing Company in Varanasi India

Devin Karlson said...

Thanks for provide great informatic and looking beautiful blog, really nice required information & the things i never imagined and i would request, wright more blog and blog post like that for us. If you are looking for instagram reels like and view So, you can connect to Famoid Support.

Unknown said...


Nice blog thank you .For your Sharing It's a pleasure to read your post.It's full of information I'm looking for and I'd like to express that "The content of your post is awesome"

Aimore Tech is the Best Software training institute in chennai with 6+ years of experience. We are offering online and classroom training.
manual testing Training in Chennai
Oracle Training in Chennai
Oracle DBA Training in Chennai
ASP.NET Training in Chennai

Hussey said...

Happy to read the informative blog. Thanks for sharing
best java training institute in chennai
best java training institute in chennai

Ramesh Sampangi said...

Excellent blog. Informative and knowledgeable content. I really enjoyed while reading this article. Keep sharing more stuff like this. Thank you.
Data Science Training in Hyderabad

David Fincher said...

This post is so interactive and informative.keep update more information...
Android Training in Anna Nagar
Android Training in Chennai

خدمات منزلية said...

وين الاقي قطع غيار الفرن

ابو روز said...


شركة رش مبيدات بالاحساء
شركة رش مبيدات بالدمام
شركة رش مبيدات بالقطيف

preety said...

Amazing blog content.Thank you for sharing Good information.

here You can get more information about Oracle Fusion Courses.

Oracle Fusion SCM Online Training

Oracle Fusion HCM Online Training

Oracle Fusion Financials Online Training

Oracle Fusion Technical Training

preety said...

Nice Article,Thank you for Sharing Valuable Content.

Soft Online Training offers

Oracle Fusion SCM Online Training

Oracle Fusion HCM Online Training

Oracle Fusion Financials Online Training

Oracle Fusion Technical Online Training

Oracle Fusion PPM Online Training

Oracle Integration Cloud Online Training

Ganesh said...

Thanks for sharing....Calibration & Measurement Services in Chennai
VMS Measurement in Chennai
Snap Gauge & Plug Gauge Calibration in Chennai

Asquare Cloud Hosting said...

This is amazing post for Quickbooks user which is one of the best software for manipulating many accounting task . this software could be any issue. to resolve your issue connect with at QuickBooks Windows 8 PDF Not Working.

Unknown said...

Thank you for this informative blog. To know about AI automated helpdesk and service desk at MS teams, visit our website and schedule your demo. To know more click here
microsoft teams hr service desk

traininginstitute said...

I am genuinely thankful to the holder of this web page who has shared this wonderful paragraph at this place
cyber security course in malaysia

data scientist course said...

This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post!
business analytics course in hyderabad

Unknown said...

Great post. keep sharing such a worthy information.

Devops Training in Bangalore

AWS Training in Bangalore

pranisha said...
This comment has been removed by the author.
123tws webdesign company said...

such a informative blog. really useful.
Training centers is the place to learn all the seo techniques to become a proficient SEO Specialist.
web designing course in coimbatore

web development training in coimbatore

full stack developer course in coimbatore

seo training in coimbatore

digital marketing classes in coimbatore

graphic designing course in coimbatore

123tws webdesign company said...
This comment has been removed by the author.
Anonymous said...

great post on JDK 8: , keep posting valuable information Java Training In Pune

Anonymous said...

thanks for providing valuable information Salesforce Classes In Pune

LYSA Consultancy said...

hey, buddy thank you so much for the information you provided above I really enjoyed reading your blog if you are in any problem related to the IT sector then you can visit our website LYSA Consultany a global IT Solution provider, we strive to help businesses to reach new heights of success through the power of remote dba support, DevOps Consulting Services and Cloud services including aws cloud services & google cloud services and a lot more that can help customers to fulfill their unique business needs.

Stellarhomesdevelopers said...

"I appreciate the practical tips and advice you provide. It's clear that you have firsthand experience in the home building industry."

iteducationcentre said...

Nice blog. Keep up with your writing.
CCNA course in Pune

Anonymous said...

amazing post , keep posting python classes in pune

iteducationcentre said...

wonderful Post.
CCNA course in Pune

Garhwal Cab Services said...
This comment has been removed by the author.
Sudhir said...

Good job & thank you very much for the new information, I learned something new. Very well written.
 Data Analyst Classes in Pune

Chaitanya said...

Oh man! This blog is sick! How did you make it look like this !
SAP GRC 11g and 12c Training
Dell Boomi Training
SQL Server DBA Training
<a href="https://viswaonlinetrainings.com/courses/exchange-server-online-training/>Exchange Server Training</a>

Chaitanya said...

Thanks for sharing your info. I truly appreciate your efforts and I am waiting for your next post thank you once again.
.Net Training
Data Analytics Training
Python Training

Dr Tony Eye hospital said...

Tony Fernadez eye hospital provide Laser Eye Surgery Cost in Kerala

balamarketer said...

AWS (Amazon Web Services) online training typically covers a wide range of topics related to cloud computing and the services offered by AWS. Here's a suggested outline for an AWS online training course:

iteducationcentre said...

Nice Post.
Java training in Pune

shivani said...

Thanks for sharing valuable information, keep more posting. best java training institute in pune

best java classes in pune

Insight IT Training Institute said...

Nice blog and informative content,
Thanks for sharing with us,
Visit Insight IT - SAP BODS Training in Hyderabad

«Oldest ‹Older   401 – 437 of 437   Newer› Newest»