Hundreds of millions of daily requests amount of electricity supplier system, how the young generation garbage collection JVM parameter optimization? ---- combat teach you how to configure

table of Contents:

  1. Case Background introduction

  2. Special large electricity providers to promote scenes

  3. Withstood big promotion instantaneous pressure required several machines?

  4. Promote the use of large memory model to estimate the peak of the order system

  5. The memory in the end how to allocate?

  6. One of the new generation garbage collection optimization: Survivor is not enough space

  7. How many times after the new generation garbage collection target escaped into the old year?

  8. How much objects directly into the old year?

  9. Do not forget to specify the garbage collector

  10. Today Questions

 

1, Case Background introduction

As always, we will drive the next case to analyze in the end how to bring everyone in a particular scenario, memory usage model predictive system.

 

Then the rational optimization of the new generation, years old, Eden, and memory size of each area of ​​Survivor.

 

Then again try to optimize the parameters of the object to avoid the new generation into the old era, as far as possible to stay in the new generation of the object to be recovered in the fall.

 

The background here is the electricity supplier system, the electricity supplier system in fact usually split into many independent subsystems deployment

 

Such as commodity systems, order systems, marketing systems, inventory systems, storage systems, membership systems, etc.

 

We are here to more central order system as an example to illustrate.

 

(Tip: Before you eat this case, make sure you fully understand the column two weeks before the article!)

 

Our case is an electrical background daily business system billions requested amount, then you can calculate your energy provider system on the amount of one hundred million requests per day, he would daily number of active users?

 

Generally each user to calculate average visit 20 times, then the amount of hundreds of millions of requests, requires roughly 5 million users daily living.

 

So it continues to project, of which 5 million are users Nikkatsu will come in a lot of browsing, then many people will place an order?

 

Here can be calculated in accordance with the pay conversion rate of 10%, about 50 million people will be under orders every day, so that there will be approximately 500,000 orders per day.

 

It counted 500,000 orders for him to concentrate on four hours of daily peak period, in fact, on average it is probably also dozens of orders per second, we feel that there is not nothing to say?

 

Because under the pressure of dozens of orders, there is no need to pay more attention to JVM is basically occupied by some of the new generation of memory every second, every new generation will be full for a long time. Then after the first Minor GC clean out the garbage objects, memory is vacant, almost no pressure.

 

2, special large electricity providers to promote scenes

But if you wanted to take into account the special large electricity providers to promote the scene, I will not think so

 

Because many small and medium business platform, the system pressure usually does in fact not so big, nor too high concurrency, even thousands of concurrent pressure is the peak pressure of the second.

 

However, if you encounter some big promotion scenarios, such as the two 11 something, the situation is different.

 

Assuming a similar festival's 11 double, zero, many people waiting for big promotion will begin to chop hand shopping, this time, could start in as little as 10 minutes of big promotion, the moment will be 500,000 orders.

 

So this time there will be close to 1,000 orders per second, then we have for this big promotion at the scene to analyze memory usage model ordering system.

 

3, withstood great pressure to promote instantaneous need several machines?

So be withstood pressure during transient single big promotion, ordering system need to deploy several machines?

 

Basically to count by three, that is, each machine needs to 300 per single anti requests. This is also very reasonable, and the need to assume that the order system deployment is the most common standard 4-core 8G machine.

 

From the CPU and memory resources angle of the machine itself, withstood 300 per single request is no problem.

 

But the problem is that the need for limited memory resources JVM reasonable allocation and optimization, including garbage collection be optimized to improve, so that the fewest possible number of GC JVM, and try to avoid the Full GC, GC thus reducing the JVM as possible the impact of the new system is more difficult peak.

 

4, using the large memory model to promote estimate the peak of the ordering system

Background have all been finished, then we would have to estimate the memory usage of the system model orders.

 

May be processed at substantially 300 per second in accordance with a single request to estimate, in fact, both the order processing performance or concurrency, related to the production of very close

 

Because the request processing unit is relatively time consuming, involving many call interface, substantially 100 to 300 per second single request is the same.

 

Then we will go by the size of each order to estimate 1kb, just 300 orders will have 300kb of memory overhead

 

Then the order entry object count objects associated orders, inventory, promotions, coupons and so a series of other business objects, generally need to zoom in on a single target cost 10 to 20 times.

 

Furthermore, in addition to the orders, the order system will have a lot of orders related to other operations, such as order inquiries and the like, so the joint Looking back, you can go big estimate, and then expand the amount of 10 times.

 

So every second would probably 300kb * 20 * 10 = 60mb of memory overhead.

 

But after a second, it can be considered 60mb object is garbage, because 300 order processing is over, all related objects have lost their reference can be recovered state.

 

We look:

 

 

 

5, how to allocate memory in the end?

Suppose we have a 4-core 8G machine, then to the JVM memory generally to 4G, the rest will leave vacant a few G to use the operating system and the like

 

Do not think of the machine run out of memory all at once, which we can heap memory to 3G, we can give to the new generation of 1.5G, the old year is 1.5G.

 

Then each thread's stack has a Java Virtual Machine 1M, so the JVM if there are hundreds of threads is likely to be several hundred M

 

Then give permanent behalf 256M memory, 4G memory, which basically almost the same.

 

But also remember to set some necessary parameters, such as open "-XX: HandlePromotionFailure" option (not familiar with this argument, we can look back and review the article before the column)

 

JVM parameters are as follows:

“-Xms3072M -Xmx3072M -Xmn1536M -Xss1M -XX:PermSize=256M -XX:MaxPermSize=256M  -XX:HandlePromotionFailure”

 

But "-XX: HandlePromotionFailure" parameter in JDK 1.6 after it was abandoned, so now generally do not set this parameter in a production environment.

 

After JDK 1.6, as long as the determination "Space Available year old"> "New Generation sum object", or "Space Available year old"> "Minor GC ascend to the average size of the previous year old object"

 

These two conditions are met one Minor GC can be carried out directly, without advance trigger a Full GC.

 

So in fact, if you are using a JDK 1.7 or JDK 1.8, the JVM parameters remains as to the back are also no longer join the parameters:

 

“-Xms3072M -Xmx3072M -Xmn1536M -Xss1M -XX:PermSize=256M -XX:MaxPermSize=256M”

 

At this time, the JVM memory shown below.

 

 

Then it is clear, the system program ordering system kept running during the big promotion, handling 300 orders per second, the new generation will occupy 60MB of memory space

 

But after one second it will become garbage objects 60MB, then the new generation of 1.5G of memory takes about 25 seconds will be filled, as shown below.

 

After 25 seconds it will be carried Minor GC, this time because there are "-XX: HandlePromotionFailure" option, so you can think needs to be checked after the main is to compare the "old's available space" and "into the previous Minor GC old the average size "of the object's beginning can be sure that this check is passed.

 

So Minor GC run directly, you can recover all of a sudden out 99% of the new generation of objects, because in addition to the last second request is still processing orders, most orders would have finished processing, so at this time may survive object is about 100MB.

 

But here the question is, if "-XX: SurvivorRatio" default parameter value is 8, so this time the new generation in the Eden area occupies about 1.2GB of memory, each Survivor region is 150MB of memory, as shown below.

 

So Eden area is full 1.2GB Minor GC will be carried out, so only takes about 20 seconds, will be filled with the Eden area, will be a Minor GC.

 

GC then the live objects around 100MB, will be placed in the region S1. As shown below.

 

 

Then run again for 20 seconds, the area filled Eden, Eden and S1 again garbage collection objects, live objects may enter or around 100MB-S2 region, as FIG.

 

 

At this time JVM parameters are as follows:

 

“-Xms3072M -Xmx3072M -Xmn1536M -Xss1M -XX:PermSize=256M -XX:MaxPermSize=256M  -XX:SurvivorRatio=8”

 

6, one of the new generation garbage collection optimization: Survivor is not enough space

First, during the JVM optimized when the first issue to consider is that you pass estimate, your new generation of Survivor in the end zone enough?

 

按照上述逻辑,首先每次新生代垃圾回收在100MB左右,有可能会突破150MB,那么岂不是经常会出现Minor GC过后的对象无法放入Survivor中?然后岂不是频繁会让对象进入老年代?

 

还有,即使Minor GC后的对象少于150MB,但是即使是100MB的对象进入Survivor区,因为这是一批同龄对象,直接超过了Survivor区空间的50%,此时也可能会导致对象进入老年代。

(关于jvm的垃圾回收规则,如果不太清楚,请参加专栏之前的文章)

 

所以其实按照我们这个模型来说,Survivor区域是明显不足的。

 

这里其实建议的是调整新生代和老年代的大小,因为这种普通业务系统,明显大部分对象都是短生存周期的,根本不应该频繁进入老年代,也没必要给老年代维持过大的内存空间,首先得先让对象尽量留在新生代里。

 

所以此时可以考虑把新生代调整为2G,老年代为1G,那么此时Eden为1.6G,每个Survivor为200MB,如下图。

 

 

