the more memory you give to the JVM the more likely you are to get java.lang.OutOfMemoryError: unable to create new native thread exceptions when you have many threads.
Absurd but true: to create more threads you have to reduce the memory allocated to the JVM.
Another option is to host the JVM in your own process using JNI.
I've done some interesting work lately, improving performance on a server designed to handle thousands of stateful concurrent connections. One of the major problems I've been seeing is the mass proliferation of threads. I was seeing memory errors like
java.lang.OutOfMemoryError unable to create new native thread
The problem with this message is that the server actually had plenty of memory to go around. The max heap was set to 1G, but it was only using up perhaps a few hundred megabytes of that. Initially, I thought that, perhaps, the memory being allocated for the thread's stack wasn't being reported against the heap and was causing these out of memory errors. I remembered reading somewhere that the default Window's thread stack size was 1M. According to MSDN, httpmsdn.microsoft.comlibrarydefault.aspurl=libraryen-usdllprocbasethread_stack_size.asp, what really happens, is that the operating system will reserve a certain amount of address space for each thread, but only commit a percentage of it to physical memory. The actual amount of memory committed and address space reserved isn't documented there, though it does note that if you change the initial amount committed to be larger than the default, the operating system will reserve the same amount of address space, rounded up to the nearest megabyte - hence using -Xss to change the initial stack size (even just slightly) can cause a much larger amount of address space to be reserved - potentially an entire megabyte.
After running some tests with the JVM, I've come to the conclusion that Sun's JDK 1.4 allocates about 256K of address space per thread. I also ran some tests with JDK 1.5, which seem to indicate that it was allocating about 1M of address space per thread. Those numbers come from an assumption of a 2G address space per process under Windows, and the following statistics for the maximum number of threads I could create
JDK1.4
-Xmx750 = 4580 threads.
-Xmx1000 = 3608 threads.
-Xmx1500M = 1663 threads
JDK1.5
-Xmx750M = 1129 threads
-Xmx1000M = 880 threads
-Xmx1500M = 384 threads
As you can see, the maximum number of threads I can create decreases as the heap size gets larger. This is because the JVM immediately reserves the address space specified for the maximum heap size - That's probably because it needs that memory to be contiguous.
This tells us two things
1) There are very finite limits on the maximum number of threads that you can create in an application, and those numbers are very small. This only reinforces the mantra that server's should be designed to operate with a fairly small and constant number of threads.
2) If you're really stuck creating a lot of threads, you can maximize the number available to you by reducing the address space allocated by the VM through a lower maximum heap size. You should also avoid changing the thread stack size, as a larger stack size can result in a much larger consumption of address space. Finally, unless things change, you should be prepared to run under JDK1.4 for a long time.
分享到:
相关推荐
《Java Performance - The Definitive Guide》是一本深入探讨Java性能优化的专业书籍,它为Java开发者提供了关于如何提升代码效率、降低系统资源消耗的关键指导。这本书涵盖了从基础的编程实践到复杂的工具使用,...
《High-Performance Java Persistence》是一本专注于Java持久化性能的书籍,作者Vlad Mihalcea,涵盖了数据库服务器、连接层、应用数据访问层、ORM框架、原生查询构建器、性能与扩展性、响应时间、吞吐量、数据库...
java-se-subscription-enterprise-performance-pack-users-guide.pdf
在高性能计算(High Performance Computing, HPC)领域,C语言一直占据着主导地位,尤其是在处理密集型任务和基础设施功能开发方面。C语言以其高效、紧凑和对硬件资源的直接访问能力,确保了在有限的尺寸、重量和...
标题《Top-10-Java-Performance-Problems》和描述《专家总结》指出,文章将讨论Java性能问题中最常见的十个问题。这篇文章主要面向Java开发者,旨在帮助他们识别和解决影响Java应用性能的关键问题。 内容概要如下:...
Enhancing-the-performance-of-a-tensioned-meta_2018_Nuclear-Instruments-and-M
When O’Reilly first approached me about writing a book on Java performance tuning, I was unsure. Java performance, I thought—aren’t we done with that? Yes, I still work on performance of Java (and ...
This book was written with one goal in mind: to provide Java programmers with... These lessons cover performance-critical areas such as memory management, garbage collection, caching, and multithreading.
《Java Performance》是一本专注于Java程序性能优化的专业书籍,它为开发者提供了深入理解并提升Java应用程序性能的关键知识。这本书详细探讨了如何在Java开发中识别和解决性能问题,旨在帮助开发者构建更快、更高效...
Java: High-Performance Apps with Java 9: Boost your application’s performance with the new features of Java 9 By 作者: Mayur Ramgir – Nick Samoylov ISBN-10 书号: 1789130514 ISBN-13 书号: ...
Romain Manni-Bucau - Java EE 8 High Performance-Packt (2018).epub
Java® Performance Companion shows how to systematically and proactively improve Java performance with today’s advanced multicore hardware and complex operating system environments. The authors, who...
Skywalking是分布式系统的应用程序性能监视工具,专为微服务,云原生架构和基于容器(Docker,K8S,Mesos)架构而设计,它是一款优秀的APM(Application Performance Management)工具,包括了分布式追踪,性能指标...
In this paper, the performance of voltage-source converter-based shunt and series compensators used for load voltage control in electrical power distribution systems has been analyzed and compared, ...
Gain in-depth knowledge of Java application performance, using the Java Virtual Vachine (JVM) and the Java platform, including the language and API. With this comprehensive guide, developers and ...
Troubleshooting Java Performance Detecting Anti-Patterns with Open Source Tools 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
script-performance-up-cmd.sh
Thesis the performance of STBC-VBLAST with interference cancellation and channel estimation error
Design of High-Performance Microprocessor Circuits Chandrakasan, A. Bowhill, W. Fox, F. Publication Date: 2000 Publisher: Wiley-IEEE Press Number of Pages: 584 Edition: 1 Subject Categories: ...