Skip to content

[#5904] Disable the Recycler by default - #5968

Closed
normanmaurer wants to merge 1 commit into
4.1from
disable_recycler_by_default
Closed

normanmaurer wants to merge 1 commit into
4.1from
disable_recycler_by_default

Conversation

@normanmaurer

Copy link
Copy Markdown
Member

Motivation:

The Recycler was introduced as a light-weight object pool, to reduce GC pressure. We use it in various places, like pooling ByteBuf objects (not the actual memory, just the container), write tasks (When you call write(...) from outside the EventLoop) and many more.

This usage gave us a lot of trouble in the past and also users a hard time when they ended up with a lot of objects in the Reycler due writing to fast etc.

Modifications:

Disable the recycler by default so users need to explicit enable it to use it.

Result:

Recycler is not used anymore by default.

Motivation:

The Recycler was introduced as a light-weight object pool, to reduce GC pressure. We use it in various places, like pooling ByteBuf objects (not the actual memory, just the container), write tasks (When you call write(...) from outside the EventLoop) and many more.

This usage gave us a lot of trouble in the past and also users a hard time when they ended up with a lot of objects in the Reycler due writing to fast etc.

Modifications:

Disable the recycler by default so users need to explicit enable it to use it.

Result:

Recycler is not used anymore by default.
@normanmaurer normanmaurer self-assigned this Nov 1, 2016
@normanmaurer

Copy link
Copy Markdown
Member Author

@nmittler @Scottmitch @buchgr @ejona86 @mosesn @vkostyukov PTAL ... As discussed in #5904.

We did a few internal benchmarks and not noticed any bad impact because of this change. Could you please check and maybe run a benchmark as well ?

@normanmaurer

Copy link
Copy Markdown
Member Author

Also @trustin @cconroy and @NiteshKant

@cconroy

cconroy commented Nov 1, 2016

Copy link
Copy Markdown
Contributor

I can confirm that I saw no performance difference when I disabled the recycler via config.

@vkostyukov

Copy link
Copy Markdown
Contributor

Thanks for the heads up @normanmaurer! I'm running a benchmark right now. Will report back when I have the results.

@vkostyukov

Copy link
Copy Markdown
Contributor

@normanmaurer

I've been running some experiments internally and I can confirm this affects us dramatically: GC times are 10x up. Although, I can see how this doesn't actually affect most of Netty users. Here is why.

As you probably know, we're not pooling in Finagle (yet) and turns out we've been relying on recyclers to "pool" our outbound/send buffers (we fall through the first condition because our allocators are not pooled). This makes GC times climb up (I constantly reproduced that on our ping-pong test for Mux).

Even though this affect us so much, I personally don't think this should be considered as a blocker. As long as the majority of the Netty community happy - we're happy. Worst case, as a workaround, we can re-enable recyclers back locally.

@normanmaurer

Copy link
Copy Markdown
Member Author

@vkostyukov gotcha.... actually the plan was to remove it completely at some point. With what you told me that may be not feasible at all. Let me think a bit about it.

@normanmaurer

Copy link
Copy Markdown
Member Author

AS this has such a big impact let me close this for now.

@normanmaurer
normanmaurer deleted the disable_recycler_by_default branch December 3, 2016 19:45
@vkostyukov

vkostyukov commented Dec 13, 2017 •

Copy link
Copy Markdown
Contributor

Going back to this (I thought you might be interested in hearing the update).

We've been profiling Finagle's HTTP implementations (Netty 4 vs Netty 3) and figured that N4 is performing slightly worse (spends about ~20% more of CPU time normalized per request) while handling the same amount of traffic.

Turns out Recyclers account for about half of that overhead. Disabling them manually, helped us to reduce the gap to ~10% in our tests. We're in the process of running more tests with different Finagle components to see if Recyclers can be disabled globally in Finagle.

I will report back when I know more.

@normanmaurer

normanmaurer commented Dec 14, 2017 via email

Copy link
Copy Markdown
Member Author

@normanmaurer

Copy link
Copy Markdown
Member Author

@vkostyukov not sure you saw the comment above :)

@johnou

johnou commented Dec 19, 2017 •

Copy link
Copy Markdown
Contributor

@normanmaurer when disabling the recycler we see increased GC pressure in new TLAB from MessageToMessageEncoder creating CodecOutputList, most likely coming from the LengthFieldPrepender in our pipeline.

screenshot from 2017-12-19 15-59-21

@normanmaurer

Copy link
Copy Markdown
Member Author

@johnou yeah this sounds possible. That said we could fix this one easily by use a normal FastThreadLocal here. The recycler is not really needed.

@vkostyukov

Copy link
Copy Markdown
Contributor

Hey @normanmaurer! Thanks for your patience (and sorry for me being so slow in responding). I think I might be able to get you those profiles. Our simulation framework isn't yet open-sourced so I guess I have to talk to somebody to make sure it doesn't expose any sensitive information.

