Causes of Thread Overflow
The term "thread overflow" is broadly used interchangeably to refer to two main situations: memory exhaustion within a single thread (stack overflow) and exceeding the system-wide thread creation limit (thread exhaustion).
What is Thread Overflow? #
When a program executes, the operating system creates execution units called threads to handle tasks.
Each thread is allocated its own independent stack memory space to store function call history, local variables, and so on.
Thread overflow or related failures occur when the memory space allocated to a thread becomes full, or when too many threads are created, exceeding the system's capacity and depleting system resources.
When this error occurs, the affected process (application) often can no longer function normally and is forcibly terminated.
Two Main Causes of Thread-Related Overflow #
Depending on where and why they occur, these can be broadly divided into two situations.
Stack Overflow #
This occurs when an attempt is made to push data beyond the stack memory size typically allocated to a single thread, which is usually around 1-2MB.
- Occurs when stack frames accumulate endlessly due to infinite recursion, such as a function continuously calling itself without a termination condition.
- Also occurs when a single function declares excessively large arrays or objects as local variables, depleting stack memory all at once, similar to excessive local variable allocation.
- Occurs when a logic involving circular references, such as object A calling B and B calling A again, repeats.
Thread Exhaustion / OOM #
This is not a memory issue within a thread, but rather occurs when an application attempts to create too many threads, hitting operating system (OS) limits or overall memory limits.
- Main Causes
- Lack of Thread Pool Usage: Incorrect design that creates an unlimited number of
new Thread()instances every time traffic comes in. - Missing Resource Release: Threads are not terminated or returned after completing their work, and instead accumulate in a waiting (zombie) state.
- OS Limit Reached: Exceeding the maximum number of processes/threads that can be created, as configured in the operating system (e.g., Linux).
- Lack of Thread Pool Usage: Incorrect design that creates an unlimited number of
In summary, stack overflow is a memory issue within a single thread, with java.lang.StackOverflowError being a typical error name. The approach to resolve it involves checking recursion termination conditions or modifying the logic.
Thread exhaustion is a resource and thread count issue for the entire process, with java.lang.OutOfMemoryError:unable to create new native thread being a typical error name. The approach to resolve it involves applying thread pools and tuning Tomcat/OS thread limit settings.
Example Data and Result Sets #
The simplest way to cause a stack overflow is an infinite recursive function call without a termination condition.
public class OverflowExample {
// 자기 자신을 무한히 호출하는 메서드
public static void recursiveCall(int number) {
System.out.println("Current Number: " + number);
// 종료 조건이 없으므로 계속해서 스택에 쌓임
recursiveCall(number + 1);
}
public static void main(String[] args) {
recursiveCall(1);
}
}
Current Number: 10483
Current Number: 10484
Exception in thread "main" java.lang.StackOverflowError
at java.base/sun.nio.cs.UTF_8.updatePositions(UTF_8.java:58)
at OverflowExample.recursiveCall(OverflowExample.java:6)
at OverflowExample.recursiveCall(OverflowExample.java:6)
... (수천 줄의 동일한 에러 반복)
Thread exhaustion error logs, when an application creates an unmanageable number of threads, appear in backend logs for Spring Boot and Tomcat as follows:
Exception in thread "http-nio-8080-Acceptor" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:717)
at org.apache.tomcat.util.net.NioEndpoint.setSocketOptions(NioEndpoint.java:444)
This indicates an OutOfMemory (OOM) error due to the inability to allocate new native threads from the OS, suggesting a traffic surge or thread leak.
Let's look at commands to check and adjust OS-level thread limits. If a thread exhaustion error has occurred, check if the server's OS settings are set too low.
# 1. 특정 유저가 생성할 수 있는 최대 프로세스/스레드 수 확인 (Soft/Hard Limit)
ulimit -u
# 2. 커널 시스템 전체에서 허용하는 최대 스레드 수 확인
cat /proc/sys/kernel/threads-max
# 3. 현재 실행 중인 특정 프로세스(예: PID 1234)가 생성한 스레드 개수 확인
cat /proc/1234/status | grep Threads
출력 결과 예시 (3번 명령어):
Threads: 205 (해석: 현재 1234번 프로세스는 205개의 스레드를 띄우고 있습니다.)