Simple project to show the difference in memory usage of Springs RestTemplate and WebClient APIs for this stackoverflow question.
- Start the
DemoServer,DemoRestTemplateandDemoWebClientapplications. - Start
jconsole -interval=1and 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
DemoRestTemplateapplication.
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.
