I am building a very complex software that will be used for production and will run on a server as a service. I need to make this jar have set max RAM usage when running with some calculations made by my program, i have seen that there are ways for setting the memory before running the built program, but i would like to set how much memory the jar is going to use when i am running it, is this possible?
Asked
Active
Viewed 161 times
-1
-
I need to make this jar have _unlimited_ RAM access when running. No, that is dangerous. You should never code directly in the app the memory usage (and reduce it if it reaches a point). That is the job of Java's Garbage Collection (which would be specified in JVM Arguments). Instead, do set a memory limit before running the built program. _If you really want to do this, you could call System.gc() to request the JVM to call for Garbage Collection_ but I can guarantee you there are much better solutions to solve your issue than manually coding it. – Pulse Sep 04 '20 at 19:58
-
Additionally, you never know if your program encounters issue during runtime. If it does, (_and it is given unlimited RAM access_) the issue might be bad enough to crash the server computer. – Pulse Sep 04 '20 at 20:01
-
@ElliottFrisch I'm assuming when they mean by unlimited ram access they mean that they have full control of all the ram in the server computer. So if the server has 128 GB of RAM, the application has access to all 128 Gigs of RAM to use. – Pulse Sep 04 '20 at 20:13
-
Frameworks that allows you to run a Java app as a service, such as yajsw, lets you configure through config files the size of heap space for the JVM. You'll have to set an specific value though, as others have mentioned. – Mauricio Guzinski Sep 04 '20 at 20:14
-
Ok, so alternatively, is it possible to have access to at least half size of the RAM? I wanted to have full access so then i could make particular calculations that could have improved my program's performances, but i see this is impossible. – DRE Sep 05 '20 at 09:52
-
@Pulse that is exactly what i would like, basically i want my program to have access only to a part of the memory. So you are telling me that i can set a max limit, what are the better solutions? I was asking to have full access to memory just to see how much memory the server had and then make calculations to improve the limit of my memory usage of my app. – DRE Sep 05 '20 at 09:55
-
@Mauricio Guzinski setting the amount of RAM that the JVM can use is the solution i am searching for, instead of using other software can i code it myself? Do you have any resources where i can learn how to make this happen? – DRE Sep 05 '20 at 09:58
-
1@AndreaDattero. Yes you can easily set the amount of RAM that the JVM can use for your app. But this is not set in your java app. Instead this is set at the moment you launch your app using JVM's arguments for this purpose. Example: `java -Xmx5G -jar myapp.jar`. It will run your app allowing the JVM to use up to 5 gigabytes of RAM. [Actually the 5G is just for the heapspace (main java memory), a bit more will be used for permgen and other auxiliary memory spaces, but it is usually not much.] Please check other JVM arguments in https://docs.oracle.com/en/java/javase/13/docs/specs/man/java.html – Mauricio Guzinski Sep 05 '20 at 20:19
-
1You might be interested in this as well: https://stackoverflow.com/questions/14763079/what-are-the-xms-and-xmx-parameters-when-starting-jvm/14763095#14763095 – Mauricio Guzinski Sep 05 '20 at 20:21
-
i see there is no other solution then... well we tried, i will just have to make a script that when run starts my app with the amount of RAM needed. – DRE Sep 06 '20 at 13:37
1 Answers
1
There are two issues here. As mentioned above, you can only request up to a specific amount of memory. Efficient garbage collection can help you reclaim memory that is no longer needed.
The second, and probably real, issue here is metering how much memory is actually used by the application. There are many frameworks (e.g., JMeter) for measuring how much memory is used - and this can be done with respect to the amount of data used. When doing NP-complete (or even just more than O(n) problems) this can be very useful from the users perspective ("This works well with up to 2 ||| objects")

Dr Dave
- 550
- 1
- 6
- 22
-
Ok, so it appears i have been misunderstood, my bad, basically i want to read the amount of memory that the server has and then make some calculations to set the max limit of memory my app can use, i know by default the JVM sets it to 256MB this is not enough most surely for my app mostly because my program stores data in RAM, 256MB are good but might not fit every type of client is going to use this app. – DRE Sep 05 '20 at 10:01
-
1In this case you could create a bash script which calls "free" (or other memory tool) that will tell you how much RAM is available and parse the result to use it in order set the -Xmx argument of your JVM command line. I would not use more than 90% of the available, but it's up to you to decide. About memory consumption of you app: if you want to know how much memory your java app uses I would suggest using visualVM: https://visualvm.github.io/ – Mauricio Guzinski Sep 05 '20 at 20:26