Java (JVM) Memory Model – Memory Management in Java
原文地址:http://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java Understanding?JVM Memory Model,?Java Memory Management?are very important if you want to understand the working of?Java Garbage Collection. Today we will look into memory management in java,different parts of JVM memory and how to monitor and perform garbage collection tuning. Java (JVM) Memory ModelAs you can see in the above image,JVM memory is divided into separate parts. At broad level,JVM Heap memory is physically divided into two parts –?Young Generation?and?Old Generation. ?
Memory Management in Java – Young GenerationYoung generation is the place where all the new objects are created. When young generation is filled,garbage collection is performed. This garbage collection is called?Minor GC. Young Generation is divided into three parts –?Eden Memory?and two?Survivor Memory?spaces. Important Points about Young Generation Spaces:
Memory Management in Java – Old GenerationOld Generation memory contains the objects that are long lived and survived after many rounds of Minor GC. Usually garbage collection is performed in Old Generation memory when it’s full. Old Generation Garbage Collection is called?Major GC?and usually takes longer time. ?
Stop the World EventAll the Garbage Collections are “Stop the World” events because all application threads are stopped until the operation completes. Since Young generation keeps short-lived objects,Minor GC is very fast and the application doesn’t get affected by this. However Major GC takes longer time because it checks all the live objects. Major GC should be minimized because it will make your application unresponsive for the garbage collection duration. So if you have a responsive application and there are a lot of Major Garbage Collection happening,you will notice timeout errors. Java Memory Model – Permanent GenerationPermanent Generation or “Perm Gen” contains the application metadata required by the JVM to describe the classes and methods used in the application. Note that Perm Gen is not part of Java Heap memory. Perm Gen is populated by JVM at runtime based on the classes used by the application. Perm Gen also contains Java SE library classes and methods. Perm Gen objects are garbage collected in a full garbage collection. Java Memory Model – Method AreaMethod Area is part of space in the Perm Gen and used to store class structure (runtime constants and static variables) and code for methods and constructors. Java Memory Model – Memory PoolMemory Pools are created by JVM memory managers to create a pool of immutable objects,if implementation supports it. String Pool is a good example of this kind of memory pool. Memory Pool can belong to Heap or Perm Gen,depending on the JVM memory manager implementation. Java Memory Model – Runtime Constant PoolRuntime constant pool is per-class runtime representation of constant pool in a class. It contains class runtime constants and static methods. Runtime constant pool is the part of method area. Java Memory Model – Java Stack MemoryJava Stack memory is used for execution of a thread. They contain method specific values that are short-lived and references to other objects in the heap that are getting referred from the method. You should read. Memory Management in Java – Java Heap Memory SwitchesJava provides a lot of memory switches that we can use to set the memory sizes and their ratios. Some of the commonly used memory switches are: Memory Management in Java – Java Garbage CollectionJava Garbage Collection is the process to identify and remove the unused objects from the memory and free space to be allocated to objects created in the future processing. One of the best feature of java programming language is the?automatic garbage collection,unlike other programming languages such as C where memory allocation and deallocation is a manual process. Garbage Collector?is the program running in the background that looks into all the objects in the memory and find out objects that are not referenced by any part of the program. All these unreferenced objects are deleted and space is reclaimed for allocation to other objects. One of the basic way of garbage collection involves three steps:
There are two problems with simple mark and delete approach.
The above shortcomings with the simple approach is the reason that?Java Garbage Collection is Generational?and we have?Young Generation?and?Old Generation?spaces in the heap memory. I have already explained above how objects are scanned and moved from one generational space to another based on the Minor GC and Major GC. Memory Management in Java – Java Garbage Collection TypesThere are five types of garbage collection types that we can use in our applications. We just need to use JVM switch to enable the garbage collection strategy for the application. Let’s look at each of them one by one.
Memory Management in Java – Java Garbage Collection MonitoringWe can use Java command line as well as UI tools for monitoring garbage collection activities of an application. For my example,I am using one of the demo application provided by Java SE downloads. If you want to use the same application,go to??page and download?JDK 7 and JavaFX Demos and Samples. The sample application I am using is?Java2Demo.jar?and it’s present in Command used by me to start the demo application is:
The advantage of?jstat?is that it can be executed in remote servers too where we don’t have GUI. Notice that sum of S0C,S1C and EC is 10m as specified through? Java VisualVM with Visual GCIf you want to see memory and GC operations in GUI,then you can use? Just run? After installing?Visual GC,just open the application from the left side column and head over to?Visual GCsection. You will get an image of JVM memory and garbage collection details as shown in below image. Java Garbage Collection TuningJava Garbage Collection Tuning?should be the last option you should use for increasing the throughput of your application and only when you see drop in performance because of longer GC timings causing application timeout. If you see? If you are see a lot of Full GC operations,then you should try increasing Old generation memory space. Overall garbage collection tuning takes a lot of effort and time and there is no hard and fast rule for that. You would need to try different options and compare them to find out the best one suitable for your application. That’s all for Java Memory Model,Memory Management in Java and Garbage Collection,I hope it helps you in understanding JVM memory and garbage collection process. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |