physical memory vs. virtual memory
1. Physical memory
(1) RAM
The so-called physical memory is what we commonly call RAM (random memory).
(2) Register
In the computer, there is also a storage unit called Register, which is used to store the intermediate results of the calculation unit execution instruction (such as floating point, Integer, etc.). The size of the register determines the maximum number of values that can be used for a calculation.
(3) Address bus
Connecting the processor to the RAM or processor and register is address bus, and the width of the address bus affects the index range of the physical address, because the width of the bus determines how many bits the processor can get from the register or memory at a time. It also determines the maximum addressable address space for the processor, such as the range of 32-bit address buses that can be addressed to 0x0000 0000~0XFFFF FFFF. This range is 232=4 294 967 296 memory locations, each address will reference one byte, so the 32-bit bus width can have 4GB of memory space. Typically, address bus and registers or RAM have the same number of bits because it makes it easier to transfer data.
(4) Memory address
The running program needs to request the memory address from the operating system, the operating system manages the memory according to the process, each process has a separate address space, each process does not overlap each other, that is, each process can only access its own memory space.
2. Virtual memory
The independence of the process memory space is logically independent, which is guaranteed by the operating system, but the real physical memory is not necessarily used by a single process. With the complexity of the program, the physical memory can not meet its needs, in this case, there is the emergence of virtual memory.
The advent of virtual memory allows multiple processes to share physical memory, where sharing is only spatially shared, and logically they are still not accessible to each other. Virtual memory not only allows processes to share physical memory, improve memory utilization, but also expands the address space of memory, such as a virtual memory address that may be mapped to a piece of physical memory, a file, or other addressable storage.
When a process is inactive, the operating system moves the data in this physical memory to a disk file, which is swap partitions, and the truly efficient physical memory is left to the active program. In this case, when we re-awaken a program that has not been used for a long time, the disk will squeak, and a brief pause will confirm that the operating system will re-interact the data on the disk into physical memory. It is important to avoid this situation because the operating system frequently interacts with physical memory data and disk data, which is inefficient, especially on Linux servers.
We want to focus on the activity of the swap partition in Linux, and if the swap partition is used frequently, the system will be very slow, which probably means that the physical memory is severely inadequate or some programs are not releasing memory in time.
second, kernel space and user space
A computer usually has a certain size of memory space, such as the address space of 4GB, but the program does not fully use these address space, because these address space is divided into kernel space and user space. The program can only use the memory of the user space, which refers to the use of the program can give the application of memory space, not the program really access the address space.
Kernel space mainly refers to the program logic used by the operating system for program scheduling, virtual memory use, or to connect hardware resources. The partition of kernel space and user space is for the security and stability of the system, but it also sacrifices a part of efficiency. System calls are in the kernel space has a system to initiate, such as: Network transmission, the data transmitted over the network from the core space to receive the data from the remote host, and then from the kernel space to the user space for the user program to use, each time such a system call will have two memory space to switch. But there are a number of other technologies that can reduce the way data is replicated from kernel space to user space, such as: Linux systems provide sendfile file transfer.
In the current Windows 32-bit operating system, the default kernel space and user space ratio is 1:1 (that is, 2GB kernel space, 2GB user space), and the default ratio of 32-bit Linux system is 1:3 (1GB kernel space, 3GB user space).
third, which components in Java need to use memory
1. Java Heap
The Java heap is the area of memory used to store Java objects, and the heap size is applied to the operating system once the JVM is started,-xmx and-xms two options to control the size, XMX represents the maximum size of the heap, and XMS represents the initial size. Once the allocation is complete, the heap size is fixed and cannot be re-applied to the operating system when there is insufficient memory, and the extra space cannot be exchanged to the operating system when memory is idle.
The management of the memory space in the Java heap is controlled by the JVM, and object creation is controlled by the Java application, but the space occupied by the object is freed by the garbage collector that manages the heap memory. Depending on the garbage collection (GC) algorithm, the way and timing of memory recycling can vary.
2. Threads
The entity in which the JVM runs the actual program is a thread, and of course the thread needs memory space to store some necessary data. Each time a thread is created, the JVM creates a stack for it, and the stack size differs depending on the JVM implementation, usually between 256kb~756kb.
The thread occupies less space than the heap space. However, if there are too many threads, the total memory usage of the thread stack can be very large. There are currently many applications that allocate the number of threads created based on the number of cores of the CPU, and if the number of threads running the application is much larger than the number of processors available to process them, the efficiency is usually low and may result in poor performance and higher memory usage.
3. Class and Class loader
Classes in Java and Class loaders that load classes themselves also need storage space, and in the sun JDK they are stored in the heap, which is called the Permanent Generation (PermGen zone).
The JVM loads classes on demand, and the JVM only loads the classes that are explicitly used in your application into memory. To see what classes the JVM is loading, you can add-verbose:class to the startup parameters.
The more Java classes you use in theory, the more memory you will need to occupy, and the other case where you may be repeatedly loading the same class. Typically, the JVM will load only one class to memory at a time, but if its own implementation of the ClassLoader will be repeated loading, if the PermGen zone cannot unload the already defunct classes, it may cause memory leaks in the PermGen area. Usually a class can be unloaded, and the following conditions need to be satisfied:
(1) There is no reference to the Java.lang.ClassLoader object representing the ClassLoader in the Java heap.
(2) There is no reference to any Java.lang.Class object in the Java heap that represents a class loaded by the ClassLoader.
(3) All objects of any class loaded by the ClassLoader on the Java heap no longer exist (referenced).
It is important to note that the 3 default class loaders created by the JVM, Bootstrap ClassLoader, Extclassloader, and Appclassloader, are unlikely to meet these conditions, so Any system (such as java.lang.String) or any application class loaded through the application ClassLoader cannot be freed at run time.
4. NIO
Java added a new I/O Class (NIO) class library after version 1.4, introducing a new way to perform I/O based on channels and buffers. NiO allocates memory using the Java.nio.ByteBuffer.allocateDirect () method, which uses native memory instead of memory on the Java heap, and each time it allocates memory invokes the operating system's Os::malloc () function.
5. JNI
JNI technology enables native code (such as a C language program) to invoke Java methods, which are commonly referred to as native memory. In fact, the Java runtime itself relies on JNI code to implement class library functions such as file operations, network I/O operations, or other system calls. So JNI also increases the native memory footprint of the Java runtime.
iv. JVM Memory Structure
The JVM divides the memory structure according to the storage structure of the runtime data, and the JVM divides them into several different formats when running Java programs, which are stored in different regions, which are collectively known as runtime data. The runtime data includes the data information of the Java program itself and the additional data information required by the JVM to run the Java program, such as a pointer to the current program instruction execution (also known as a PC pointer). In the Java Virtual Machine specification, the Java Runtime data is divided into 6 types, which are described separately below.
1. PC Register
The PC register is strictly a data structure that holds the memory address of the program that is currently executing normally. At the same time, the Java program is multi-threaded execution, it is not possible to always follow the linear execution, when there are multiple threads cross-executing, the interrupt thread of the program is currently executed to which the memory address must be saved, so that it is resumed when the execution of the command address to continue execution.
2. Java stack
The Java stack is always associated with threads, and whenever a thread is created, the JVM creates a corresponding Java stack for that thread, and in this Java stack there will be multiple stack frames (Frames) that are associated with each method, creating a stack frame each time a method is run. Each stack frame contains information such as internal variables (variables defined within the method), operation Stacks, and method return values.
Whenever a method executes, the stack frame pops up the stack frame element as the return value of the method, and clears the stack frame, and the stack frame at the top of the Java stack is the active stack currently executing, which is the current executing method, and the PC register points to the address. Only the local variables of this active stack frame can be used by the manipulation stack, and when another method is called in the stack frame, a new stack frame corresponding to it is created. This newly created stack frame is then placed at the top of the Java stack and becomes the current active stack frame. Also now only the local variables of this stack frame can be used, when all the instructions in this stack frame is completed when the stack frame moved out of the Java stack, just now the stack frame into the active stack frame, the back of the stack frame before the return value into the stack frame in the operation of an operand. If the previous stack frame does not have a return value, the operand of the current stack frame's Operation stack does not change.
Since the Java stack is associated with a Java thread, this data is not shared by the thread, so we do not have to worry about its data consistency, and there is no synchronization lock problem.
3. Heap
The heap is where Java objects are stored, which is the core storage area of the JVM's managed Java objects, and the heap is the most important concern for Java programmers because it is the storage area where our applications are most closely related to memory.
Each Java object stored in the heap is a copy of the object's class, and it assigns values that include all non-static properties inherited from its parent class.
The heap is shared by all Java threads, so access to it requires attention to synchronization issues, and the methods and corresponding attributes need to be consistent.
4. Method area
The JVM method area is where the class structure information is stored, such as parsing a class file into several parts that the JVM can recognize, which are stored in different data structures when the class is loaded into the JVM, with constant pools, fields, method data, method bodies, constructors, Includes specialized methods in the class, instance initialization, and interface initialization are stored in this zone.
This storage area of the method area is also part of the Java heap, which is what we typically call a permanent zone in the Java heap, which can be shared by all threads, and its size can be set by parameters.
5. Running a constant-rate pool
In the JVM specification, this defines a data structure that runs a constant pool: The runtime Constant pool represents the constants table in each class file at run time. It includes several constants: Numeric constants for the compilation period, methods, or references to the domain (parsing at run time). The Runtime Constant pool functions like a symbol table in a traditional programming language, although it contains much more data than a typical symbol table. Each runtime Constant pool is assigned in the method area of the JVM, and each class or interface Constant pool is created when the JVM creates a class or interface.
The run-time-constant pool is part of the method area, so its storage is also constrained by the specification of the method area, and OutOfMemoryError is thrown if the constant pool cannot be allocated.
6. Local Method Stack
The local method stack is the space prepared for the JVM to run the native method, which is similar to the Java stack described earlier, because many of the native methods are implemented in C, so it is often called C-stack, except that the usual native methods contained in our code will use this storage space , the JVM uses JIT technology to recompile some Java methods into native code codes, which are often used to track the execution state of a method using this stack.
There is no strict restriction on this area in the JVM specification, it can be implemented freely by different JVM implementations, but it will also throw OutOfMemoryError and stackoverflowerror like other stores.
v. JVM memory allocation policy
1. Common Memory allocation policy
In the operating system, the memory allocation policy is divided into three types, namely:
(1) Static memory allocation
Static memory allocation is when the program compiles to determine the storage space requirements for each data at run time, so they can be allocated a fixed amount of memory at compile time. This allocation policy does not allow for the existence of mutable data structures (such as mutable arrays) in program code, nor does it allow nested or recursive structures to occur because they cause the compiler to be unable to calculate the exact storage space requirements.
(2) Stack memory allocation
A stack of memory allocations can also be called dynamic storage allocations, implemented by a stack-like run stack. In the stack memory scheme, the requirements of the program to the data area are completely unknown at compile time, only to be known at runtime, but it is necessary to know the size of the data area required by the program module in order to allocate memory for it when it enters a program module in operation. Stack memory allocations are distributed according to the principle of advanced post-out.
(3) Heap memory allocation
In addition to compiling the program to determine the storage space of the data and at the entrance of the program can know the storage space, there is another situation is when the program really run into the corresponding code to know the size of the space, in this case we need to heap this allocation strategy.
In these memory allocation strategies, it is obvious that the heap allocation policy is the most liberal, but this allocation strategy is a challenge for the operating system and memory management programs. In addition, this dynamic memory allocation is executed when the program is running, and it is inefficient to run.
2. Detailed memory allocation in Java
JVM memory allocation is based on two main types, namely heap and stack.
(1) Stack
The allocation of Java stacks is tied to threads, and when we create a thread, it is clear that the JVM will create a new Java stack for this thread, the invocation of a thread's method and the return of the stack and the stacks that correspond to the Java stack. When a thread activates a Java method, the JVM presses a new frame into the Java stack of threads, which naturally becomes the current frame. During the execution of this method, this frame is used to hold parameters, local variables, intermediate calculation processes, and other data.
The stack mainly stores some basic types of variable data (int, short, long, byte, float, double, Boolean, char) and object handle (reference). Access speed is faster than heap, second only to register, stack data can be shared, the disadvantage is that the data size and lifetime of the stack must be determined, which also leads to lack of flexibility.
(2) heap
The Java heap is a run-time data area that is established by directives such as new, NewArray, Anewarray, and Multianewarray, which do not require program code to be displayed to be released. Heap is responsible for garbage collection, the advantage of the heap is the ability to dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, because it is at runtime to allocate memory dynamically, Java garbage collector will automatically take away these no longer use data. However, the disadvantage is that the memory is allocated dynamically at runtime and the access speed is slow.
Each Java application uniquely corresponds to a single JVM instance, and each instance uniquely corresponds to one heap. All instances or arrays of classes created by the application in the run are placed in this heap and shared by all threads of the application. Allocating heap memory in Java is automatically initialized, all objects are allocated in the heap, but the reference to the object is allocated on the stack, that is to say, when an object is created, two places are allocated memory, the memory allocated in the heap actually establishes this object, The memory allocated in the stack is just a pointer (reference) to the heap object.
vi. JVM Memory recovery policy
1. Static memory allocation and recovery
Static memory allocation in Java is the ability to determine the required memory space when Java is compiled, and the system allocates memory to it once the program is loaded. The memory does not change when the program executes, and the memory is not recycled until the program executes. Local variables in Java classes and methods include native data types (int, long, char, and so on), and references to objects are statically allocated memory.
2. Dynamic memory allocation and recovery
The so-called dynamic allocation is when the program executes to know the size of the storage space to allocate, rather than at compile time can be determined. The allocation of memory occurs when the object is created, and the memory is recycled as if the object is no longer referenced. Dynamic memory allocation and recycling is associated with some data types in Java, and their recycling is handled by the garbage collector.
3. How to detect garbage
The garbage collector must be able to do two things: one is to detect the garbage object correctly, and the other is to be able to free up the memory space occupied by the garbage object. How to detect garbage is the key to the garbage collector. This object can be recycled as long as an object is no longer referenced by another active object.
4. Garbage collection algorithm based on generational separation
The design of the algorithm is: the object according to the length of life to group, divided into young and old generation, the newly created objects are divided in the young generation, if the object after a few recovery still survive, then the object is divided into the elderly generation. The collection frequency of older generations is not as frequent as that of younger generations, which reduces the number of objects scanned per garbage collection, thus improving garbage collection efficiency.
The JVM divides the entire heap into young, old, and perm areas, each of which holds objects of different ages.
Http://blog.chinaunix.net/uid-27064719-id-4477390.html
JVM memory Management (RPM)