Java memory allocation
• Register: the program counter, which is thread private, is a pointer to the method bytecode in the method area.
• Static field: static member defined by static.
• Constant pool: identified at compile time and saved ina.class file (final)
• Non-RAM storage: Permanent storage space such as hard disks.
• Heap memory: Objects and arrays created by new are managed by the Automatic Java virtual machine garbage collector and are accessed slowly.
• Stack memory: Variables of basic types and reference variables of objects (the access address of the heap memory space) are fast and can be shared, but the size and lifetime must be determined, which lacks flexibility.
What is GC? Why GC?
GC stands for garbage collection, and memory handling is where programmers are prone to problems. Forgotten or incorrect memory collection can lead to programs or
Describe the Java garbage collection mechanism.
• Objects referenced in the virtual machine stack
• The object referenced by the static property of the method area class
• Objects referenced by the method area constant pool
• Objects referenced by the local method stack JNI
Advantages and principles of garbage collection. Two recycling mechanisms are considered.
A notable feature of the Java language is the introduction of garbage collection mechanism, which makes C++ programmers’ most troublesome memory management problem solved. It makes Java programmers no longer need to consider memory management when writing programs. Thanks to a garbage collection mechanism, objects in Java are no longer scoped; only references to objects are scoped. Garbage collection can effectively prevent memory leaks and efficiently use available memory. The garbage collector is usually run as a single low-level thread, cleaning and collecting objects in the heap that have died or have not been used for a long time in unexpected circumstances. The programmer cannot call the garbage collector in real time to collect an object or all objects.
What do system.gc () and Runtime.gc() do?
What is the structure of the Java heap? What is Perm Gen space in the heap?
The JVM’s heap is the run-time data area on which all instances and arrays of classes are allocated memory. It is created when the JVM starts. The heap memory occupied by objects is automatically controlled
Is there a memory leak in Java? Please describe it briefly.
The following code can see the memory reclamation in this case:
import java.io.IOException; public class GarbageTest {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
try {
gcTest();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(“has exited gcTest!” );
System.in.read();
System.in.read();
System.out.println(“out begin gc!” );
for(int i=0; i<100; i++){
System.gc();
System.in.read();
System.in.read();
}
}
private static void gcTest() throws IOException {
System.in.read();
System.in.read();
Person p1 = new Person();
System.in.read();
System.in.read();
Person p2 = new Person();
p1.setMate(p2);
p2.setMate(p1);
System.out.println(“before exit gctest!” );
System.in.read();
System.in.read();
System.gc();
System.out.println(“exit gctest!” );
}
private static class Person{
byte[] data = new byte[20000000];
Person mate = null;
public void setMate(Person other){
mate = other;
}
}
}
Memory leaks in Java:
public class Stack {
private Object[] elements=new Object[10];
private int size = 0;
public void push(Object e){
ensureCapacity();
elements[size++] = e;
}
public Object pop(){
if( size == 0) throw new EmptyStackException();
return elements[–size];
}
private void ensureCapacity(){
if(elements.length == size){
Object[] oldElements = elements;
elements = new Object[2 * elements.length+1];
System.arraycopy(oldElements,0, elements, 0, size);
}
}}
What does it matter. Let’s look at two examples.
public class Bad{
public static Stack s=Stack();
static{
s.push(new Object());
s.pop(); // There is a memory leak on an object
s.push(new Object()); // The above object can be reclaimed, i.e. from
}}
Deep copy and shallow copy.
Person p=new Person(” Person “);
When is the Finalize () method called? What is the purpose of finalization?
So what does Finalize () do?
If a reference to an object is set to NULL, does the garbage collector immediately free the memory occupied by the object?
What is Distributed Garbage Collection (DGC)? How does it work?
DGC is called distributed garbage collection. RMI uses DGC for automatic garbage collection. Because RMI contains references to remote objects across virtual machines, garbage collection is difficult. DGC uses reference counting algorithms to provide automatic memory management for remote objects.
Briefly describe the Java memory allocation and reclamation policy and the Minor and Major GC.
• Objects are allocated in the Eden area of the heap first
• Big objects go straight to the old age
• Long-lived objects will go straight to the old age
Full /Major GC occurs in the old generation. Normally, the old generation GC does not trigger the Minor GC, but can be configured to trigger the Full GC
Does garbage collection occur in JVM permanent generations?
Native memory area.
What are the methods of garbage collection in Java?
1.
2.
Replication algorithm:
Mark – Finish:
Collection by generation:
What is a class loader, and what are class loaders?
• The Bootstrap ClassLoader is used to load Java core class libraries and cannot be referenced directly by Java programs.
• Extensions Class Loader: It is used to load Java extension libraries. The Implementation of the Java Virtual machine provides an extension library directory. The class loader finds and loads Java classes in this directory.
• System Class Loader: It loads Java classes based on the Java application’s CLASSPATH. In general, Java application classes are loaded by it. Through this. GetSystemClassLoader () to get it.
• User-defined class loaders are implemented by inheriting java.lang.ClassLoader classes.
Class loader parent delegate model mechanism?
1. Memory model and partition, need to detail what to put in each partition.
The JVM is divided into a heap and a stack, and a method section, where initialized objects are placed on the heap, references are placed on the stack, and the class information constant pool (static constants and static variables) is placed on the method section
· Method area: mainly stores class information, constant pool (static constants and static variables), compiled code (bytecode) and other data
· Heap: Initialized objects, member variables (non-static variables), and all object instances and arrays are allocated on the heap
· Stack: the structure of stack is composed of stack frames. When a method is called, a frame is pressed into the frame. Local variable table, operand stack and method output are stored on the frame
· Local method stack: mainly serves Native methods
· Program counter: Records the line number executed by the current thread
2. Partitions in the heap: Eden, Survival (from+ to), old age, their respective characteristics.
3. Object creation method, object memory allocation, object access location. New an object
4. Two determination methods of GC:
What is SafePoint
1.
2.
3.
4.
6. What are the principles and characteristics of the three GC collection methods: tag clearing, tag sorting and copy algorithm? What are your thoughts on how to optimize the collection method?
What are the GC collectors? Features of CMS collector and G1 collector.
G1 is a collector based on a mark-tidy algorithm as a whole, and a copy algorithm locally (between two regions)
When does the Minor and Full GC occur
Jstack looks at the current stack, JMap looks at the memory, and Jhat dumps the heap
Major GC
1.
2.
3.
Java class loading process?
Java class loading goes through seven processes:
1. The load
• Gets the binary stream of a class by its fully qualified name.
• Convert static storage structures in this binary stream to methods to run the data junction
• Generate an in-memory Class object for the Class as a data access entry for the Class.
2. Verify
• Bytecode verification: it is the most complex stage in the whole verification process. Through the analysis of the verification data flow and control flow, the correctness of the program semantics is determined, mainly for the verification of the method body. For example: method type conversion is correct, jump instruction is correct and so on.
• Symbol reference validation: This action occurs later in the parsing process, mainly to ensure that the parsing action is performed correctly.
3. Prepare
public static int value=123; // During the preparation phase value the initial value is 0. It only becomes 123 during initialization.
4. The parsing
5. The initialization
6. Use
7. Remove
Describe how the JVM loads Class files.
The Java language is a dynamic interpreted language, and classes cannot run until they are loaded into the JVM. When the specified program is run, the JVM compiles it
The main steps of class loading:
• load. Find the corresponding class file according to the search path, and then import.
• links. Links can be broken down into 3 small steps:
• Check the correctness of the class file to be loaded.
• Prepare to allocate storage space for static variables in a class.
• Parse to convert symbolic references to direct references (this step is optional)
• Initialization. Performs initialization work on static variables and static code blocks.