Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run spring boot fat jar with minimal memory?

Here is our maven MediSpan project. Actually it's a REST web service. Its return JSON object for Drug Drug Interaction query through GPI code for a specified patient. Here is an sample of an rest query:

http://localhost:8017/mspn/query?pid=1000&
gpis=83-20-00-30-20-03-10&gpis=64-99-10-02-12-03-20

Here is the structure of our project:

/opt/java/spring/boot/fat/jar/project/chorke─mspn─server/
    ├─ MediSpan.Documents.Monograph.css                   [     1,697 Byte]
    ├─ MediSpan.Documents.Monograph.xslt                  [    35,167 Byte]
    ├─ bitronix-tx-mgr-log-001                            [ 2,097,173 Byte]
    ├─ bitronix-tx-mgr-log-002                            [ 2,097,173 Byte]
    └─ chorke─mspn─server.jar!                            [26,022,610 Byte]
        ├─ medispan/                                      [   443,756 Byte]
        ├─ META-INF/                                      [    33,702 Byte]
        ├─ org/springframework/boot/loader/               [   165,003 Byte]
        ├─ com/chorke/                                    [    27,633 Byte]
        ├─ application.properties                         [       501 Byte]
        ├─ application.yml                                [     2,234 Byte]
        ├─ MediSpan.Foundation.Config.xml                 [    14,939 Byte]
        ├─ MediSpan.Foundation.Text.xml                   [     9,003 Byte]
        ├─ log4j.xml                                      [     2,254 Byte]
        └─ lib/                                           [25,688,056 Byte]
            ├─ aopalliance-1.0.jar
            ├─ btm-2.1.4.jar
            ├─ c3p0-0.9.1.2.jar
            ├─ camel-core-2.15.2.jar
            ├─ camel-jasypt-2.15.2.jar
            ├─ camel-quartz2-2.15.2.jar
            ├─ camel-spring-2.15.2.jar
            ├─ camel-spring-javaconfig-2.15.2.jar
            ├─ chorke-comn-spring-2.0.00-SNAPSHOT.jar     [    11,698 Byte]
            ├─ chorke-mspn-entity-2.0.00-SNAPSHOT.jar     [    13,486 Byte]
            ├─ chorke-mspn-parser-2.0.00-SNAPSHOT.jar     [    15,921 Byte]
            ├─ chorke-mspn-persis-2.0.00-SNAPSHOT.jar     [    23,328 Byte]
            ├─ chorke-mspn-utlity-2.0.00-SNAPSHOT.jar     [    25,684 Byte]
            ├─ commons-lang3-3.3.2.jar
            ├─ commons-logging-1.1.1.jar
            ├─ ehcache-core-2.6.11.jar
            ├─ jackson-annotations-2.4.6.jar
            ├─ jackson-core-2.4.6.jar
            ├─ jackson-databind-2.4.6.jar
            ├─ jasypt-1.9.2.jar
            ├─ javax.transaction-api-1.2.jar
            ├─ jaxb-core-2.2.11.jar
            ├─ jaxb-impl-2.2.11.jar
            ├─ log4j-1.2.17.jar
            ├─ medispan-business-5.1.10.jar
            ├─ medispan-concepts-5.1.10.jar
            ├─ medispan-conditions-5.1.10.jar
            ├─ medispan-documents-5.1.10.jar
            ├─ medispan-interactions-5.1.10.jar
            ├─ medispan-screening-5.1.10.jar
            ├─ medispan-utility-5.1.10.jar
            ├─ mybatis-3.2.8.jar
            ├─ mybatis-ehcache-1.0.2.jar
            ├─ mybatis-spring-1.2.2.jar
            ├─ ojdbc6-11.2.0.3.jar
            ├─ org.apache.servicemix.bundles.cglib-2.1_3_7.jar
            ├─ quartz-2.2.1.jar
            ├─ slf4j-api-1.7.12.jar
            ├─ slf4j-log4j12-1.7.12.jar
            ├─ snakeyaml-1.14.jar
            ├─ spring-aop-4.1.6.RELEASE.jar
            ├─ spring-beans-4.1.6.RELEASE.jar
            ├─ spring-boot-1.2.4.RELEASE.jar
            ├─ spring-boot-actuator-1.2.4.RELEASE.jar
            ├─ spring-boot-autoconfigure-1.2.4.RELEASE.jar
            ├─ spring-boot-starter-1.2.4.RELEASE.jar
            ├─ spring-boot-starter-actuator-1.2.4.RELEASE.jar
            ├─ spring-boot-starter-tomcat-1.2.4.RELEASE.jar
            ├─ spring-context-4.1.6.RELEASE.jar
            ├─ spring-context-support-4.1.6.RELEASE.jar
            ├─ spring-core-4.1.6.RELEASE.jar
            ├─ spring-expression-4.1.6.RELEASE.jar
            ├─ spring-jdbc-4.1.6.RELEASE.jar
            ├─ spring-jms-4.1.6.RELEASE.jar
            ├─ spring-messaging-4.1.6.RELEASE.jar
            ├─ spring-tx-4.1.6.RELEASE.jar
            ├─ spring-web-4.1.6.RELEASE.jar
            ├─ spring-webmvc-4.1.6.RELEASE.jar
            ├─ tomcat-embed-core-8.0.23.jar
            ├─ tomcat-embed-el-8.0.23.jar
            ├─ tomcat-embed-logging-juli-8.0.23.jar
            └─ tomcat-embed-websocket-8.0.23.jar

