什么是垃圾回收机制?
A garbage collector is responsible for
- allocating memory
- ensuring that any referenced objects remain in memory, and
- recovering memory used by objects that are no longer reachable from references in executing code.
如何进行垃圾回收?
The task of fulfilling an allocation request, which involves finding a block of unused memory of a certain size in ++the heap++, is a difficult one. The main problem for most dynamic memory allocation algorithms is to ++avoid fragmentation++ (see below), while keeping both ++allocation and deallocation++ efficient.
什么时候进行垃圾回收?
Space is commonly allocated from a large pool of memory referred to as the heap.
The timing of garbage collection is up to the garbage collector. Typically, the entire heap or a subpart of it is collected either when it fills up or when it reaches a threshold percentage of occupancy.
如何优化垃圾回收?
- Throughput(吞吐量)—the percentage of total time not spent in garbage collection, considered over long periods of time.
- Garbage collection overhead—the inverse of throughput, that is, the percentage of total time spent in garbage collection.
- Pause time—the length of time during which application execution is stopped while garbage collection is occurring.
- Frequency of collection—how often collection occurs, relative to application execution.
- Footprint—a measure of size, such as heap size.
- Promptness(及时性)—the time between when an object becomes garbage and when the memory becomes available.
参数说明
heap size
- not server-class machines
- the client JVM
- the serial garbage collector
- Initial heap size of ++4MB++
- Maximum heap size of ++64MB++
- server-class machine
- Initial heap size of ++1/64th++ of the physical memory, up to 1GB. (Note that the minimum initial heap size is 32MB, since a server-class machine is defined to have at least 2GB of memory and 1/64th of 2GB is 32MB.)
- Maximum heap size of ++1/4th++ of the physical memory, up to 1GB.
Select a Different Garbage Collector
- –XX:+UseSerialGC
- –XX:+UseParallelGC
- –XX:+UseParallelOldGC
- –XX:+UseConcMarkSweepGC
Evaluate Garbage Collection Performance
- –XX:+PrintGCDetails
- –XX:+PrintGCTimeStamps
What to Do about OutOfMemoryError
Java heap space
This indicates that an object could not be allocated in the heap. The issue may be just a configuration problem. You could get this error, for example, if the maximum heap size specified by the –Xmx command line option (or selected by default) is insufficient for the application. ++It could also be an indication that objects that are no longer needed cannot be garbage collected++ because the application is unintentionally holding references to them.
PermGen space
This indicates that the permanent generation is full. As described earlier, that is the area of the heap where the JVM stores its metadata. ++If an application loads a large number of classes, then the permanent generation may need to be increased++. You can do so by specifying the command line option –XX:MaxPermSize=n, where n specifies the size.
Requested array size exceeds VM limit
This indicates that the application attempted to allocate an array that is larger than the heap size. For example, ++if an application tries to allocate an array of 512MB but the maximum heap size is 256MB++, then this error will be thrown. In most cases the problem is likely to be either that the heap size is too small or that a bug results in the application attempting to create an array whose size is calculated to be incorrectly huge.
Tools to Evaluate Garbage Collection Performance
- jmap
- jstat