參考下面連結:Spring-boot memory usage
因為 spring boot 可使用 jar 檔直接運行,不需要打包成 war,也不需要實體 tomcat,實際上,spring boot 使用的是 embedded tomcat,整個專案打包後的大小非常小,使得記憶體可用空間也能夠更多。
文中利用下面的 controller 來輸出記憶體使用量:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class AdminController {
@RequestMapping(value = "/admin/mem", method = {RequestMethod.GET}
, produces = "text/html;charset=UTF-8")
@ResponseBody
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(value = "/admin/rungc" , method = RequestMethod.GET)
@ResponseBody
public String rungc() {
Runtime.getRuntime().gc();
return "Run GC finish";
}
}
自己的專案,包含了 spring security、jdbc、rest service、以及 rabbitmq client
執行的結果如下:
剛啟動時記憶體使用量
(http://localhost:8080/admin/mem):
max: 1,908,932,608 bytes
total: 372,768,768 bytes
free: 274,485,040 bytes
used: 98,283,728 bytes
執行GC後的使用量
(http://localhost:8080/admin/rungc):
max: 1,908,932,608 bytes
total: 374,865,920 bytes
free: 346,690,128 bytes
used: 28,175,792 bytes
還不賴!
沒有留言:
張貼留言