Here is the command of running this fat jar project and their matrix:

# without setting java option
java -jar chorke-mspn-server.jar

Started BootstrapApplication in 41.824 seconds (JVM running for 42.807)

  • Memory Used: 532,858 kbytes
  • Committed: 751,616 kbytes
  • Max: 1,847,808 kbytes
  • 9502 classes loaded

Working fine.

# setting java option for 1024 MB
java -Xmx1024M -XX:MaxPermSize=768M -XX:+CMSClassUnloadingEnabled \
-jar chorke-mspn-server.jar

Started BootstrapApplication in 42.134 seconds (JVM running for 43.084)

  • Memory Used: 160,016 kbytes
  • Committed: 422,912 kbytes
  • Max: 932,352 kbytes
  • 9505 classes loaded

Working fine.

# setting java option for 512 MB
java -Xmx512M -XX:MaxPermSize=384M -XX:+CMSClassUnloadingEnabled \
-jar chorke-mspn-server.jar

Started BootstrapApplication in 42.385 seconds (JVM running for 43.358)

  • Memory Used: 244,280 kbytes
  • Committed: 463,360 kbytes
  • Max: 465,920 kbytes
  • 9503 classes loaded

Working fine.

# setting java option for 256 MB
java -Xmx256M -XX:MaxPermSize=192M -XX:+CMSClassUnloadingEnabled \
-jar chorke-mspn-server.jar

Started BootstrapApplication in 42.202 seconds (JVM running for 43.174)

  • Memory Used: 244,280 kbytes
  • Committed: 463,360 kbytes
  • Max: 465,920 kbytes
  • 9503 classes loaded

threw exception [Handler processing failed; nested exception is java.lang.OutOfMemoryError: GC overhead limit exceeded] with root cause

java.lang.OutOfMemoryError: GC overhead limit exceeded
        at java.lang.Integer.valueOf(Integer.java:642)
        at medispan.foundation.convert.ValueType.asValue(Unknown Source)
        at medispan.foundation.dataaccess.providers.sql.SQLProvider.executeForResults(Unknown Source)
        at medispan.foundation.dataaccess.providers.sql.SQLDataAccessProvider.innerExecuteForCollection(Unknown Source)
        at medispan.foundation.dataaccess.DataAccessProvider.executeForCollection(Unknown Source)
        at medispan.business.dataaccess.BusinessDataAccessManager.load(Unknown Source)
        at medispan.business.dataaccess.QueryPropertyValues.getPropertyValue(Unknown Source)
        at medispan.business.dataaccess.QueryPropertyValues.getInnerValue(Unknown Source)
        more..

This application consist with embedded tomcat server, spring boot, spring mvc, myBatis, apache-camel, bitronix transaction manager, medispan. we wish to run it on 32~128 MB memory. Could you please kind enough to suggest us for running this application with optimal/minimal of memory usage?

like image 248
Śhāhēēd Avatar asked Oct 05 '15 06:10

Śhāhēēd


1 Answers

You need to run a Profiler like JProfile or VisualVM to see what is cosuming memory.

I can tell you right off the bat you are never going to get down to 32MB with all those extra libs in there. Apache Camel alone will probably need at least 256MB to run reliably. And Apache Camel is not something that should be embedded inside a micro service. That is a service your service should call externally. Also using JPA or pretty much any caching will make it hard to get the memory use that low.

I do have some high volume micro services in Spring Boot that run with just 32MB - 64MB but they basically just do though put and some computation. All those extra libs and services will greatly expand your memory usage.

like image 131
BrianC Avatar answered Oct 19 '22 10:10

BrianC