Having such simple app
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = reader.nextInt();
reader.close();
}
}
I run it with different Xmx parameters (keep in mind the JConsole has been injected to the process)
- -Xmx1M won't run
- -Xmx2M error
- -Xmx4M nothing prints
- -Xmx6M and bigger - will run
The JConsole shows me strange memory usage for the app
If ran with -Xmx8M
- Used 3.0MB
- Commited 8.4MB
- Max 8.4MB
If ran with -Xmx16M
- Used 4.0MB
- Commited 16.8MB
- Max 16.8MB
And with -Xmx32M
- Used 11.0MB
- Commited 33.6MB
- Max 33.6MB
While using external software to measure the memory consumption may have some impact on these values, it still seems to Java will eat everything you let it eat even if it's not required. This is a part of Java I hate the most, but trying to understand.
What happens with the memory that is not required by my program, but has been eaten by JVM because I allowed it to do so by changing Xmx parameter? Can I have impact the amount of "lost" memory using different way than Xmx parameter?