1. Types of Java references
Memory management is divided into: memory allocation and memory reclamation. are handled automatically by the JVM.
- The state of an object in memory: reachable, recoverable (call the Finalize method before recycling), unreachable
- JVM Recovery criteria: whether there are reference variables referencing the object
- Have to understand the graph. A thread object acts as a root node, a variable, an object as a node, and a reference relationship as a forward edge. In a graph, from thread nodes < of course, thread objects are also present, and objects that are not destroyed > are up to state.
- Strong references
- General references/Most are strong references, objects that are strongly referenced are not recycled, and are one of the main reasons for memory leaks.
- Soft references
- Implemented by the SoftReference class (usage of the Class)
- Only soft-referenced objects are not recycled when the system is fully memory, and are recycled when memory is tight
- Can be used to solve the problem of system memory tension
- Weak references
- String constants are cached by the system (which is referenced by a strong reference) and the cached string constants are not reclaimed by the system.
- When the garbage collection mechanism runs (with uncertainty), it is recycled (uncertainty), regardless of whether the system memory is tight.
- WeakReference class, Weakhashmap more commonly used
- Virtual reference
- Primarily used to track the state of an object being garbage collected: The program can see whether the object referenced by the virtual reference is about to be reclaimed by checking that the reference queue associated with the virtual reference already contains the specified virtual reference.
- Cannot be used alone, it needs to be combined with a reference queue
- Soft and weak references are used in conjunction with the reference queue, and after the system recycles the referenced object, the corresponding reference to the reclaimed object is added to the associated reference queue.
- When a virtual reference is used in conjunction with a reference queue, the virtual reference to it is added to the reference queue before the object is freed, making it possible to take action before the object is reclaimed.
- Cannot get the object it refers to by a virtual reference
2. Memory leaks in Java
- Memory that is not in use is not recycled, it is a memory leak
- In Java, memory that is up to state but no longer in use can cause memory leaks (garbage collection does not reclaim the memory that is up to state)
- The Remove method in Eg:arraylist elementdata[--size] = null;
3. Garbage collection mechanism
- Two things: Reclaim unreachable objects, clean up memory allocations, memory fragmentation generated during recycling (discontinuous memory space)
- Basic algorithms for garbage collection
- Serial Recycling and parallel recycling
- Concurrent execution and application stop
- Compress, do not compress, copy
- Copy, Mark Clear, Tag compress
- Generational collection of heap memory (heap memory is divided into three generations to hold objects)
- According to: The length of the object life time, and then according to different generations adopt different garbage collection strategy, give full play to their respective advantages.
- Based on the following two facts: ① Most objects will not be referenced for long periods of time, and young generation will be recycled; there is very little mutual reference between ② very old objects and very new objects.
- Young generation: Replication algorithms, fewer objects in the reach state, and less cost to replicate
- 1 Eden and 2 Survivor: objects are first assigned to the Eden Zone (some large objects may be assigned directly to old generations), and objects in the Survivor area experience at least one garbage collection. The one-time replication algorithm copies the Survivor objects in the Eden area and one in the zone to another empty survivor area, and then goes to the next loop, where two survivor zones are used to hold objects and the other is empty.
- Old generation
- Young's repeated recovery of the objects will go into the old generation, where the characteristics of the object: not easy to die, the passage of time, the objects will be more and more, so the old generation of space costs than young generation
- Recycling Features: Garbage collection does not need to be executed too often, because fewer objects are dead, and each execution takes more time to complete
- Tag compression algorithms that do not generate large amounts of memory fragmentation
- Permanent generation
- Mainly used for loading classes, methods and other information, garbage collection mechanism usually does not reclaim objects in this generation
- Secondary recycling: When Young's memory is about to run out, the garbage collection mechanism recycles the generation, recycles more frequently, and has a low system overhead.
- Main recycling: When the old generation of memory will be exhausted, the garbage collection mechanism will be fully recycled, that is, the young and old generations are to be recycled, at this time the cost is large, and therefore become the main recycling
- In general, young Dessian is recycled, and old generations are much less frequently recycled, and each generation is compressed independently when memory is compressed.
- Common garbage collector
- Serial collector
- Young generation: Serial copy algorithm
- Old generation: Serial tag compression algorithm, three stages: Mark, Sweep, compact (compression phase, execute sliding compaction, move the active object to the front of the old generation, the tail retains continuous space)
- Parallel collector
- Young: Similar to the serial collector, the ability to increase multi-cpu parallelism, that is, to initiate multiple threads to recycle concurrently (not with the main thread concurrently, but by multiple recycle threads executing concurrently
- Old: Exactly the same as serial
- Parallel compression collector (will replace parallel collector)
- Young: Exactly the same as in parallel
- Old: Divides the old into several fixed-size areas
- Mark: Multiple recycle threads are tagged in parallel to the object (updates the size of the area where the object is located and its location information)
- Summary: Operation of an old generation region (not a single object). The area density is checked from the far left, and when the density of an area reaches a certain value, it is determined that the area and its right area should be recycled (compressed and reclaimed space), and its left area is identified as dense (no new objects will be moved here, and the area will not be compressed). This stage: serial implementation
- Compact: Uses the data generated in the previous phase to identify areas that need to be filled, and multiple recycling threads replicate the data in parallel to the zone. After this phase, a large number of active objects are densely stored at one end of the old and chunks of free blocks on the other.
- Concurrent identity-cleanup collector (CMS) for programs with high real-time requirements for old generation recycling: Concurrent execution with a program that requires only two short pauses
- Young: Exactly the same as the parallel collector, which will still cause the application to pause,
- Old: Concurrent operation
- Mark
- A brief pause at the beginning of a garbage collection that identifies a direct reference to the accessible object (initial mark);
- Concurrent identification phase (concurrent marking phase), to find other accessible objects according to the objects found in the initial identification;
- The re-tagging phase (remark), because the concurrent identity process may re-generate the object, in order to avoid missing these, requires a short pause again, multithreading in parallel to re-identify
- Concurrent Sweep: Concurrency cleanup
- The default is to start recycling when the old generation is 68% full, because it is in parallel, and if it waits until the old is full, the application will not have available memory, while the other garbage collector is not in parallel, and when reclaimed, the program pauses and the application does not produce new objects.
- The CMS does not compress memory for the old generation, that is, its free space is discontinuous, a list of available spaces is saved, and the allocation memory efficiency decreases.
- The CMS requires a larger heap of memory because when the application is also allocating objects concurrently, the old generation grows at the same time, and the objects that become garbage in the identity phase are not immediately reclaimed, and are not reclaimed until the parallel cleanup phase. (Mine: Serial, Mark after the execution of the sweep, between the program pauses, no longer produce new objects; concurrency, mark (with concurrent mark) and concurrency Sweep, the program executes concurrently, it is still possible to produce new objects, the object to be cleaned and the newly generated objects together naturally a little larger memory. In short, because of parallelism, it is possible to create new objects at the time of cleanup, so a larger amount of memory is required, and when the line is cleaned, no new objects are generated, and the new objects are not created until the cleanup is complete, and the memory required is not so large.
- Execute additional options to force reclaim memory for permanent generation
- Serial and concurrent recovery Comparisons (mine)
- Serial: Application paused--The collector performs the recycling phase (Mark-->....-->sweep ... )--Program continues execution
- Concurrency: Application pauses 1-->cms execution initial mark (tag directly referenced by object)-Program execution, CMS concurrency tag concurrent Mark (Mark the other objects that can be found by the accessible object identified by the initial identity)--the program briefly pauses 2-->remark (the new object that the program generates when the tag is marked for concurrency)--Programs continue to execute, concurrency cleanup < This is the time to actually start the cleanup operation, which was preceded by the identity operation >
4. Memory Management Tips
- Use direct volume as much as possible
- Use strings and Byte, short, Interger ... When wrapping a class, directly using direct volume creation without the new keyword
- string concatenation using Stringbuilder/stringbuffer instead of string (string immutable, with which a large number of temporary strings are generated when stitching)
- Release references to useless objects as early as possible
- In general, the action time of a local variable is compared to a segment, and the reference value does not need to be = null, but if it is in a method where memory-intensive, time-consuming operations are performed, the value of the reference variable of the useless object should be assigned null to release the useless object as soon as possible
- Minimize the use of static variables, especially static variables to reference objects. Because the static variable belongs to the class object, the class object is in the permanent generation (its class variable is naturally in that generation), and it is very long
- Avoid creating objects in a frequently called method, loop.
- Causes constant allocation (creation), Recycling (destruction) of memory for objects that affect performance
- Cache frequently used objects (eg: database connection pool)
- Avoid constant distribution and recycling operations
- Method:
- Use HashMap: Control the Key-value in the container is not too much, otherwise the hashmap consumes too much memory will cause the performance to degrade
- Using open Source Cache projects
- Cache design:
- Sacrificing space for time is the use of containers to save used objects. The key to program design is how to control the memory occupied by the container and retain most of the objects that have been used. (Some cache algorithms)
- Try not to use the Finalize method
- This method is called before the garbage collector reclaims the resource.
- The recycling mechanism itself has taught severely restricting application performance
- The burden of its own recovery mechanism is heavier, and recovering Young's memory will cause the program to pause, affect performance, and then perform resource cleanup in the Finalize method to increase the burden on the recovery mechanism, resulting in less efficient operation
- Consider using SoftReference
- When creating an array of a very long length, consider wrapping the array element with a soft reference (enough time to refer to the memory, release when insufficient)
- Soft references are nondeterministic, and when you get a reference object, you need to show whether the object is empty (avoid an exception), and if it is empty, it should be recreated.
- Roar and Roar
No.4 Java Memory Recycling (memory recycling)