26 November, 2013

Spring-boot memory usage

Typical spring-boot application - single jar file.
Size of the file is about 10+ megabytes (it contains another jars, classes and resources inside).
What about memory usage for this app?
I created sample with only one web @Controller and two webmethods: first - reports memory usage, second - runs Java Garbage Collector.

Memory usage after start (http://localhost:8080/test/mem):
max: 1,875,378,176 bytes
total: 355,729,408 bytes
free:  178,138,976 bytes
used:  177,590,432 bytes
and after GC (http://localhost:8080/test/rungc):
max: 1,875,378,176 bytes
total: 355,729,408 bytes
free:  331,821,496 bytes
used:   23,907,912 bytes
With spring-boot we have a lot of available memory)

Controller.java
package sb; 
 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
 
@RestController 
@RequestMapping("test") 
public class Controller { 
 
    @RequestMapping(value = "mem", produces = "text/plain")
    public String mem() { 
        long max = Runtime.getRuntime().maxMemory(); 
        long total = Runtime.getRuntime().totalMemory(); 
        long free = Runtime.getRuntime().freeMemory(); 
        long used = total - free; 
        return String.format("Memory:\n" +
                "max: %,d bytes\n" +
                "total: %,d bytes\n" +
                "free: %,d bytes\n" +
                "used: %,d bytes\n",
                max, total, free, used); 
    } 
 
    @RequestMapping("rungc") 
    public String rungc() { 
        Runtime.getRuntime().gc(); 
        return "Run GC";
    } 
} 


Application.java
package sb; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
 
@EnableAutoConfiguration 
@Configuration 
@ComponentScan 
public class Application { 
 
    public static void main(String[] args) throws Throwable {
        new SpringApplication(Application.class).run();
    } 
} 

build.gradle
buildscript { 
    repositories { 
        maven { url "http://repo.spring.io/libs-snapshot" }
    } 
    dependencies { 
        classpath("org.springframework.boot:spring-boot-gradle-plugin:0.5.0.M6")
    } 
} 
 
apply plugin: "java" 
apply plugin: "spring-boot" 
 
buildDir = "out" 
 
jar { 
    baseName = "sb-mem" 
    version = "0.1" 
} 
 
repositories { 
    mavenCentral() 
    maven { url "http://repo.spring.io/libs-snapshot" }
} 
 
dependencies { 
    def springBootVersion = '0.5.0.M6'
    compile("org.springframework.boot:spring-boot-starter-web:$springBootVersion")
} 

1 comment:

Billy Yarosh said...

Spring Actuator Metrics can help you discover this along with other useful endpoints. Thanks for the post. http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#production-ready-metrics