Detailed explanation of the whole process of calculation and analysis of the number of threads carried by the server

How many threads can the server host in parallel at the same time?

What resources of the server need to be occupied by a thread during execution-CPU, memory

The number of threads that a server can hold is determined by the CPU or the memory?
Are threads processed in parallel or serially on the CPU? —Serial- Threads are processed by polling on the CPU-CPU processing speed determines the execution speed of threads, but CPU processing speed cannot determine the number of
threads. Thread generation and execution require memory. N threads can poll for processing on the CPU, but can n threads poll and occupy memory? —No. Even if the thread is in a blocked state, it still takes up memory, but it does not take up the CPU.

The number of threads that a server can host at the same time is mainly determined by memory

The relationship between the number of threads and memory structures

In Java, the memory is divided into 5
stacks: stack memory. Used to execute code blocks (logic). Variables are additional products during the execution of code blocks for the convenience of reading and understanding. Each thread has exclusive use of a stack memory
Heap: heap memory. Used to store objects. All threads share a heap memory
Method Area: method area. Used to store class information. All threads share a method area
Native Stack: native method stack. Used to execute local methods. A method modified with native but without a method body is called a native method. The method body of native methods is implemented in other languages. Most native methods are implemented in C/C++, and a few native methods are implemented in Python. Each thread has an exclusive local method stack
Program Counter: PC counter. Counting the threads determines the next instruction to be executed by the thread. Which instruction the PC counter points to, then which instruction the CPU will execute next. Each thread has an exclusive PC counter
*****Starting from JDK1.8, the method area has been cancelled and replaced by the meta space and permanent generation in the heap memory.

The number of threads that a server can hold will be determined by the stack memory, local method stack and the size of the PC counter. The
PC counter is very small, only a few bytes in size, generally no more than 4 bytes-
local methods can be ignored The size of the stack is uncertain, depending on the memory size of the underlying implementation language and logic. If it is small, it may be only a few KB, and if it is large, it may even cause a memory overflow-OutOfMemoryError
stack memory is specified in JDK1.8, and the minimum is 128KB

An 8-core 128G memory server can host multiple threads at the same time?

Extreme conditions: Ignore the PC counter and the local method stack size, and the stack memory is 128KB to calculate. The
memory is marked as 128G, and the actual memory is about 90%, which is 115.2GB
JVM regulations. The sum of all stack memory generally exceeds 2/3 of the actual memory. -> 115.2/3*2=76.8GB
76.8GB/128KB≈629146 In the
actual process, basic operations such as thread waiting and waking up and class loading are all local methods used, which leads to the local method stack that cannot be ignored, even The local method stack and stack memory size are basically the same

The number calculated above needs to be halved in the actual process, which means that a 128G server can actually carry no more than 35W threads at the same time.

Guess you like

Origin blog.csdn.net/qq_41536934/article/details/113703011