When a Java program starts running as a process in the operating system, what parts of the process need to allocate memory?
1 Java Heap
The Java heap is used to store Java objects, and the size of the heap is applied to the operating system once at the start of the JVM, using-XMX and-XMS to control the size. Where xmx represents the maximum value of the heap, XMS represents the initial value of the heap. Once the allocation is complete, the heap size is fixed and cannot be re-applied to the operating system when memory is insufficient, and the extra space cannot be returned to the operating system when memory is idle.
2 Threads
The entity on which the JVM runs the actual program is a thread, and the thread needs memory to store the data. Each time a thread is created, the JVM creates a stack for it, and the size of the stack differs depending on the JVM implementation.
Class 3 and Class loaders
Classes and class loaders are stored in the heap, which is called a permanent generation (PermGen zone). The JVM loads classes on demand and loads only one class into memory at a time.
4 NIO
jdk1.4 later introduced a new way of performing I/O based on channel and buffer NiO, using the Java.nio.ByteBuffer.allocateDirect () method to allocate native memory instead of memory on the Java heap.
5 JNI
JNI enables native code, such as a C language program, to invoke a Java method. In fact, the Java runtime itself relies on JNI to implement class library functionality, so JNI increases the native memory footprint of the Java runtime.
Resources
In-depth analysis of Java Web Technology Insider p201-203
Java Essentials components that need to use memory in Java