What format would you prefer? I can do flame-graphs / plain flat texts.

@normanmaurer

Copy link
Copy Markdown
Member Author

@vkostyukov flame-graphs would be nice

@cconroy

cconroy commented Dec 19, 2017

Copy link
Copy Markdown
Contributor

One thing to keep in mind here is that the recycler is doing work in the application that effectively replaces the functionality of GC. GC isn't free, but the JVM has a lot of optimization in the GC implementations. So, while GC pressure might increase without the recycler, overall throughput may still in fact be higher! When I ran tests previously, I found that disabling the recycler had no performance impact but vastly reduced memory footprint.

@vkostyukov

Copy link
Copy Markdown
Contributor

As I think about this again, it might that Finagle is seeing such a big impact is because it's already quite heavily using thread-locals (though its Local primitive, a thread-local, but for futures).

As you know threads locals are cheap, but not free. They always make it to the top of the CPU profile in our simulations:

  2540698572 ( 1.99%)    2536  java.lang.ThreadLocal$ThreadLocalMap.getEntryAfterMiss
  2268468014 ( 1.77%)    2263  itable stub
  1994345805 ( 1.56%)    1992  java.lang.ThreadLocal$ThreadLocalMap.getEntry
  1758812059 ( 1.37%)    1756  u32_classify	[cls_u32]_[k]
  1489312083 ( 1.16%)    1483  sun.nio.cs.UTF_8$Decoder.decode
  1441616135 ( 1.13%)    1439  com.twitter.util.Promise.isDefined
  1396069234 ( 1.09%)    1394  java.lang.ThreadLocal$ThreadLocalMap.set
  1392016909 ( 1.09%)    1388  java.lang.ThreadLocal.get

One of the ideas we explored recently was to reduce the number of times we access thread-locals and turns out, disabling recyclers do the trick.

@normanmaurer

Copy link
Copy Markdown
Member Author

SO you say disabling recycler did the trick because it removes threadlocal access ina big fashion ?

@vkostyukov

Copy link
Copy Markdown
Contributor

I haven't checked the CPU profile with recyclers disabled but based on the throughput improvements, I'd guess it helps to some extent. One thing I forgot to mention, it's HTTP/1.1 that benefits the most (in Finagle) as its implementation is 100% in Netty. The rest of the protocols (i.e., ThriftMux) don't really notice recyclers.

@normanmaurer

normanmaurer commented Dec 19, 2017 via email

Copy link
Copy Markdown
Member Author

@johnou

johnou commented Dec 20, 2017 •

Copy link
Copy Markdown
Contributor

Created new issue for our discussion wrt LengthFieldPrepender / MessageToMessageEncoder #7528

@johnou

johnou commented Dec 21, 2017

Copy link
Copy Markdown
Contributor

@normanmaurer also seeing high allocation in PooledUnsafeDirectByteBuf when disabling the recycler, maybe I didn't run the application for long enough..?

screenshot from 2017-12-21 13-29-51

@normanmaurer

Copy link
Copy Markdown
Member Author

Well one of these ByteBuf will be created whenever you create a new ByteBuf so its no wonder these are allocated a lot.

@johnou

johnou commented Dec 21, 2017

Copy link
Copy Markdown
Contributor

@normanmaurer I thought they were suppose to be, well, pooled?

@normanmaurer

Copy link
Copy Markdown
Member Author

@johnou the internal storage is... and the external object (the ByteBuf itself) is in the recycler.

@normanmaurer

Copy link
Copy Markdown
Member Author

@vkostyukov any update ?

@vkostyukov

Copy link
Copy Markdown
Contributor

Just wanted to close the loop on this. We re-did our testing just recently and came to a conclusion that disabling recyclers improves the overall throughput of both ThrifMux and HTTP servers (at the cost of extra allocations):

Here are some concrete numbers we observed:

  • HTTP/1.1: about better 5% thrpt (per core) via 2% increase in allocations/request
  • ThriftMux: about 7% better thrpt (per core) via <1% increase in allocations/request

We're disabling recyclers in Finagle and are totally onboard if they will be disabled by default (or just removed in a long run) in Netty.

@ejona86

ejona86 commented Jun 25, 2018

Copy link
Copy Markdown
Member

FYI: ByteBufUtil's threadLocalDirectBuffer (non-obviously to me) relies on Recycler. If we disable recycler, at the very least we should add a comment when reading the io.netty.threadLocalDirectBufferSize property that it's probably a bad idea to enable it without also enabling the Recycler.

@normanmaurer

normanmaurer commented Jun 25, 2018 via email

Copy link
Copy Markdown
Member Author

@re-thc

re-thc commented Jun 29, 2018

Copy link
Copy Markdown

Is it as simple as removing the recycler completely OR removing certain items that are affecting performance from using the Recycler? Maybe not all objects are suitable for it, e.g. those that are easily created and so would be easier task for the JVM GC to deal with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants