질문] Linux 에서 java 통해 쓰레드 1024 개 이상생성하신분 ?
linux 에서 이상하게 native 쓰레드 개수가 1024개를 넘지 못합니다.
물론 윈도우에서는 2000개 3000개 잘 생성됩니다.
Linux 를 default 로 설치 하고 java 실행을 해본 결과
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start(Native Method)
at ThreadOverhead.overheadTest(ThreadOverhead.java:21)
at ThreadOverhead.main(ThreadOverhead.java:6)
이런 에러가 발생합니다. 물론 이건 OutOfMemoryError 가 아닌 native thread 생성 에러 입니다.
아래 프로그램을 컴파일 하고 돌려 보시면 알수 있을 겁니다.
(마이크로 소프트 2002 년 기사중에 있던 ThreadOverhead.java 라구 쓰레드의 컨텍스트 전환에 대한 시간 측정 프로그램을 좀더 정확하게 측정하게 수정한것입니다. )
>java ThreadOverhead 1000
>java ThreadOverhead 2000
>ps -ef | grep ThreadOverhead | wc // native 쓰레드 개수를 확인 , +1 은 grep 프로세스 일겁니다. (그냥 빼고 생각하세요)
1025
혹시 linux 에서 1024개 이상 쓰레드를 생성하신 분 있으면 리플 부탁 드립니다.
----------------------------------------------------------
public class ThreadOverhead implements Callback {
private int threadCount;
private long totalMsec;
public static void main(String[] args) throws Exception {
new ThreadOverhead().overheadTest(Integer.parseInt(args[0]));
}
public void overheadTest(int threadCount) throws Exception {
this.threadCount=threadCount;
TestThread[] threads = new TestThread[threadCount];
System.out.println();
for(int i=0;i<threads.length; i++)
{
threads[i] = new TestThread(this);
System.out.print("\r"+i);
}
System.out.println("\nthread num : "+threads.length);
System.out.println();
for(int i=0;i<threads.length; i++)
threads[i].start();
while(true) {
Thread.sleep(100);
//System.out.println("\r"+TestThread.Count);
if(TestThread.Count==threads.length)
break;
}
System.out.println("threads started");
synchronized(TestThread.SyncObj) {
TestThread.SyncObj.notifyAll();
}
waitForAllThreads();
System.out.println("thread num : "+threadCount );
System.out.println("threads(s) : " + (totalMsec / threadCount));
}
public synchronized void reset() {
this.totalMsec=0;
}
public synchronized void workCompleted(long msec) {
totalMsec += msec;
threadCount -- ;
this.notify();
}
public synchronized void waitForAllThreads() throws Exception {
while(threadCount> 0) this.wait();
}
}
interface Callback {
void workCompleted(long msec) ;
}
class TestThread extends Thread {
private Callback callback ;
public static Object SyncObj=new Object();
public static int Count=0;
public TestThread(Callback callback)
{
this.callback = callback ;
}
public void run() {
try {
synchronized(SyncObj) {
Count++;
}
synchronized(SyncObj) {
SyncObj.wait();
}
long startMsec = System.currentTimeMillis();
for(int i=0;i<1000; i++)
Thread.sleep(10);
callback.workCompleted(System.currentTimeMillis() - startMsec);
} catch(Exception e) {
e.printStackTrace();
}
}
}
자바 관련 내용은 아니지만...결국 비슷한 얘기 같네요.다음
자바 관련 내용은 아니지만...
결국 비슷한 얘기 같네요.
다음 스레드를 참고하세요.
http://bbs.kldp.org/viewtopic.php?t=368&highlight=THREADS_MAX
우리 모두 리얼리스트가 되자. 그러나 가슴에 이룰 수 없는 꿈을 가지자
쓰레드 생성은....
운영체제 마다 다른데
쓰레드 스텍이 디폴트로 얼마가 잡혀 있는냐에 따라 다를겁니다
운영체제종에 따라 , 프로세스가2-3기가의 메모리 영역을 쓸수 있는데
쓰레드가 스텍 공간을 얼마나 사용하는가에 따라...... 문제가 되는것이지요
자바로 짜든 C로 짜든 그건 마찬가지 일겁니다
윈도우는 쓰레드 스택을 디폴트로 1메가를 잡기 때문에.....
2천개 정도 잡으면 쓰레 생성이 안됩니다
서버급이라면 3천개도 가능하지만요
[VC++에선 컴파일러 옵션으로 조절이 좀 가능하긴 합니다만 리눅스에선 어떻게 할지 잘 모르겠네요]
그럼
승자는 자기보다 우월한 사람을 보면 존경심을 갖고 그로부터 배울 점을 찾지만 패자는 자기보다 우월한 사람을 만나면 질투심을 갖고 어디 구멍난 곳이 없는지 찾는다.
- 하비스
댓글 달기