The process of learning Java is much easier than learning C + + and C, and to a certain extent, with the JVM's own garbage collection mechanism, in C or C + + after the creation of the array needs to be manually released, accidentally a memory leak occurs. The JVM helps us to automatically reclaim unused memory, which, of course, is replaced by efficiency.
How does the JVM determine if an instance should be recycled? There are two ways of doing this:
The reference counting method: When an instance is referenced by an object, the counter of that instance is incremented by 1, and when the counter in the instance is 0 o'clock, the description is not referenced by the object, and the JVM can then release the space of the instance. However, there is a problem, if two instances are referenced to each other, and these two instances will not be used again, the use of this method of two instances is not released.
The accessibility analysis method: This method is more advanced, it starts from main, for all instances to establish an accessible channel, if an instance is in an unreachable range, then the instance can be released. When the JVM frees the memory, it gives the instance that is about to be freed a chance to save itself, and if the instance is a copy of the Finalize () method, then the instance is placed in a queue, the Finalize () method is executed, and if the rescue is unsuccessful, the memory consumed by it is released directly.
There are 4 ways to refer to objects in Java, such as strong references, soft references, weak references, and virtual references, and here are some of their differences:
1, strong reference: the use of the most frequency of a way, normal use should be a strong reference, no matter how nervous the system's resources, strongly referenced objects will not be released.
2, soft reference: use is softreference<person> p = new softreference<person> (New person ("Rain")), and when system resources are tight, soft-referenced objects are recycled.
3, weak reference: The use is weakreference<person> p = new weakreference<person|> (New person ("Rain")), the weak reference is to be recycled when the memory will certainly be reclaimed.
4, Virtual reference: This is a special, virtual reference is used in conjunction with Referencequeue, it can not be used alone, when the instance of a virtual reference callout is recycled, the instance is placed in the queue to track the status of garbage collection of instances.
For garbage collection algorithms and methods in the JVM, start by understanding some basic concepts:
1, serial recovery: equivalent to only one thread in the recycling of resources.
2, parallel recovery: Multiple threads are working together to recycle resources.
3. Concurrent execution: When the work of recycling begins, all Java applications stop executing and wait for the collection to complete.
4, Tag-compression: Refers to the collection of all the space to be recycled, and then through the copy of the way, all the allocated space to one end.
5. Mark-clear: The indicator record the space to be reclaimed and clean it up directly.
6, Replication: The available memory space is divided into two, each time only use one of the memory space, in the garbage collection, the memory of the objects can be all copied to another space, and then the space for a single collection.
The JVM uses a generational recycling strategy in heap memory, a combination of the above recovery algorithms and methods that the JVM divides into new generation, Laosheng generations, and permanent generations.
There are three districts in the young generation, one Eden area and two survivor District. The newly generated object is placed in the Eden area, and when the Eden area is full, the surviving objects in the Eden area are copied into one of the survivor areas, and when the Survivour area is full, the surviving objects are copied to another survivor area, and when another area is full, The object in which it survives is transferred to the Laosheng generation area.
Some objects with longer life cycles are stored in the Laosheng region.
Some static files are stored in the last permanent generation.
The garbage collection mechanism in the JVM triggers in the following cases:
1, Eden area full, will trigger scavenge GC, this recovery will only affect the Cenozoic region, will not affect other areas.
2, when the permanent generation, Laosheng or display call System.GC (), will trigger the full GC, this efficiency is low, to try to avoid.
Java in-store recycle