• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Memory wasted by spring Boot application

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the widely wasted resources in the world today is Memory. Due to inefficient programming, surprising (sometimes ‘shocking’) amount of memory is wasted. We see this pattern repeated in several enterprise applications. To prove this case, we conducted a small study. We analyzed the famous spring boot pet clinic application to see how much memory it is wasting. This application has been designed by the community to show how the spring application framework can be used to build simple but powerful database-oriented applications.

Environment

*Spring Boot 2.1.4.RELEASE
*Java SDK 1.8
*Tomcat 8.5.20
*MySQL 5.7.26 with MySQL Connector/J 8.0.15
*Stress Test

We used Apache JMeter, a popular open-source load testing tool, to conduct our stress test. We executed the load test for 30 minutes with the below settings:

*Number of Threads (Users) – 1000 (Number of users connects to the target)
*Ramp-Up Period (in seconds) – The time frame for all requests to start. As per our configuration at every 0.01 second, 1 new thread will start i.e 100 threads/second.
*Loop Count – These 1000 threads perform a test iterations back-to-back.
*Duration (seconds) – After ramp-up 1000 threads run continuously for 1800 seconds.


       Fig: Jmeter settings

We were exercising the following scenarios in our load test:

*Add a new pet owner to the system.
*View information pertaining to a pet owner.
*Add a new pet to a system.
*View information pertaining to a pet.
*Add information pertaining to a visit to a pet’s visitation history.
*Update the information pertaining to a pet.
*Update the information pertaining to a pet owner.
*View owner information by searching his name.
*View information of all owners.

How to measure memory wastage?

Industry has hundreds of tools to show the amount of memory used. But seldom we come across tools that can measure the amount of memory wasted due to inefficient programming.HeapHerois a simple tool that analyzes your heap dumps and tells how much memory is wasted due to inefficient programming.

We captured the heap dump from the Spring Boot Pet Clinic application when the test was running. (there are 7 different options to capture heap dumps from Java/Android applications. You can choose the option that is convenient for you).

We uploaded the captured heap dump into HeapHero tool. Tool generated this beautiful report showing that 65% of memory is wasted due to inefficient programming. Yes, this is a simple vanilla application, which is supposed to have all best practices implemented in it, that too on a highly celebrated framework is wasting 65% of memory.


Fig: Chart generated by HeapHero, showing 65% of memory is wasted by Spring Boot pet clinic application

Analyzing Memory wastage

From the report you can notice the following:

*15.6% of memory is wasted due to duplicate strings
*14.6% of memory is wasted due to inefficient primitive arrays
*14.3% of memory is wasted due to duplicate primitive arrays
*12.1% of memory is wasted due to inefficient collections

Duplicate Strings

Top reason for memory wastage in this Spring boot application (and in most enterprise application) is duplication of strings. The report shows how much memory is wasted due to duplicate of strings, what strings are they, who is creating them and how to optimize it.


                                      Fig: Duplicate Strings


You can notice that 15.6% of memory is wasted due to duplicate strings. Please note

*‘Goldi’ string is has been created 207,481 times.
*‘Visit’ string has been created 132,308 times. ‘Visit’ was the description we mentioned in the test script.
*‘Bangalore’ string has been created 75,374 times. ‘Banglore’ is the name of the city we specified in the test script.
*‘123123123’ has been created 37,687 times.
*‘Mahesh’ string has been created 37,687 times.

Apparently ‘Goldi’ is the name of the pet that was entered on the screen through the test script. ‘Visit’ was the description entered on the screen through the test script. Similarly, are the values. But the question why so many thousands of times these same string objects are created.

We all know that strings are immutable (i.e. once they are created, they can’t be modified). Given that why these many thousands of duplicate strings are created?

HeapHero tool also reports the code path where these duplicate strings are created.


        Fig: Codepath from where duplicate strings are originating


Here are the high-level recommendations to fix duplicate strings in your application. You can employ the strategies are applicable to your application.

Inefficient Collections

Another primary reason for memory wastage in the spring boot pet clinic application is inefficient collections implementation. Below is the excerpt from the HeapHero report:



       Fig: memory wasted due to inefficient collections


You can notice that 99% of LinkedHashSet in the memory doesn’t have any elements in them. If there are no elements, why even create LinkedHashSet? When you create a new LinkedHashSet object, space for 16 elements are reserved in memory. All the space reserved for those 16 elements are wasted now. If you do lazy initialization of the LinedHashset then this problem wouldn’t arise.

Bad Practice:



Bad Practice:



Similarly, another observation is: 68% of ArrayList contains only 1 element in them. When you create an ArrayList object, space for 10 elements are reserved in memory. It means in 88% of ArrayList 9 elements space is wasted. If you can initialize ArrayList with capacity this problem can be avoided.

Bad Practice: Initializing Collections with default.



Read More here: https://blog.gceasy.io/2019/11/06/memory-wasted-by-spring-boot-application/







 
I'm full of tinier men! And a tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!