Test parameter settings:
1. Looping to call new A() to achieve heap overflow, java.lang.OutOfMemoryError: Java heap space,
Virtual machine parameters: -Xms1M -Xmx1M -XX:+HeapDumpOnOutOfMemoryError, Explanation: Setting -Xmx and -Xms to the same can avoid the heap automatic expansion. -XX:+HeapDumpOnOutOfMemoryError allows the virtual machine to dump the current heap memory dump snapshot when a memory overflow exception occurs.
// while (true){// new A().do2();// }2. Looping to call object references to achieve stack overflow. java.lang.StackOverflowError,
Virtual machine parameters: -Xss128k,
Explanation: Set the size of the virtual machine stack to 128kn
Under a single thread, the above error will be thrown regardless of whether the stack frame is too large or the virtual machine stack capacity is too small and the memory cannot be allocated.
void do2(){ do2();};3. Looping to call the String.intern() method to write to the constant pool, and the constant pool overflows. java.lang.OutOfMemoryError: PermGen space
while (true) { list.add(String.valueOf(i++).intern()); }Virtual machine parameters: -XX:PermSize=10M -XX:MaxPermSize=10M,
Explanation: Indicates the capacity and maximum capacity of the permanent generation initially allocated by the JVM. (The permanent area has insufficient memory, and it is all on the heap after 1.8. Method area = permanent generation, PermGen space", that is, permanent generation)
Four reference states: (class inheritance extends WeakReference<Car>)
1. Strong quotation: Object obj = new Object(). As long as the strong quotation still exists, the garbage collector will never collect the referenced object;
2. SoftReference: SoftReference, the objects associated with soft reference will be recycled when there is insufficient memory. If there is not enough memory in this recycling, a memory overflow exception will be thrown; SoftReference<byte[]> sr = new SoftReference<byte[]>(bytes);
3. Weak reference: WeakReference, objects associated with weak references can only survive until the next garbage collection;
4. Virtual reference: PhantomReference, virtual reference is used to track the recycling status of objects.
Temporary time to trigger GC:
1. When the younger generation or the older generation is full, the Java virtual machine can no longer allocate memory space for new objects, then the Java virtual machine will trigger a GC to recycle objects that will no longer be used.
2. System.gc(), Runtime.getRuntime().gc() methods usually trigger a Full GC and at least one Minor GC, and it may not be recycled immediately.
3. Triggered when the server is idle or the elderly are in the heap.
Recycle the space occupied by the referenceless object, not the object itself.
But it is also unpredictable at what point the real garbage collection mechanism will start to take action (unopen source), which is the same principle as the preemptive thread when it takes effect.
Generational collection algorithm:
New generation collectors and senior generation collectors.
Mark-clearing algorithm
Mark-organization algorithm
The difference between memory overflow and memory leakage:
1. Memory overflow: The program does not have enough space when allocating memory.
2. Memory leak: After a program applies for memory, it has no way to free up memory. It always occupies memory, that is, the allocated object can be reached but is useless. Memory leaks are usually caused by a large object in the memory, but cannot be released. Will cause memory overflow.
Parallel and concurrency:
1. Parallel: refers to the operation of multiple garbage collector threads;
2. Concurrency: refers to the user thread and the garbage collector thread work simultaneously.
Class loading mechanism:
1. Loading (generating java.lang.class object), verification, preparation, parsing, initialization (assignment process), use (using) and unloading (unloading)
2. Verify (whether the information contained in the byte stream is correct and whether it complies with jvm),
Prepare (assign value to class variables, i.e. static variables, assign 0),
The three parts of parsing (replace symbolic references with direct references, class file to memory) are collectively called linking (Linking)
3. Loading stage: Get the binary stream of the .class file;
Put the contents of class information, static variables, bytecodes, and constants into the method area
Generate a java.lang.Class object representing this .class file in memory, as an access portal for various data in the method area of this class.
4. What you do in the initialization stage is to call the client> method to assign the static variable to the user specified value and execute static code blocks.
Parent delegation model: (Start class loader > Extended class loader > Application class loader > Custom class loader)
A class loader receives a class loading request and delegates the request to the parent class loader to complete. Only after the parent class fails to complete it will it complete it yourself.
Principle of occurrence:
It is not possible to rely solely on synchronized and volatile to constrain order in Java memory. It depends on program call order rules to determine the order of setters and getters.
Minor GC: Clean up young belt memory, unable to allocate space for a new object.
Major GC: It is to clean up the old age, and many Major GCs are triggered by Minor GC
Full GC: It is to clean up the entire heap space - including the younger generation and the older generation