I'm confused with Heap,Young,Tenured and Perm g开发者_开发百科eneration.
Could anyone please explain?
The Java garbage collector is referred to as a Generational Garbage Collector. Objects in an application live for varying lengths of time depending on where they are created and how they are used. The key insight here is that using different garbage collection strategies for short lived and long lived objects allows the GC to be optimised specifically for each case.
Loosely speaking, as objects "survive" repeated garbage collections in the Young Generation they are migrated to the Tenured Generation. The Permanent Generation is a special case, it contains objects, that are needed by the JVM, that are not necessarily represented in your program, for example objects that represent classes and methods.
Since the Young Generation will usually contain a lot of garbage in it, it is optimised for getting rid of a lot of unused objects at once. The Tenured Generation since it contains longer lived objects is optimised for speedy garbage collection without wasting a lot of memory.
With improvements in garbage collection technology the details have become pretty complex and vary depending on your JVM and how it has been configured. You should read the documentation for the specific JVM you are using if you need to know exactly what is happening.
That said, there is a simple historical arrangement this is still useful at a conceptual level. Historically the Young Generation would be a copy collector and the Tenured Generation be a mark and sweep collector. A copy collector pays essentially no CPU cost for getting rid of garbage, most of the cost is in maintaining live objects, the price of this efficiency is heavier memory usage. A mark and sweep collector pays some CPU cost for both live and unused objects but utilizes memory more efficiently.
Java Heap Memory is part of memory allocated to JVM by Operating System. Whenever we create objects they are created inside heap in java.
Java Heap space is divided into three regions or generation for sake of garbage collection called Young Generation, Old or tenured Generation and Permanent Generation. Permanent generation is garbage collected during full gc in hotspot JVM
The Young Generation is where all new objects are allocated and aged. When the young generation fills up, this causes a minor garbage collection. A young generation full of dead objects is collected very quickly. Some surviving objects are aged and eventually move to the old generation.
The Old Generation is used to store long surviving objects. Typically, a threshold is set for young generation object and when that age is met, the object gets moved to the old generation. Eventually the old generation needs to be collected. This event is called a major garbage collection.
The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application. The permanent generation is populated by the JVM at runtime based on classes in use by the application.
PermGen has been replaced with Metaspace since Java 8 release. PermSize & MaxPermSize parameters will be ignored now. Have a look this dzone article by Pierre - Hugues Charbonneau to understand about Metaspace.
Image source:http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html
Refer to same article for more details.
All objects in the heap survive when they are being referenced. When they're not more, the garbage collector (GC) will reclaim their memory.
PermGen, Young and Tenured are diferent clasifications of objects (or spaces in the heap where they can be).
PermGen: these objects will be always there, they're not garbage collected. Classes objects are there, interned strings, etc. I don't know if there is a GC there (when system UNloads classes... but it's not a normal thing)
Young: when an object is created it's here.
Tenured: an object goes to this classification/category when it survives N GC passes (survive = GC passes but this object is referenced so it can't be reclaimed).
Depending of GC used and some parametrization, GC passes more or less often.
Then garbage collection can have different approaches to maange objects in the heap. This classification of objects helps to do it.
Here's another excellent (though long) article on how to tune/size your GC parameters, which may help you understand even more:
https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/
A very useful read if you're having GC issues and need to know how to read GC logs, or need to understand how your current GC collector works.
If you want to hook up remote monitoring of a running system to see realtime memory usage and GC runs check this tool out:
http://java.sun.com/performance/jvmstat/visualgc.html
精彩评论