1

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.

Imranmadbar
  • 4,681
  • 3
  • 17
  • 30
  • Not sure what exactly you want to ask here. But the size of your jar depends on the dependencies that you have. Remove the dependencies that are not actually required by your application. Even containerisation will not help you here, because you will add layers in your container. – Govil Kumar Jul 06 '22 at 10:57
  • GovilKumar i am not talking about jar or any java app source size, I am searching tha any way that I can set the limit of memory when a java app (Spring boot jar) running time is occupied for different kinds of processes from its host machine. See my example – Imranmadbar Jul 06 '22 at 11:40

1 Answers1

0

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 (i.e. '-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/

jklaze
  • 145
  • 7
  • `-Xmx` only sets the maximum heap memory. There are other memory areas, that are used by the JVM, so the application will still consume more than what's specified with `-Xmx`, that's something one needs to keep in mind. – dunni Jul 06 '22 at 11:04
  • Yes it is. Java applications not only deal with Heap memory but other areas also. – Imranmadbar Jul 06 '22 at 11:59
  • At this point I think the best solution for what you need is Docker. You can set a container memory limit, although you must be aware about the risks of phisically limiting an application, I'll update the answer once again to add more info about it – jklaze Jul 08 '22 at 07:37