1

I have a small Java application which is basically a command line utility which sleeps 99% of the time and runs once a day to copy a file in particular location. That app takes in memory 10 Mb. Of course, that is not a big number, but it looks like that app takes more memory than it could. Could you suggest how to reduce the amount of memory for that app?

I run it like this:

java -Xmx12m -jar c:\copyFiles\copyFiles.jar

3
  • -Xmx12m allows Java to use roughly 12MB, so if it's using 10MB this looks fine Commented Mar 27, 2019 at 10:08
  • "app takes more memory than it could" - how much memory do you expect the app should consume? Commented Mar 27, 2019 at 10:14
  • @PavelSmirnov I'm not perfectly shure but the number should be much smaller than 10mb. Commented Mar 27, 2019 at 10:37

2 Answers 2

2

AFAIK this heavily depends on the Garbage Collector that is used and the jvm version, but in general the VM is not very eager to give memory back to the OS, for numerous reason. A much broader discussion is here about the same topic

Since jdk-12, there is this JEP :

http://openjdk.java.net/jeps/346

that will instruct the VM (with certain flags) to give memory back to the OS after a "planned" garbage collector cycle takes place - this is controlled by some newly introduced flags; read the JEP to find out more.

Sign up to request clarification or add additional context in comments.

Comments

1

You are allowing your application to use roughly 12 MB because you use the -Xmx12m parameter for the JVM. Java is a bit wasteful regarding memory. You can decrease the max amount of memory your application can use:

-Xmx10k - Can use up to 10 KB of memory.

-Xmx10m - Can use up to 10 MB of memory.

-Xmx10g - Can use up to 10 GB of memory.

Feel free to mix this setting however you want but be careful. If you exceed the set amount an OutOfMemory-exception is thrown.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.