One, Java reference type 1, the state of the object in memory
Up to state: When an object is created, there is more than one reference variable pointing to it.
Recoverable status:
Unreachable state: When all associations of an object are cut off, and the Finalize method that calls all objects in the system still does not make the object reachable, the object will permanently lose all references and eventually become unreachable.
2. Type of reference
Strong references:
Soft references:
Weak references:
Virtual References:
Ii. memory Leaks in Java
Three, garbage collection mechanism 1, garbage collection algorithm
Serial (Serial) recycling and parallel (Parallel) recycling: Serial is always only one CPU performing garbage collection operations; Parallel is splitting the entire collection into multiple parts, each of which is owned by one CPU, allowing multiple CPUs to be recycled in parallel. The efficiency of parallel recovery is high, but the complexity increases and memory fragmentation increases.
Concurrent (Concurrent) execution and application Stop (Stop-the-world):
Compression (compacting), uncompressed (non-compacting), and Replication (Copying): To reduce memory fragmentation, the garbage collector, which supports compression, moves all active objects together, and then reclaims all the previously occupied memory. Uncompressed garbage collection only reclaims memory, so that the recovered memory is not contiguous, causing memory fragmentation. Uncompressed relative to compression, recovery is faster, but memory allocation is slow. A replicated garbage collection copies all the available objects to the same memory, with the advantage that there is no memory fragmentation during garbage collection, with the disadvantage of replicating data and additional memory.
Mark Clear (Mark-sweep): Do not compress
Tag compression (mark-sweep-compact): Compressed
2. Generation of heap memory collection
Yong
Old
Permanent
3. Additional options with garbage collection
-xmx:java maximum capacity of virtual machine heap memory
-XMS: initial capacity
-xx:minheapfreeratio=40
-xx:maxheapfreeratio=70
-xx:newratio=2
-xx:newsize=64m
-xx:survivalratio=8
-xx:maxnewsize=128m
-xx:permsize=128m
-xx:maxpermsize=128m
4. Common garbage collector
Serial collector: Yong, serial copy, old, serial tag compression
Parallel collector:
Parallel compression collector (Parallel compacting Collector):
Concurrency Token-Cleanup (mark-sweep) collector:
Four, memory management tips 1, the use of direct volume as far as possible;
String str= "Hello World";
String Str=new string ("Hello World");
2, using StringBuilder and StringBuffer string connection, 3, the early release of useless object reference;
Obj=null;
4, the use of static variables as little as possible;
Synchronize with the life cycle of the class.
5. Avoid creating objects in frequently called, cyclic methods, 6. Caches frequently used objects;
Sacrificing system space in exchange for run time
7, try not to use the Finalize method;
After the object loses its reference, the garbage collector first calls the object's Finalize method for resource cleanup before the object is reclaimed by the garbage collector.
8, consider the use of softreference;
Memory Recycling in Java