Why does the (Oracle) JVM have a fixed upper limit for memory usage (-Xmx)?

后端 未结 8 751
生来不讨喜
生来不讨喜 2020-11-28 09:30

In the spirit of question Java: Why does MaxPermSize exist?, I\'d like to ask why the Oracle JVM uses a fixed upper limit for the size of its memory allocation pool.

8条回答
  •  情书的邮戳
    2020-11-28 10:19

    I think part of it has to do with the implementation of the Garbage Collector (GC). The GC is typically lazy, meaning it will only start really trying to reclaim memory internally when the heap is at its maximum size. If you didn't set an upper limit, the runtime would happily continue to inflate until it used every available bit of memory on your system.

    That's because from the application's perspective, it's more performant to take more resources than exert effort to use the resources you already have to full utilization. This tends to make sense for a lot of (if not most) uses of Java, which is a server setting where the application is literally the only thing that matters on the server. It tends to be slightly less ideal when you're trying to implement a client in Java, which will run amongst dozens of other applications at the same time.

    Remember that with native programs, the programmer typically requests but also explicitly cleans up resources. That isn't typically true with environments who do automatic memory management.

提交回复
热议问题