Avatar ·

What are the -Xms and -Xmx parameters when starting the JVM and how do they affect memory?

Hello everyone! I'm a beginner Java developer, and recently I encountered a problem when running my application on a server. The application processes large CSV files (about 500 MB), and I noticed that it periodically crashes with the error OutOfMemoryError: Java heap space.

\n\n

I run the application via command line approximately like this:

\n
java -jar myapp.jar
\n\n

A colleague said that I need to add the -Xms and -Xmx parameters, but I don't quite understand what they do. I tried adding them randomly:

\n
java -Xms512m -Xmx1024m -jar myapp.jar
\n\n

After that, the error started appearing less frequently, but it still sometimes occurs when the file is particularly large. I read the documentation, but it seems too complicated for a beginner.