Simple project to show the difference in memory usage of Springs RestTemplate and WebClient APIs for this stackoverflow question.
- Start the
DemoServer
,DemoRestTemplate
andDemoWebClient
applications. - Start
jconsole -interval=1
and attach to both client applications. - Perform a GC on both clients.
- Type the absolute path to a file (in this case 550MB) in the client CLIs and press Enter.
- Wait a couple of seconds and perform a GC for the
DemoRestTemplate
application.
For some reason the RestTemplate
uses an excessive amount of memory (take the differently scaled y-axis into account) and creates a lot of garbage in the old generation.
- The initial manual GC
- The request
- The manual GC
It looks like the RestTemplate
creates multiple copies of the complete file (memory usage is roughly 5x file size) while the WebClient
creates just some helper objects and a buffer which it frees correctly after use.