Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java app that uses a lot of memory. Use -Xmx?

Tags:

I have a java app that uses about 15G on a machine with 16G. I don't know if I should set the max heap size.

If set will the jvm eat all the ram up to the limit and then start garbage collecting and stop everything while it churns through 15G of heap objects?

If not will the jvm hurt performance by not using all of the available ram on the machine.

My specific vm is: Java HotSpot(TM) 64-Bit Server VM (build 1.6.0_03-b05, mixed mode).

Thanks

like image 389
Martin Redmond Avatar asked Jun 23 '09 01:06

Martin Redmond


People also ask

Why is my Java application using so much memory?

Java is also a very high-level Object-Oriented programming language (OOP) which means that while the application code itself is much easier to maintain, the objects that are instantiated will use that much more memory.

What program uses the most memory?

The most obvious offender is Google Chrome: it's long been reported by users that Chrome (and its several helpers) consumes upwards of 500 MB of RAM. Chrome is not the only app guilty of hogging RAM.

How much memory does a Java application use?

The JVM has a default setting of 1/4 of main memory. If you have 4 GB it will default to 1 GB. Note: this is a pretty small system and you get get some embedded devices and phones which this much memory. If you can afford to buy a little more memory it will make your life easier.

Can a Java application run out of memory?

A small Java application might have a memory leak, but it will not matter if the JVM has enough memory to run your program. However, if your Java application runs constantly, then memory leaks will be a problem. This is because a continuously running program will eventually run out of memory resources.


2 Answers

-Xmx15G will set the maximum heap size to 15 gig. Java will only allocate what it needs as it runs. If you don't set it, it will only use the default. For info on the default, see this post.

-Xms15G sets the minimum heap to 15 gig. This forces java to allocate 15 gig of heap space before it starts executing, whether it needs it or not.

Usually you can set them both to appropriate values depending on how you're tuning the JVM.

like image 149
Cogsy Avatar answered Dec 02 '22 15:12

Cogsy


In Java 6, the default maximum heap size is determined by the amount of system memory present.

According to the Garbage Collector Ergonomics page, the maximum heap size is:

Smaller of 1/4th of the physical memory or 1GB. Before J2SE 5.0, the default maximum heap size was 64MB.

By using the -Xmx switch can be used to change the maximum heap size. See the java - the Java application launcher documentation for usage details.

like image 20
coobird Avatar answered Dec 02 '22 16:12

coobird