Kotlin - Coroutines cause heavy CPU usage with DefaultDispatcher after migration to Kotlin 1.3










2















I have been using the experimental versions of Kotlin coroutines under high concurrency for a long time, and the performance has always been excellent. The main logic could be simplified to the code below:



// works fine in kotlin 1.2 with 3000+ QPS for a 40-core host
launch
// running in ForkJoinPool.commonPool() by default
// non-blocking IO function
val result = supendFunction()
doSomething(result)



However, after I updated kotlin to 1.3, and migrated to the formal version of coroutines, like this



// kotlin 1.3 version
GlobalScope.launch
// running in DefaultDispatcher
// non-blocking IO function
val result = supendFunction()
doSomething(result)



The CPU usage rises from 2% to 50% without any exception or error thrown. The only difference that I notice is that the coroutines are no longer executed in ForkJoinPool.commonPool() like before. Instead, they are running in DefaultDispatcher threads, like DefaultDispatcher-worker-30.



My questions are:



  1. Why does it cost so much CPU usage with DefaultDispatcher?

  2. Why does kotlin 1.3 use DefaultDispatcher in place of ForkJoinPool.commonPool() by default?

  3. How to keep the behavior of coroutines just like before 1.3?









share|improve this question




























    2















    I have been using the experimental versions of Kotlin coroutines under high concurrency for a long time, and the performance has always been excellent. The main logic could be simplified to the code below:



    // works fine in kotlin 1.2 with 3000+ QPS for a 40-core host
    launch
    // running in ForkJoinPool.commonPool() by default
    // non-blocking IO function
    val result = supendFunction()
    doSomething(result)



    However, after I updated kotlin to 1.3, and migrated to the formal version of coroutines, like this



    // kotlin 1.3 version
    GlobalScope.launch
    // running in DefaultDispatcher
    // non-blocking IO function
    val result = supendFunction()
    doSomething(result)



    The CPU usage rises from 2% to 50% without any exception or error thrown. The only difference that I notice is that the coroutines are no longer executed in ForkJoinPool.commonPool() like before. Instead, they are running in DefaultDispatcher threads, like DefaultDispatcher-worker-30.



    My questions are:



    1. Why does it cost so much CPU usage with DefaultDispatcher?

    2. Why does kotlin 1.3 use DefaultDispatcher in place of ForkJoinPool.commonPool() by default?

    3. How to keep the behavior of coroutines just like before 1.3?









    share|improve this question


























      2












      2








      2








      I have been using the experimental versions of Kotlin coroutines under high concurrency for a long time, and the performance has always been excellent. The main logic could be simplified to the code below:



      // works fine in kotlin 1.2 with 3000+ QPS for a 40-core host
      launch
      // running in ForkJoinPool.commonPool() by default
      // non-blocking IO function
      val result = supendFunction()
      doSomething(result)



      However, after I updated kotlin to 1.3, and migrated to the formal version of coroutines, like this



      // kotlin 1.3 version
      GlobalScope.launch
      // running in DefaultDispatcher
      // non-blocking IO function
      val result = supendFunction()
      doSomething(result)



      The CPU usage rises from 2% to 50% without any exception or error thrown. The only difference that I notice is that the coroutines are no longer executed in ForkJoinPool.commonPool() like before. Instead, they are running in DefaultDispatcher threads, like DefaultDispatcher-worker-30.



      My questions are:



      1. Why does it cost so much CPU usage with DefaultDispatcher?

      2. Why does kotlin 1.3 use DefaultDispatcher in place of ForkJoinPool.commonPool() by default?

      3. How to keep the behavior of coroutines just like before 1.3?









      share|improve this question
















      I have been using the experimental versions of Kotlin coroutines under high concurrency for a long time, and the performance has always been excellent. The main logic could be simplified to the code below:



      // works fine in kotlin 1.2 with 3000+ QPS for a 40-core host
      launch
      // running in ForkJoinPool.commonPool() by default
      // non-blocking IO function
      val result = supendFunction()
      doSomething(result)



      However, after I updated kotlin to 1.3, and migrated to the formal version of coroutines, like this



      // kotlin 1.3 version
      GlobalScope.launch
      // running in DefaultDispatcher
      // non-blocking IO function
      val result = supendFunction()
      doSomething(result)



      The CPU usage rises from 2% to 50% without any exception or error thrown. The only difference that I notice is that the coroutines are no longer executed in ForkJoinPool.commonPool() like before. Instead, they are running in DefaultDispatcher threads, like DefaultDispatcher-worker-30.



      My questions are:



      1. Why does it cost so much CPU usage with DefaultDispatcher?

      2. Why does kotlin 1.3 use DefaultDispatcher in place of ForkJoinPool.commonPool() by default?

      3. How to keep the behavior of coroutines just like before 1.3?






      kotlin java.util.concurrent coroutine kotlinx.coroutines






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 11:07







      ProtossShuttle

















      asked Nov 13 '18 at 10:52









      ProtossShuttleProtossShuttle

      886725




      886725






















          1 Answer
          1






          active

          oldest

          votes


















          1















          1. Why does it cost so much CPU usage with DefaultDispatcher?



          It's a completely different implementation that optimizes for several performance targets, for example communication via a channel. It is subject to future improvements.




          1. Why does kotlin 1.3 use DefaultDispatcher in place of ForkJoinPool.commonPool() by default?



          Actually it has been using the Default dispatcher all the time, but the resolution of Default changed. In the experimental phase, it was equal to the CommonPool but now it prefers the custom implementation.




          1. How to keep the behavior of coroutines just like before 1.3?



          Set the kotlinx.coroutines.scheduler system property to off.






          share|improve this answer























          • Thanks! Setting kotlinx.coroutines.scheduler to off fixed this problem.

            – ProtossShuttle
            Nov 14 '18 at 11:03










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279409%2fkotlin-coroutines-cause-heavy-cpu-usage-with-defaultdispatcher-after-migration%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1















          1. Why does it cost so much CPU usage with DefaultDispatcher?



          It's a completely different implementation that optimizes for several performance targets, for example communication via a channel. It is subject to future improvements.




          1. Why does kotlin 1.3 use DefaultDispatcher in place of ForkJoinPool.commonPool() by default?



          Actually it has been using the Default dispatcher all the time, but the resolution of Default changed. In the experimental phase, it was equal to the CommonPool but now it prefers the custom implementation.




          1. How to keep the behavior of coroutines just like before 1.3?



          Set the kotlinx.coroutines.scheduler system property to off.






          share|improve this answer























          • Thanks! Setting kotlinx.coroutines.scheduler to off fixed this problem.

            – ProtossShuttle
            Nov 14 '18 at 11:03















          1















          1. Why does it cost so much CPU usage with DefaultDispatcher?



          It's a completely different implementation that optimizes for several performance targets, for example communication via a channel. It is subject to future improvements.




          1. Why does kotlin 1.3 use DefaultDispatcher in place of ForkJoinPool.commonPool() by default?



          Actually it has been using the Default dispatcher all the time, but the resolution of Default changed. In the experimental phase, it was equal to the CommonPool but now it prefers the custom implementation.




          1. How to keep the behavior of coroutines just like before 1.3?



          Set the kotlinx.coroutines.scheduler system property to off.






          share|improve this answer























          • Thanks! Setting kotlinx.coroutines.scheduler to off fixed this problem.

            – ProtossShuttle
            Nov 14 '18 at 11:03













          1












          1








          1








          1. Why does it cost so much CPU usage with DefaultDispatcher?



          It's a completely different implementation that optimizes for several performance targets, for example communication via a channel. It is subject to future improvements.




          1. Why does kotlin 1.3 use DefaultDispatcher in place of ForkJoinPool.commonPool() by default?



          Actually it has been using the Default dispatcher all the time, but the resolution of Default changed. In the experimental phase, it was equal to the CommonPool but now it prefers the custom implementation.




          1. How to keep the behavior of coroutines just like before 1.3?



          Set the kotlinx.coroutines.scheduler system property to off.






          share|improve this answer














          1. Why does it cost so much CPU usage with DefaultDispatcher?



          It's a completely different implementation that optimizes for several performance targets, for example communication via a channel. It is subject to future improvements.




          1. Why does kotlin 1.3 use DefaultDispatcher in place of ForkJoinPool.commonPool() by default?



          Actually it has been using the Default dispatcher all the time, but the resolution of Default changed. In the experimental phase, it was equal to the CommonPool but now it prefers the custom implementation.




          1. How to keep the behavior of coroutines just like before 1.3?



          Set the kotlinx.coroutines.scheduler system property to off.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 11:12









          Marko TopolnikMarko Topolnik

          147k19197323




          147k19197323












          • Thanks! Setting kotlinx.coroutines.scheduler to off fixed this problem.

            – ProtossShuttle
            Nov 14 '18 at 11:03

















          • Thanks! Setting kotlinx.coroutines.scheduler to off fixed this problem.

            – ProtossShuttle
            Nov 14 '18 at 11:03
















          Thanks! Setting kotlinx.coroutines.scheduler to off fixed this problem.

          – ProtossShuttle
          Nov 14 '18 at 11:03





          Thanks! Setting kotlinx.coroutines.scheduler to off fixed this problem.

          – ProtossShuttle
          Nov 14 '18 at 11:03

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279409%2fkotlin-coroutines-cause-heavy-cpu-usage-with-defaultdispatcher-after-migration%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20