1

我正在进行性能测试。我看过很多关于设置 -Xmx 和 -Xmx 值的博客。我有点困惑,因为有些人说 -Xms=-Xmx 很好。我在一个项目中,在生产服务器上大约有 8-15K 人在线,即它总是处于高负载状态。-Xms 和 -Xmx 的优化设置应该是什么?

4

1 回答 1

1

Sadly this is a big topic in Java, and the more tuning that you do here for a specific release of your code then the more risk you will have of having to retune it when you make your next release. Several consultancies specialise in this type of stuff because of its complexity, plus we have now see the release of Zing which is a commercial JVM intended to manage this for us (by throwing spare CPUs at the problem of GC management). However, that said there are some settings that nearly always help.

Your goal is to avoid 'stop the world' events within the JVM, ones that will pause all threads while the JVM goes off to do work. Setting Xms and Xmx to the same value help here because having them different tells the JVM to start with a small heap and to then resize as it needs. Unfortunately this resizing is a stop the world event that causes large copies of objects within the heap and full GC. Expensive.

Your next goal should be to avoid objects getting tenured prematurely. The JVM is very efficient at creating lots of objects, and then releasing them soon afterwards. The problems occur when the objects are 'too big', they are held on for a while or they are more of them than fit in the eden space (a region of the heap reserved for new objects).

In general I like to increase the amount of the heap given to the young generations, and as much as possible reduce the size of the total heap. Smaller heaps are faster in Java, but you risk running out of memory. So I then spend most of my time optimising my algorithms and cleaning the code. If that does not give me the performance that I require then I instruct the JVM to write to log file its GC activity (details here Analyze GC logs for Sun Hotspots, JVM 6). My goal is to watch how much data is getting tenured.

Lastly you should read up on the different GC algorithms and select the one supported by your JVM that gives you the best performance. In general I would stay away from the G1 collector, it does not perform very well and was discussed at QCon as being a 'failed experiment' and the GC implementors are moving on to other approaches.

于 2013-03-11T12:51:08.663 回答