这个时候,Survivor区域变大,就大大降低了新生代GC过后存活对象在Survivor里放不下的问题,或者是同龄对象超过Survivor 50%的问题。

 

这样就大大降低了新生代对象进入老年代的概率。

 

此时JVM的参数如下:

 

“-Xms3072M -Xmx3072M -Xmn2048M -Xss1M -XX:PermSize=256M -XX:MaxPermSize=256M  -XX:SurvivorRatio=8”

 

其实对任何系统,首先类似上文的内存使用模型预估以及合理的分配内存,尽量让每次Minor GC后的对象都留在Survivor里,不要进入老年代,这是你首先要进行优化的一个地方。

 

7、新生代对象躲过多少次垃圾回收后进入老年代?

大家都知道,除了Minor GC后对象无法放入Survivor会导致一批对象进入老年代之外,还有就是有些对象连续躲过15次垃圾回收后会自动升入老年代。

 

其实按照上述内存运行模型,基本上20多秒触发一次Minor GC,那么如果按照“-XX:MaxTenuringThreshold”参数的默认值15次来说,你要是连续躲过15次GC,就是一个对象在新生代停留超过了几分钟了,此时他进入老年代也是应该的。

 

有些博客会说,应该提高这个参数,比如增加到20次,或者30次,其实那种说法根本是不对的

 

因为你对这个参数考虑必须结合系统的运行模型来说,如果躲过15次GC都几分钟了,一个对象几分钟都不能被回收,说明肯定是系统里类似用@Service、@Controller之类的注解标注的那种需要长期存活的核心业务逻辑组件。

 

那么他就应该进入老年代,何况这种对象一般很少,一个系统累计起来最多也就几十MB而已。

 

所以你说你提高“-XX:MaxTenuringThreshold”参数的值,有啥用呢?让这些对象在新生代里多停留几分钟?

 

因此考虑问题,一定不要人云亦云,要结合运行原理,自己推演和思考,不同的业务系统还都是不一样的。

 

其实这个参数甚至你都可以降低他的值,比如降低到5次,也就是说一个对象如果躲过5次Minor GC,在新生代里停留超过1分钟了,尽快就让他进入老年代,别在新生代里占着内存了。

 

总之,对于这个参数务必是结合你的系统具体运行的模型来考虑。

 

要记住,JVM没有万能的最佳参数,但是有一套通用的分析和优化的方法。

 

此时JVM参数如下:

 

“-Xms3072M -Xmx3072M -Xmn2048M -Xss1M  -XX:PermSize=256M -XX:MaxPermSize=256M  -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=5”

 

8、多大的对象直接进入老年代?

另外有一个逻辑是说,大对象可以直接进入老年代 ,因为大对象说明是要长期存活和使用的

 

比如在JVM里可能会缓存一些数据,这个一般可以结合自己系统中到底有没有创建大对象来决定。

 

但是一般来说,给他设置个1MB足以,因为一般很少有超过1MB的大对象。如果有,可能是你提前分配了一个大数组、大List之类的东西用来放缓存的数据。

 

此时JVM参数如下:

 

“-Xms3072M -Xmx3072M -Xmn2048M -Xss1M  -XX:PermSize=256M -XX:MaxPermSize=256M  -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=5 -XX:PretenureSizeThreshold=1M”

 

9、别忘了指定垃圾回收器

同时大家别忘了要指定垃圾回收器,新生代使用ParNew,老年代使用CMS,如下JVM参数 :

 

“-Xms3072M -Xmx3072M -Xmn2048M -Xss1M  -XX:PermSize=256M -XX:MaxPermSize=256M  -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=5 -XX:PretenureSizeThreshold=1M -XX:+UseParNewGC -XX:+UseConcMarkSweepGC”

 

ParNew垃圾回收器的核心参数,其实就是配套的新生代内存大小、Eden和Survivor的比例

 

只要你设置合理,避免Minor GC后对象放不下Survivor进入老年代,或者是动态年龄判定之后进入老年代,给新生代里的Survivor充足的空间,那么Minor GC一般就没什么问题。

 

然后根据你的系统运行模型,合理设置“-XX:MaxTenuringThreshold”,让那些长期存活的对象,抓紧尽快进入老年代,别在新生代里一直待着。

 

这样基本上一个初步的优化好的JVM参数就结合你的业务出来了。明天我们继续结合案例来分析 老年代的垃圾回收和参数优化方式。

Guess you like

Origin www.cnblogs.com/technologykai/p/11264813.html