简体   繁体   English

对于多线程应用程序,有用的JVM选项有哪些?

[英]What are the useful JVM options for a multithreaded application?



I am working on an application that creates a lot of threads and relies heavily on String manipulation. 我正在开发一个创建大量线程并严重依赖String操作的应用程序。
The application works for a good 24 hrs at a time and needs to be always very responsive. 该应用程序一次可以运行24小时,并且需要始终保持快速响应。
I am trying to keep the creation of objects to a minimum. 我试图将对象的创建减到最少。 The application is doing well without any configuration at the moment. 该应用程序目前运行良好,无需任何配置。

But I was wondering for my own knowledge if there were any advantages (or disavantages) in using a specific JVM configuration? 但是我想知道,使用特定的JVM配置是否有任何优势(或劣势)?

Please bear with me, I am pretty new on on the subject of the JVM/GC configuration: 请多多包涵,关于JVM / GC配置的话题我还很陌生:

  • I was wondering if there were any JVM options I should absolutely use while working with multithreads? 我想知道在使用多线程时是否应该绝对使用任何JVM选项?
  • Should I configure the heap? 我应该配置堆吗?
  • Should I also configure the GC? 我还应该配置GC吗?
  • Should I keep the Garbage Collection to a minimum? 我应该将垃圾收集降至最低吗?

    I started reading: http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html 我开始阅读: http : //www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html
    Any tips on the subject would greatly be appreciated. 任何关于这个问题的技巧将不胜感激。

    Thanks in advance, 提前致谢,

  • Generally, the best intial advice concerning tweaking your JVM is don't . 通常,关于调整JVM的最佳建议不是 Unless you are experiencing specific JVM-related problems with the default settings, leave them alone. 除非使用默认设置遇到与JVM有关的特定问题,否则请不要理会它们。

    If you do need to fiddle around with the settings, I would recommend you set up a representative testcase and use an advanced profiler such as JProfiler . 如果确实需要弄乱设置,我建议您设置一个代表性的测试用例,并使用诸如JProfiler之类的高级分析器。

    Furthermore, you should really read the technical documentation regarding the HotSpot VM, specifically the Memory Management Whitepaper, all of which you may find here . 此外,您应该真正阅读有关HotSpot VM的技术文档,尤其是《内存管理白皮书》,您可以在此处找到所有这些文档。

    If it is working fine then you should not do anything. 如果一切正常,则您什么也不要做。

    If your application is CPU bound you should not create Lot of threads. 如果您的应用程序受CPU限制,则不应创建大量线程。 Reason is lot of time is wasted in context switching. 原因是上下文切换浪费了很多时间。 String manipulation if it in memory then there should be only those threads which are required 如果在内存中进行字符串操作,则应该仅存在那些需要的线程

    NCPU = UCPU* (1+W/C)
    
    Where  NCPU--> Number of CPU
    UCPU--> Target CPU Utilization
    W-->Wait time
    C--> Compute time
    

    So for CPU bound operations it should be max (Number of CPU +1) threads. 因此,对于受CPU限制的操作,它应该是最大(CPU +1个)线程。

    Also there are lot of test cases defined for concurrency applications in Java Concurrency in Practice. 此外,在Java Concurrency in Practice中为并发应用程序定义了很多测试用例。 You may want to check those. 您可能要检查那些。

    I was wondering if there were any JVM options I should absolutely use while working with multithreads? 我想知道在使用多线程时是否应该绝对使用任何JVM选项?

    No. 没有。

    Should I configure the heap? 我应该配置堆吗?

    No, apart from setting the heap size to something reasonable (with -Xmx and -Xms) 不,除了将堆大小设置为合理的值(使用-Xmx和-Xms)

    Should I also configure the GC? 我还应该配置GC吗?

    No, unless you have a particular need for "low-pause". 不可以,除非您特别需要“低暂停时间”。 The default throughput compiler is the best option if you are currently meeting your "responsiveness" goals. 如果当前正在实现“响应”目标,则默认吞吐量编译器是最佳选择。 If you are not meeting those goals then you should consider CMS or G1 ... but beware that they reduce pauses but they also reduce throughput. 如果您未达到这些目标,则应考虑使用CMS或G1 ...,但要注意它们会减少暂停时间,但同时也会降低吞吐量。

    Should I keep the Garbage Collection to a minimum? 我应该将垃圾收集降至最低吗?

    No. That is not a sensible goal. 不。那不是明智的目标。 Your aim is to maximize throughput, and minimizing GC won't necessarily achieve that. 您的目标是最大化吞吐量,而最小化GC不一定能达到目标。 In a lot of case, it is more efficient to generate garbage than to to have the application do extra work to avoid generating garbage. 在许多情况下,生成垃圾比使应用程序执行额外的工作以避免生成垃圾更为有效。 (And as Peter Lawrey pointed out, you've also got the extra developer effort in writing and maintaining mode complex code.) (并且正如Peter Lawrey指出的那样,您还付出了额外的开发人员精力来编写和维护模式复杂的代码。)


    I would advise you to use a profiler to see if your application is spending a lot of time (CPU time or elapsed time) relative to doing other productive work. 我建议您使用探查器来查看您的应用程序相对于执行其他生产性工作是否花费大量时间(CPU时间或经过的时间)。 If not, or if the application is already running fast enough then don't fiddle with the JVM options. 如果不是,或者应用程序已经足够快地运行,请不要摆弄JVM选项。

    If you are worried that your application won't cope with increased load in the future, then tweaking the GC doesn't scale. 如果您担心您的应用程序将来无法应对增加的负载,那么对GC进行调整将无法扩展。 A better option is to investigate scaling up your hardware and/or figuring out how to do the work on multiple machines. 更好的选择是调查扩大硬件规模和/或弄清楚如何在多台计算机上进行工作。 In addition, tuning the GC to improve performance with current load may actually result in worse performance when the load increases. 此外,当负载增加时,调整GC以提高当前负载下的性能实际上可能会导致性能变差。 (Consider the problem that arises with CMS when it can't keep up and is forced to do a full stop-the-world collection to recover.) (请考虑CMS无法跟上并被迫进行完整的世界收集以恢复时出现的问题。)


    Finally, it is generally speak a bad idea to have lots of threads. 最后,拥有很多线程通常是一个坏主意。 It is better to use a small number of worker threads (roughly equal to the number of processors / cores) and feed them work via concurrent queues, etcetera. 最好使用少量的工作线程(大约等于处理器/内核的数量),并通过并发队列等方式使它们工作。

    I was wondering if there were any JVM options I should absolutely use while working with multithreads? 我想知道在使用多线程时是否应该绝对使用任何JVM选项?

    All the best options will be on by default. 默认情况下,所有最佳选项均处于启用状态。 If you look at HotSpot VM Options you can see quite a few are -XX:+ which means they are on by default. 如果查看HotSpot VM Options,您会看到很多-XX:+ ,这意味着它们默认处于启用状态。

    Should I configure the heap? 我应该配置堆吗?

    Possibly. 可能吧。 But I would leave the default setting if you can. 但是,如果可以的话,我会保留默认设置。

    Should I also configure the GC? 我还应该配置GC吗?

    Possibly. 可能吧。 But I would leave the default setting if you can. 但是,如果可以的话,我会保留默认设置。

    Should I keep the Garbage Collection to a minimum? 我应该将垃圾收集降至最低吗?

    Reducing the amount of garbage created takes effort. 减少创建的垃圾量很费力。 It provides some benefit up to a point. 它提供了一些好处。 You have to decide what is the best use of your time and how much time to spend reducing the amount of garbage created. 您必须决定什么是您的时间的最佳利用方式,以及花费多少时间来减少所创建的垃圾量。

    I would always start with a memory profiler and find where you are creating the most garbage. 我将始终从内存探查器开始,然后查找要在其中创建最多垃圾的位置。 Start from the top of the list rather than trying to tune everything as this ensures you will get the most benefit for the least amount of effort. 从列表的顶部开始,而不是尝试调整所有内容,因为这可以确保您以最少的精力获得最大的收益。


    BTW: I am an advocate of low garbage and off heap programs where it makes sense to do so. 顺便说一句:我是低垃圾和非堆程序的倡导者。 I have written trading systems which can run for a day without even a minor GC and programs which can load/use 500+ GB of data in off heap memory. 我编写的交易系统即使没有次要的GC也可以运行一天,而程序可以在堆外内存中加载/使用500+ GB的数据。 However, you have to be able to demonstrate or quantify how much difference it will make to the end users or your business to determine whether it is really worth it. 但是,您必须能够证明或量化其对最终用户或您的业务有多大的影响,以确定它是否真正值得。

    In the past, I have faced the similar server application: lots of String manipulation, String creation, and needs to be always very responsive. 过去,我曾遇到过类似的服务器应用程序:大量的String操作,String创建,并且需要始终非常敏感。 The app worked fine with default configuration, until run into high-stress situation. 该应用程序在默认配置下运行良好,直到遇到高压力的情况为止。 You need to enable -XX:+UseConcMarkSweepGC for low pause, and fine tune other parameters to ensure the app behavior the way that you want. 您需要启用-XX:+ UseConcMarkSweepGC以获得较低的暂停,并微调其他参数以确保应用程序按所需方式运行。 Here is the short list: 以下是简短列表:

    -XX:+CMSParallelRemarkEnabled -XX:+ CMSParallelRemarkEnabled
    -XX:+CMSScavengeBeforeRemark -XX:+ CMSScavengeBeforeRemark
    -XX:+UseCMSInitiatingOccupancyOnly -XX:+仅使用CMSInitiatingOccupancy
    -XX:CMSInitiatingOccupancyFraction=nn -XX:CMSInitiatingOccupancyFraction = nn
    -XX:CMSWaitDuration=300000 -XX:CMSWaitDuration = 300000
    -XX:GCTimeRatio=nn -XX:GCTimeRatio = nn
    -XX:+DisableExplicitGC -XX:+ DisableExplicitGC

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM