不是全部的OutofMemory exception都是内存问题。。。

前几天有个客户的site报了下面错误:

[ERROR][thread ] Could not start thread Timer-72025. Resource temporarily unavailable
Exception in thread "Timer-72024" java.lang.OutOfMemoryError: Resource temporarily unavailable in tsStartJavaThread
(lifecycle.c:1097). Attempting to allocate 2G bytes There is insufficient native memory for the Java
Runtime Environment to continue. Possible reasons:
The system is out of physical RAM or swap space
In 32 bit mode, the process size limit was hit Possible solutions:
Reduce memory load on the system
Increase physical memory or swap space
Check if swap backing store is full
Use 64 bit Java on a 64 bit OS
Decrease Java heap size (-Xmx/-Xms)
Decrease number of Java threads
Decrease Java thread stack sizes (-Xss)
Disable compressed references (-XXcompressedRefs=false) at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:640)
at java.util.Timer.<init>(Timer.java:137)
at java.util.Timer.<init>(Timer.java:106)
at ......
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)

第一反应以为是内存不足,于是dump了heap回来看。发现系统仅仅占用了80M的内存,远远系统内存绰绰有余,那么为啥会出现这样的情况呢?查看了出错的代码。发现代码启动了非常多的线程,遂想起曾经遇到过类似的问题,weblogic里面启动的线程太多。超出了linux的最大限制,也是会出这个看起来内存泄漏的日志。查看系统的限制:

sc-1@vECE-222 limits.d # ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 38610
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 8192
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 1024
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

发现最大的进程数是1024,注意:linux里面计算线程和进程是用一样计算的,这里说的max user processes是包含线程的。

使用下面代码重现问题:

public class ThreadExample {

    public static void main(String[] args){
System.out.println(Thread.currentThread().getName());
for(int i=0; i< 2048; i++){
new Thread("" + i){
public void run(){
System.out.println(Thread.currentThread().getName() + " running"); try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " ending");
}
}.start();
}
}
}

运行和输出:

sc-1@ECE30CP3_Z1 etmdaha # su -p occas -c "/opt/jrockit-jdk/bin/java ThreadExample"
Main Thread
0 running
5 running
//
836 running
[ERROR][thread ] Could not start thread 837. Resource temporarily unavailable
Exception in thread "Main Thread" java.lang.OutOfMemoryError: Resource temporarily unavailable in tsStartJavaThread (lifecycle.c:1096). Attempting to allocate 3G bytes There is insufficient native memory for the Java
Runtime Environment to continue. Possible reasons:
The system is out of physical RAM or swap space
In 32 bit mode, the process size limit was hit Possible solutions:
<strong>Reduce memory load on the system
Increase physical memory or swap space
Check if swap backing store is full
Use 64 bit Java on a 64 bit OS
Decrease Java heap size (-Xmx/-Xms)
Decrease number of Java threads
Decrease Java thread stack sizes (-Xss)
Disable compressed references (-XXcompressedRefs=false)
</strong>
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:640)
at ThreadExample.main(ThreadExample.java:6)

linux系统资源有各种限制:内存限制。栈限制,文件数目限制,线程限制。要小心陷进这些问题。

# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 79010
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 1024
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

这里有两篇非常好的文章:

http://people.redhat.com/alikins/system_tuning.html#threads

http://kavassalis.com/2011/03/linux-and-the-maximum-number-of-processes-threads/

Weblogic OutOfMemory exception的误解 -- thread limitation的更多相关文章

  1. Delphi thread exception mechanism

    http://www.techques.com/question/1-3627743/Delphi-thread-exception-mechanism i have a dilema on how ...

  2. std::thread “terminate called without an active exception”

    最近在使用std::thread的时候,遇到这样一个问题: std::thread t(func); 如果不使用调用t.join()就会遇到 "terminate called whitho ...

  3. 如何抓取Thread Dump小结(转)

    当系统性能出现问题时,需要从各个方面来查看网络环境.主机资源.查看最经变更的代码等.如果是想从代码层面解决问题,那么最有效的方法就是查看相关dump文件.如果是使用IBM JDK(我默认你是在aix环 ...

  4. 多线程爬坑之路-Thread和Runable源码解析

    多线程:(百度百科借一波定义) 多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提 ...

  5. 创建Thread类的子类

    package unit8; public class MyThreadTest { public static void main(String[] args) { MyThread t1 = ne ...

  6. java中多线程中Runnable接口和Thread类介绍

    java中的线程时通过调用操作系统底层的线程来实现线程的功能的. 先看如下代码,并写出输出结果. // 请问输出结果是什么? public static void main(String[] args ...

  7. SSIS hang with unhandle exception

    如果你的packages在执行的过程中有没有被捕获到的异常,则sqldumper会跳出来获取这些异常信息输出,存在下面的目录中: C:\Program Files\Microsoft SQL Serv ...

  8. Java thread中对异常的处理策略

    转载:http://shmilyaw-hotmail-com.iteye.com/blog/1881302 前言 想讨论这个话题有一段时间了.记得几年前的时候去面试,有人就问过我一个类似的问题.就是j ...

  9. How a C++ compiler implements exception handling

    Introduction One of the revolutionary features of C++ over traditional languages is its support for ...

随机推荐

  1. DP———1.最大子连续子序列和

    最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  2. 修改Eclipse发布路劲(Deploy path)

    Eclipse默认的发布路劲在用户的workspace里,比较难找,为了方便查看,可以将发布路劲修改为自己想要的. 修改方法: 1.先stop Servers下tomcat里的项目,然后remove ...

  3. [bzoj2111][ZJOI2010]Perm 排列计数 ——问题转换,建立数学模型

    题目大意 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic的,答案可能很 ...

  4. mlock家族:锁定物理内存【转】

    转自:http://blog.csdn.net/fjt19900921/article/details/8074541 锁住内存是为了防止这段内存被操作系统swap掉.并且由于此操作风险高,仅超级用户 ...

  5. Linux getcwd()的实现【转】

    转自:http://www.cnblogs.com/alan-forever/p/3721908.html 通过getcwd()可以获取当前工作目录. 1 #include <unistd.h& ...

  6. 流畅的python第九章笔记 python风格的python

    9.1对象表示形式 __repr__和__str__这两个方法都是用于显示的,__str__是面向用户的,而__repr__面向程序员. 我们打印下面的A是默认输出这个对象的类型,我们对B进行了修改_ ...

  7. 无法解析的外部符号 "public: static void __cdecl std::_String_base::_Xran(void)" (?_Xran@_String_base@std@@SAXXZ)"

    采用下面的方法.重新编译了一下依赖的库,OK了.   问题描述: 今天用VS2010编译一个在VS2008下Coding的工程的时候,VS给出了一堆链接错误信息,如下图所示: 在ErrorList里面 ...

  8. java1.7集合源码阅读:ArrayBlockingQueue

    ArrayBlockingQueue是一个先进先出线程安全的队列,队列头部是进入队列时间最长的元素,队尾是进入队列时间最短的元素,同时队列的最大容量是固定的. 先看类定义: public class ...

  9. Appium+python自动化4-元素定位uiautomatorviewer【转载】

    前言 环境搭建好了,下一步元素定位,元素定位本篇主要介绍如何使用uiautomatorviewer,通过定位到页面上的元素,然后进行相应的点击等操作. uiautomatorviewer是androi ...

  10. Table is marked as crashed and should be repaire (

    https://www.cnblogs.com/cxchanpin/p/6894747.html