原文:
http://www.java2000.net/p11488
I recently came across this exception on a couple of java systems that use many threads java.lang.OutOfMemoryError: unable to create new native thread. The strange thing was that the JVM had been assigned a lot of memory (1.5GB) and that it had at least half the memory available. Michele found this article that points out that 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.
JVM已经分配了足够多的内存了(1.5GB),而且至少有一半的可用内存。但是当你有很多的线程在运行时,你给JVM的内存越多,你越容易出现无法创建本地线程的内存溢出。
Which makes perfect sense when you think about it. Each 32 bit process on Windows has 2GB "available" memory as 2GB is reserved to Windows. In my case the JVM grabbed 1.5 GB leaving 500MB. Part of the 500MB was used to map system dlls etc in memory so less than 400 MB was left. Now to the crucial point: When you create a thread in java it creates a Thread object in the JVM memory but it also creates a operating system thread. The operating system creates the thread with a thread stack in the 400MB that is left, not in the 1.5 GB allocated in the JVM. Java 1.4 uses a default stack size of 256kb but Java 1.5 uses a 1MB stack per thread. So, in the 400MB left to process I could only generate ~400 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.
在32位的Windows机器上,最大有2G的内存,所以JVM一般使用1.5G留下500M。其中的一部分用来映射系统的dll到内存里面,所以只剩下了400M.此时,我们创建了一个java线程,它不仅在JVM的内存创建了线程对象,同时创建了操作系统的线程。
操作系统创建线程,使用了400M里的线程堆栈而不是JVM里面1.5GB的。 每个线程,Java 1.4默认使用 256k, 1.5默认使用 1M的堆栈。那么 400MB的内存,最多可以创建400个线程。为了创建更多的线程,你必须减少你的JVM分配的内存数。
This formula gives a decent estimate for the number of threads you can create:
(MaxProcessMemory - JVMMemory - ReservedOsMemory) / (ThreadStackSize) = Number of threads
For Java 1.5 I get the following results assuming that the OS reserves about 120MB:
1.5GB allocated to JVM: (2GB-1.5Gb-120MB)/(1MB) = ~380 threads
1.0GB allocated to JVM: (2GB-1.0Gb-120MB)/(1MB) = ~880 threads
Java 1.4 uses 256kb for the thread stack which lets you create a lot more threads:
1.5GB allocated to JVM: ~1520 threads
1.0GB allocated to JVM: ~3520 threads
I have not tried the 3GB switch but it should in theory let you create more threads
分享到:
相关推荐
React Native By Example by Richard Kho English | 24 Apr. 2017 | ASIN: B01M31KB4Q | 414 Pages | AZW3 | 4.46 MB Key Features Work on native APIs and UI Elements using React Native Get the best of both...
Microsoft SQL Server Native Client (SQL Native Client) 是一个同时包含 SQL OLE DB 访问接口和 SQL ODBC 驱动程序的动态链接库 (DLL)。它对使用本机代码 API(ODBC、OLE DB 和 ADO)连接到 Microsoft SQL Server ...
React Native Debugger是一款强大的开发工具,特别为在macOS平台上进行React和React Native应用的可视化调试而设计。它提供了丰富的功能,使开发者能够更有效地检查、调试和优化代码,尤其是在处理复杂的Redux状态...
By bringing the advantages of ReactJS to mobile, React Native transforms every web developer into a potential mobile developer. Unlike existing JavaScript-for-mobile approaches, React Native actually ...
在Android系统中,Native Activity是一种特殊的应用程序结构,它允许开发者直接使用C或C++等语言编写的代码创建一个应用,而不必通过Java层进行桥接。Native Activity适用于需要高性能计算或者图形处理的应用场景,...
SQL Server Native Client 10.0 是微软推出的一款专门用于与SQL Server 2008及后续版本交互的客户端库,它集成了ODBC(Open Database Connectivity)和OLE DB(Object Linking and Embedding, Database)接口。...
cesium-native 编译好的东西哇cesium-native 编译好的东西哇cesium-native 编译好的东西哇cesium-native 编译好的东西哇cesium-native 编译好的东西哇cesium-native 编译好的东西哇cesium-native 编译好的东西哇...
谷歌的chrome native client是一种为浏览器编译运行native代码开源的技术,出于维护系统的安全便捷的目的,创造出人们所期望的web应用程序.native技术远远超出了javascript的web编程范畴,使得开发者使用他们自己喜欢的...
Learning React Native Building Native Mobile Apps with JavaScript(2nd) 英文azw3 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
《NativeExcel3.1.0 Delphi XE7:在Delphi编程环境中高效处理Excel的利器》 在软件开发领域,特别是在使用Delphi XE7作为开发工具时,与Excel文件进行交互是常见的需求。NativeExcel3.1.0正是为解决这一问题而设计...
React Native UWP 应用是将流行的JavaScript框架React Native应用于微软的通用Windows平台(UWP)上,使得开发者能够利用JavaScript编写代码,同时为Windows 10设备提供原生应用程序的体验。React Native UWP结合了...
NativeScript for Angular Mobile Development 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
React Native 瀑布流列表是一种流行的前端技术,它允许开发者构建动态、响应式的界面,以展示内容,尤其是图片和媒体元素,以类似瀑布或下落石头的方式排列。这种布局方式在电商、摄影或者社交媒体应用中非常常见,...
React Native Page 是一款专为React Native平台设计的分页组件,它可以帮助开发者轻松地在原生移动应用中实现页面滑动切换的效果。React Native是一种由Facebook开发的开源框架,允许开发者使用JavaScript和React来...
Microsoft SQL Server 2008 Native Client (SQL Server Native Client) 是单一动态链接库 (DLL),其中包含 SQL OLE DB 提供者和 SQL ODBC 驱动程序。此链接库针对使用机器码 API (ODBC、OLE DB 和 ADO) 的应用程序...
**SQL Server Native Client 10.0 知识点详解** SQL Server Native Client 10.0 是微软为SQL Server 2008和2008 R2设计的一个数据库访问接口,它集成了ODBC(Open Database Connectivity)和OLE DB(Object Linking...
数据库客户端Native Client 10.0是针对Microsoft SQL Server 2008设计的一款重要的数据库访问组件。在本文中,我们将深入探讨这个组件的核心功能、兼容性、安装与使用,以及它在数据库管理中的作用。 首先,SQL ...
为 React Native 带来 NFC 功能。灵感来自phonegap-nfc和react-native-ble-manager 该库最简单(也是最常见)的用例是读取NFC包含 的标签NDEF,可以通过以下代码实现: import React from 'react'; import {View, ...