当前位置:网站首页>From memory leak, memory overflow and out of heap memory, the JVM optimizes the parameter configuration parameters

From memory leak, memory overflow and out of heap memory, the JVM optimizes the parameter configuration parameters

2020-12-07 09:16:09 Big data learning and sharing

Memory leak

A memory leak is when a program requests memory , Unable to free requested memory space , Useless object ( Objects that are no longer in use ) The memory that continuously occupies memory or useless objects is not released in time , This will result in a waste of memory space . Memory leaks eventually lead to OOM.

Typical scenario of causing memory leak :

1. Improper use of the singleton pattern. Singleton objects will be initialized in JVM It exists as static variables throughout its life cycle . If the singleton holds a reference to an external object , Then this object will not be able to be JVM Normal recycling

2. database 、 The Internet 、IO The connection is not closed. Release the connection , Generally, it will provide close Method to explicitly close . But if not close, It doesn't automatically get gc The recycling .3. Inappropriate object references

such as , A long-lived object holding a reference to a short-lived object can easily cause a memory leak , Although objects with short life cycles are no longer needed , But because a long-lived object holds a reference to it, it cannot be recycled .

out of memory

Memory overflow, that is out of memory abbreviation OOM. When a program requests memory , Not enough memory for it , There are often OOM. Like applying for a Integer, But save it Long The number that can only be saved by type , That would cause a memory overflow .

Out of heap memory

On the heap memory and the corresponding memory recovery strategy , Previous articles 《JVM Memory management and garbage collection 》《JVM Garbage collector 、 Memory allocation and recycling strategy 》 Has been introduced in . Out of heap memory means that the objects allocated are directly managed by the operating system JVM Space outside the memory heap . At the same time, because this part of the area is directly managed by the operating system , Other processes can be accessed directly through the operating system , Reduced from JVM The process of copying data in .

Out of heap memory is suitable for objects with long life cycle , It has the following characteristics :

  1. Can be very convenient to open up a large amount of memory space , Good scalability for large memory
  2. Reduce the system pause time caused by garbage collection
  3. Objects can be shared between processes , Reduce JVM The replication process between
  4. Suitable for those with less allocation times , Frequent read / write scenarios

But it also has the following disadvantages :

  1. Prone to memory leaks , And it's hard to find out
  2. The data structure of out of heap memory is not intuitive , When storing objects with complex structure , Will waste a lot of time serializing it

Commonly used JVM Configuration parameters

-Xms:JVM Initial minimum heap memory

-Xmx:JVM Maximum heap memory allowed

-XX:PermSize:JVM Initial non heap memory

-XX:MaxPermSize:JVM Maximum non heap memory allowed

-XX:+UseConcMarkSweepGC: The older generation activates CMS The collector , You can minimize fullGC

-XX:+UseParNewGC: Set up young generation for multithreading parallel collection

-XX:+UseCMSCompactAtFullCollection: stay full gc when , Compression of the old age (CMS When , Can cause memory fragmentation , Make memory space discontinuous , May affect performance , But it can eliminate debris )

-XX:CMSInitiatingOccupancyFraction: Trigger when the occupied space of the old age reaches a certain proportion CMS garbage collection

-Xverify:none: Class loading with bytecode verification turned off

-XX:+DisableExplicitGC: tell JVM Turn off the System.gc

-XX:+CMSParallelRemarkEnabled: Re labeling algorithm , Reduce the mark pause time

-XX:+PrintHeapAtGC: Print GC Detailed stack information before and after , It is mainly used for the information of production environment or stress test

-XX:+PrintGCTimeStamps: Output GC The timestamp

-XX:PretenureSizeThreshold: So that large objects directly into the elderly generation

Related articles :

JVM Memory management and garbage collection

JVM Garbage collector 、 Memory allocation and recycling strategy

 


 

Focus on WeChat official account : Big data learning and sharing , Get more on technology dry goods

版权声明
本文为[Big data learning and sharing]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201207091556417r.html