Kotlin - Coroutines cause heavy CPU usage with DefaultDispatcher after migration to Kotlin 1.3
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:
- Why does it cost so much CPU usage with
DefaultDispatcher
? - Why does kotlin 1.3 use
DefaultDispatcher
in place ofForkJoinPool.commonPool()
by default? - How to keep the behavior of coroutines just like before 1.3?
kotlin java.util.concurrent coroutine kotlinx.coroutines
add a comment |
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:
- Why does it cost so much CPU usage with
DefaultDispatcher
? - Why does kotlin 1.3 use
DefaultDispatcher
in place ofForkJoinPool.commonPool()
by default? - How to keep the behavior of coroutines just like before 1.3?
kotlin java.util.concurrent coroutine kotlinx.coroutines
add a comment |
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:
- Why does it cost so much CPU usage with
DefaultDispatcher
? - Why does kotlin 1.3 use
DefaultDispatcher
in place ofForkJoinPool.commonPool()
by default? - How to keep the behavior of coroutines just like before 1.3?
kotlin java.util.concurrent coroutine kotlinx.coroutines
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:
- Why does it cost so much CPU usage with
DefaultDispatcher
? - Why does kotlin 1.3 use
DefaultDispatcher
in place ofForkJoinPool.commonPool()
by default? - How to keep the behavior of coroutines just like before 1.3?
kotlin java.util.concurrent coroutine kotlinx.coroutines
kotlin java.util.concurrent coroutine kotlinx.coroutines
edited Nov 13 '18 at 11:07
ProtossShuttle
asked Nov 13 '18 at 10:52
ProtossShuttleProtossShuttle
886725
886725
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
- 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.
- Why does kotlin 1.3 use
DefaultDispatcher
in place ofForkJoinPool.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.
- How to keep the behavior of coroutines just like before 1.3?
Set the kotlinx.coroutines.scheduler
system property to off
.
Thanks! Settingkotlinx.coroutines.scheduler
tooff
fixed this problem.
– ProtossShuttle
Nov 14 '18 at 11:03
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
- 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.
- Why does kotlin 1.3 use
DefaultDispatcher
in place ofForkJoinPool.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.
- How to keep the behavior of coroutines just like before 1.3?
Set the kotlinx.coroutines.scheduler
system property to off
.
Thanks! Settingkotlinx.coroutines.scheduler
tooff
fixed this problem.
– ProtossShuttle
Nov 14 '18 at 11:03
add a comment |
- 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.
- Why does kotlin 1.3 use
DefaultDispatcher
in place ofForkJoinPool.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.
- How to keep the behavior of coroutines just like before 1.3?
Set the kotlinx.coroutines.scheduler
system property to off
.
Thanks! Settingkotlinx.coroutines.scheduler
tooff
fixed this problem.
– ProtossShuttle
Nov 14 '18 at 11:03
add a comment |
- 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.
- Why does kotlin 1.3 use
DefaultDispatcher
in place ofForkJoinPool.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.
- How to keep the behavior of coroutines just like before 1.3?
Set the kotlinx.coroutines.scheduler
system property to off
.
- 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.
- Why does kotlin 1.3 use
DefaultDispatcher
in place ofForkJoinPool.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.
- How to keep the behavior of coroutines just like before 1.3?
Set the kotlinx.coroutines.scheduler
system property to off
.
answered Nov 13 '18 at 11:12
Marko TopolnikMarko Topolnik
147k19197323
147k19197323
Thanks! Settingkotlinx.coroutines.scheduler
tooff
fixed this problem.
– ProtossShuttle
Nov 14 '18 at 11:03
add a comment |
Thanks! Settingkotlinx.coroutines.scheduler
tooff
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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