High Java memory usage even for small programs

前端 未结 2 1323
梦毁少年i
梦毁少年i 2020-12-30 21:07

I have a couple of simple applications written in java, one of them written to act as a widget. What surprised me how much RAM even small applications use.

I wrote t

2条回答
  •  自闭症患者
    2020-12-30 21:37

    There are several reasons for this:

    1. The java runtime is a rather complex program in itself. It needs to take the byte code of your Java program (the output from javac), translate it into machine code for the system it is running on, there's an optimizer for that, there are interfaces for debugging, etc.
    2. Although your program is rather small, Java will still load many classes from its standard library. You can observe this by starting it using 'java -verbose:class ram'
    3. Java allocates a great bunch of memory for your program in advance - it cannot know how much memory it will actually need. This is, among others, governed by the -Xmx option. There are several types of such memory. To find out more about them, you can use the JConsole tool, which is included in the JDK's bin folder. You can read more about it at java.sun.com. A summary of the memory areas used by Java is also given at this stackoverflow question

提交回复
热议问题