Java 9 High Performance (一)

CPU infrastructure

Central Processing Unit (CPU) is an integral part of the computer system. The core responsibility of a CPU is to handle all the instructions from hardware as well as software. If you dissect the processing of a CPU, you will notice there are two main components, one is Arithmetic Logic Unit (ALU), which is responsible for performing logical, mathematical, and comparison operations. And the second one is Control Unit (CU), which is responsible for directing all of the processor's operations.

  • The Control Unit: The core function of this unit is to execute program instructions. As its name suggests, CU only directs and does not execute instructions by itself. It communicates with ALU and memory to perform its operation.
  • The Arithmetic/Logic Unit: As you may have guessed already, ALU executes all arithmetic operations such as addition, subtraction, multiplication, and division and logical operations such as comparison. Without this unit, your application won’t be able to perform calculations such as processing order payments and booking train or plane tickets. I hope now you can see the importance of this unit.

Here are two types of storage available, primary and secondary.

Primary storage is more closer to CPU and hence faster to use by CPU to process data and instructions. This storage is a temporary storage whereas secondary storage, like a hard drive, is permanent. It is always more expensive to process instructions from secondary storage than primary. The primary storage includes CPU registers, cache, and Random Access Memory (RAM). CPU registers are closer than cache and cache is closer than RAM. CPU registers are faster than cache and cache is faster than RAM. 

When you execute your application, JVM looks for the main method and creates a thread, which is called the main thread. The main thread is then used to derive any new thread you create in the code. This main thread is a process for the underline operating system and the threads that you create are created as a lightweight process in the operating system. 

Java heap versus stack memory

I hope the preceding example and explanation gave you a good idea about how JVM uses heap and stack. So, let’s just summarize these two points and note down a few key differences as follows:

  • Stack is faster than heap memory.
  • Stack provides a storage for local variables, including primitive and object variables referencing to objects in the heap memory.
  • Stack is LIFO, whereas heap is not.
  • Stack memory is smaller than heap memory.
  • Stack is specific to one thread of execution, whereas Heap memory is available to all parts of the program.
  • As mentioned earlier, stack is specific to one thread of execution so only the current executing thread can access stack memory whereas heap memory is accessible globally.
  • Stack gets cleared up after the completion of currently executing method, whereas heap stays for a long time and depends on the garbage collector to free up some space.
  • A very important point to note here is that it will help you to analyze the performance issue for your application. JVM throws java.lang.StackOverFlowError when the stack gets full and throws java.lang.OutOfMemoryErrorwhen the heap is full.

Escape analysis

Objects live in the heap memory and heap memory is slower than stack. Escape analysis can help determining if an object may escape the method, which is not local and may escape the thread that is its creator. If the object does not escape a method, it removes the need for creation of the object. In case of a thread, if the object does not escape a thread, then no other thread accesses the object, which means there is no need for synchronization for this object. Also, if the object is local to a thread, it means it can be allocated in the same memory location as the thread, which improves the data locality.

猜你喜欢

转载自www.cnblogs.com/codingforum/p/9196941.html