当前位置:文档之家› java GC 垃圾回收

java GC 垃圾回收



When
◦ When application can’t allocate the memory for new objects

How
◦ It will use the algorithm to collection the garbage in the Heap and Method area ◦ Garbage is the object or class that no longer referenced




When a new thread is launched, the Java Virtual Machine creates a new Java stack for the thread All the data on the Java stack is private to that thread, can’t be accessed by other thread

Soft references
◦ Only get collected if the JVM absolutely needs the memory. This makes them excellent for implementing object cache's. ◦ Employee emp = new Employee(); ◦ SoftReference<Employee> softRef = new SoftReference<employee>(emp);
Sun HotSpot Bea JRockit IBM J9
◦ A runtime instance.

JVM & JRE & JDK
◦ JVM: Translates Java byte codes and executes them as
native code on the client machine. ◦ JRE: includes JVM and runtime libraries



Memory for the new class instance One heap for one JVM instance, all threads share it Main area for GC Possible Error: ng.OutOfMemoryError: Java heap space




ห้องสมุดไป่ตู้

When an object is first created and a reference to it is assigned to a variable, the objectí reference count is set to s one When any other variable is assigned a reference to that object, the object's count is incremented. When a reference to an object goes out of scope or is assigned a new value, the object's count is decremented Any object with a reference count of zero can be garbage collected. Advantage ◦ Effective Disadvantage ◦ Does not detect cycles
◦ operand stack ◦ frame data
data to support constant pool resolution normal method return, exception dispatch

Each thread of a running program has its own pc register

Combine two strategies commonly used by mark and sweep collectors are compacting and copying.

In the Young generation Most objects created by most programs have very short lives. It is good to use copying collectors. In the Tenured generation/Permanent Generation Most programs create some objects/class that have very long lifetimes. It is good to use Mark-Compact Collectors or mark and sweep Collectors

Phantom references
◦ Can be collected whenever the collector likes
Young generation Tenured generation (or old generation)
Most objects are allocated here and most objects die here. Includes eden and two smaller survivor spaces Objects that have survived some number of minor collections are moved to this generation from young generation. Some large objects may be allocated directly in old generation. It holds data needed by the virtual machine to describe objects that do not have an equivalence at the Java language level. For example objects describing classes and methods. It’s separate heap space that is not garbage collected (ergo the permanent).


stack frame:
◦ local variables
primitive types and a reference type Size would be 1 word or 2 words(long, double) Word will be 4 byte in 32-bit machine, 8 byte in 64-bit
Generational Collectors Incremental Collectors
Divide by concurrency
Serial Collector Parallel Collector Concurrent Mark-Sweep (CMS) Collector




Divide the memory to 2 area, use one of them every time Once the memory is full, the collector will move all live objects to a new area Advantage ◦ Does not have Memory Fragmentation Disadvantage ◦ The space only use 50%
◦ JDK: Includes JRE and development tools: like javac etc.

Memory for the class type information, like Constant pool, Field information ,Method information , class (static) variables etc All threads share the same method area

Strong references
◦ Most ubiquitous form of reference ◦ Any object with an active strong reference to it, will never be garbage collected ◦ Like Employee emp = new Employee();




Trace out the graph of object references starting with the root nodes Objects that are encountered during the trace are marked in some way Unmarked objects are known to be unreachable and can be garbage collected. Disadvantage Will generate Memory Fragmentation

As a thread executes a Java method, the pc register contains the address of the current instruction being executed by the thread

What
◦ GC attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program
相关主题