简体   繁体   中英

Set memory limit for Java and Spring Boot App (jar)

I know java applications running in JRE, and Java allocates the memory for the app from the host machine.

Its also for spring boot app what is with embedded tomcat and run as jar.

Me asking for: How to set Host machine physical memory limit for java spring boot app.

Is there any way I can set the physical memory limit for every spring boot application jar Like containerization technology?

Example: myapp.jar is supposed to consume not more than 400MB ram.

I don't know if this is what you are looking for, Java already crates a VM while running an app. You can set the maximum allocation of memory while running your app with the -Xmx (Maximum heap size) parameter.

Example:

java -Xmx400m myapp.jar

In this example you set to 400Mb the max available amount of memory that can be allocated for the application, you can also set it as Gb by using 'G' after the amount (ie '-Xmx1G' )

Alternatively, if you are looking to set this limit inside the jar file I think this may be a duplicate of Can I set Java max heap size for running from a jar file?

Otherwise, if all the above is not what you are looking for and you still want a 'containerization technology' you can use docker: https://www.docker.com/

Useful links:

https://alvinalexander.com/blog/post/java/java-xmx-xms-memory-heap-size-control/\ https://spring.io/blog/2015/12/10/spring-boot-memory-performance\ https://nodramadevops.com/2019/10/docker-memory-resource-limits/

EDIT: docker may be what you need, just read the docs an you'll be fine in no time. https://docs.docker.com/config/containers/resource_constraints/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM