0

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?

3
  • 1
    "This is a part of Java I hate the most, ..." and I guess, when running a non-Java software, like the web browser of your choice, you just give as many Gigabytes it wants, without ever asking, how many of them it actually needs... Commented Nov 4, 2018 at 15:33
  • It's not the same; When I write a hello world app, I write a hello world app. It's different from application that handles network protocols, streaming, caching, and lot more. It's expected that big software consumes a lot of memory, but not vice versa Commented Nov 5, 2018 at 11:00
  • Seriously. I bet 99% of all developers are unable to predict, how much memory their application, especially a trivial hello world app, will need. If asked, they round up. Generously. E.g., even a “hello world” application settles on the abstractions of the operating system. It writes to a “file” which turns out to be a console (terminal emulation). Different environments may load different libraries into your application. You are focusing on the heap memory, which is the smallest part of memory for such a tiny application. E.g., do you know, how much stack space has been allocated? Commented Nov 5, 2018 at 11:10

1 Answer 1

1

Java will eat everything you let it eat even if it's not required.

Java doesn't aggressively clean up memory. If you give it memory it expects to use it to reduce CPU consumption.

Can I have impact the amount of "lost" memory using different way than Xmx parameter?

You can reduce the minimum size, you can control how quickly it grows. In fact, there is over 500 parameters to control GCs, but in general, the advice is to set as few as possible unless you really know what you are doing to avoid running into issues which wouldn't have happened if you left them alone.

A key thing to remember is that your time is money, and memory costs money. At some point, it is not worth your time trying to save a little bit of memory.

Based on average wages for developers and average prices for PC memory (costs at your organisation will vary)

1 day - 16 GB
1 hour - 2 GB
5 minutes - 160 MB
1 minute - 32 MB

If you spend more than one minute of your time saving less than 32 MB, it probably wasn't worth it.

This is a part of Java I hate the most,

I hate the misuse of units, but really I should learn to get over it

m - milli
M - Mega
b - bits
B - Bytes
1 MB = 8 Mb = 1000000000 mB = 8000000000 mb
Sign up to request clarification or add additional context in comments.

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.