-1

I've small Linux server (VPS) with 1GB of RAM i want to give Java VM only 256MB is there an option to limit Java VM to that memory size, so when I excede that limit I start to catch OutOfMemory Exception

The main problem that when load exceed on memroy I shall return overload error, and this is an accepted reponse, but the problem Java keeps going eating system memory that shall be free for other programs by force!!

Yes, I need to minimize the memory, because the Server is not dedicated for Java only and exceeding memory for the sack of Java is not an option.

and if there is no Java option that way, shall I go for C++ for linux develpment, complixety, threading or memory management is not a problem since this Java program is just a dedicated service

StackFlowed
  • 6,664
  • 1
  • 29
  • 45
  • 3
    -Xmx256m is the option. – President James K. Polk Oct 12 '15 at 13:45
  • Its a big secret, but Oracle actually does provide documentation. http://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html . what isn't a secret is the thousands of links google provides when you search for "java memory configuration" or any variant thereof. – Gimby Oct 12 '15 at 13:50
  • Also, don't try catch OutOfMemory exceptions (acually an error). It's very unlikely you will be able to recover from it. – Damien O'Reilly Oct 12 '15 at 13:51
  • actually I use small program, and Java goes up, the idea is simple, don't exceed you share of memeory, which i can't inforce by any means! trying this sample java -Xms1M -Xmx1M -Xss1M -jar program.jar must give error by theory, but no Java has minimum memory about 64M, it is ok, then I shall have 191MB, it goes up!!! – Mohammad Nabil Oct 12 '15 at 21:23

2 Answers2

0

You can control Java's heap size when you launch it with the -Xmx parameter. In your case, this would be -Xmx256m.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

The option you need is -Xmx256m.

Reading from JVM's documentation

-Xmx [g|G|m|M|k|K] This option sets the maximum Java heap size. The Java heap (the “heap”) is the part of the memory where blocks of memory are allocated to objects and freed during garbage collection. Depending upon the kind of operating system you are running, the maximum value you can set for the Java heap can vary.

abarisone
  • 3,707
  • 11
  • 35
  • 54
  • Try this command shall throw OutOfMemeroy error
    java -Xms1M -Xmx1M -Xss1M -jar program.jar
    no free -m shows system used more over 60 MB of memeory and release it after killing Java VM !!
    – Mohammad Nabil Oct 12 '15 at 21:17