JVM - How much RAM should I allocate - java

I didn't find a suggestion on how much RAM I actually should allocate to the JVM.
Assuming that my computer has 4GB RAM and runs Windows 10. I nearly only use Java Applications on it. With how much RAM should I start the JVM? The system needs RAM for itself too, so in my logic, I should not allocate 4 GB even if that is possible.
This isn't a black/white question, I just want to have an idea and how to come to an answer in this question.

I didn't find a suggestion on how much RAM I actually should allocate to the JVM.
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.
I nearly only use Java Applications on it. With how much RAM should I start the JVM?
Unless you have a reason to change a command line parameter I would leave it as it is.
Note: you OS will need some memory, perhaps 1 GB and you want to leave 1 GB free for caching etc, so you could increase the heap size to 2 GB, but only if you needed to.
What if I run 4 programs in parallel? Can I still allocate 2GB for every of that program?
Yes, but it's a very bad idea. You application will start swapping and your computer can become unusable. I would try to stay under 2 GB total for this machine.
We have a business application here that often reaches to the maximum allocated Ram, which makes it lag a lot if a user has too many "tabs" open.
In that case, you should make sure you have the right tool for the job which means making sure your system has enough memory. 4 GB of memory costs $18. If you had a hobby PC with 4 GB, I would say, get more memory. Three years ago, my 8 year old son when he turned 8 I gave him a PC with 8 GB of memory because it's didn't make sense to buy less. I would just get 16 GB or 32 GB minimum. Then its worth spending some time tuning it, maybe.

you can type "jconsole" or "jmc" in command line to see how much memory java used.

Related

Why does java application take much memory?

I have a small Java application which is basically a command line utility which sleeps 99% of the time and runs once a day to copy a file in particular location. That app takes in memory 10 Mb. Of course, that is not a big number, but it looks like that app takes more memory than it could. Could you suggest how to reduce the amount of memory for that app?
I run it like this:
java -Xmx12m -jar c:\copyFiles\copyFiles.jar
AFAIK this heavily depends on the Garbage Collector that is used and the jvm version, but in general the VM is not very eager to give memory back to the OS, for numerous reason. A much broader discussion is here about the same topic
Since jdk-12, there is this JEP :
http://openjdk.java.net/jeps/346
that will instruct the VM (with certain flags) to give memory back to the OS after a "planned" garbage collector cycle takes place - this is controlled by some newly introduced flags; read the JEP to find out more.
You are allowing your application to use roughly 12 MB because you use the -Xmx12m parameter for the JVM. Java is a bit wasteful regarding memory. You can decrease the max amount of memory your application can use:
-Xmx10k - Can use up to 10 KB of memory.
-Xmx10m - Can use up to 10 MB of memory.
-Xmx10g - Can use up to 10 GB of memory.
Feel free to mix this setting however you want but be careful. If you exceed the set amount an OutOfMemory-exception is thrown.

32 bit jvm on a 32 bit RHEL PAE system

I have a RHEL 6 32 bit system installed in a 64 bit hardware. The kernel version is,
2.6.32-431.17.1.el6.i686
I posted a very detailed question and answer here. Now, I have the JVM which is also 32 bit. I believe even if I install a 64 bit JVM, I will be allowed to use only upto 3 GB since I have only 32 bit OS.
So, my question is, a single process can have more than 4 GB memory and still be used in a 32 bit system. In a similar manner, if I set my maximum heap size to 2 GB in the 32 bit OS, will I still be able to use the memory?
For instance, if my java application requires 10 GB memory, and I set the maximum heap size to 2 GB, my question is, once the 2 GB is exhausted, will my application terminate or swap the address space and continue working?
I am also interested in knowing a good limit on the maximum heap memory limit.
> I believe even if I install a 64 bit JVM,
No, you can't do this. You can only run 32 bit applications on a 32 bit linux OS - even if the underlying hardware is 64 bits.
> a single process can have more than 4 GB memory and still be used in a 32 bit system.
No, a 32 bit linux installation only supports 32 bit applications, and that means you're limited to an address space of max 4GB. For linux, the kernel needs 1 GB of that space. Native libraries, stack space and other things also needs a piece of the address space, leaving you with memory available for the heap of around 2-2.5GB.
The PAE feature enables the OS to utilize more than 4GB of memory in total, but individual processes are still limited to 4 GB of address space.
> if my java application requires 10 GB memory, and I set the maximum heap size to 2 GB, my question is, once the 2 GB is exhausted, will my application terminate
Yes, if the application needs more memory, it will terminate with an OutOfMemoryError
> I am also interested in knowing a good limit on the maximum heap memory limit.
There is no universal answer - it depends on the application and whatever else you want to run on your system.
I believe even if I install a 64 bit JVM, I will be allowed to use only upto 3 GB since I have only 32 bit OS.
You can install it, but it won't run. Only a 64-bit OS can run a 64-bit application, whether it is a JVM or any other program.
So, my question is, a single process can have more than 4 GB memory and still be used in a 32 bit system.
Not directly. A 32-bit program can only use about 1.5 to 3 GB direct depending on the OS. A program can use a disk cache which is much larger. It can also use other program which themselves use more memory (in total)
In a similar manner, if I set my maximum heap size to 2 GB in the 32 bit OS, will I still be able to use the memory?
If you have a 2 GB heap this should be fine so long as the process doesn't exceed how much memory your whole process can use. The heap is not all the memory used of course.
if my java application requires 10 GB memory, and I set the maximum heap size to 2 GB, my question is, once the 2 GB is exhausted, will my application terminate or swap the address space and continue working?
If you need more than 10 GB, you should ensure you have at least 12 GB of main memory AND a 64-bit OS. What will happen if you don't have enough will range from the program running very slow, or your program crashing, or in extreme cases your whole machine will practically stop.
Java applications run very badly once they start swapping. They either crawl to a stop, or they can practically stop the machine.
I am also interested in knowing a good limit on the maximum heap memory limit.
A good limit is less than your main memory size. The default limit for the server JVM is 1/4 of main memory. Without more information, I would leave it as it is.

exceeding maximum memory of a server running java processes

I have a machine with 10GB of RAM. I am running 6 java processes with the -Xmx option set to 2GB. The probability of all 6 processes running simultaneously and consuming the entire 2GB memory is very very low. But I still want to understand this worst case scenario.
What happens when all 6 processes consume a little less than 2GB memory at the same instant such that the JVM does not start garbage collection yet the processes are holding that much memory and the sum of the memory consumed by these 6 processes exceeds the available RAM?
Will this crash the server? OR Will it slow down the processing?
You should expect each JVM could use more than 2 GB. This is because the heap is just one memory region, you also have
shared libraries
thread stacks
direct memory
native memory use by shared libraries
perm gen.
This means that setting a maximum heap of 2 GB doesn't mean your process maximum 2 GB.
Your processes should perform well until they get the point where you have swapped some of the heap and a GC is performed. A GC assumes random access to the whole heap and at this point, your system could start swapping like mad. If you have a SSD for swap your system is likely to stop, or almost stop for very long periods of time. If you have Windows (which I have found is worse than Linux in this regard) and a HDD, you might not get control of the machine back and have to power cycle it.
I would suggest either reducing the heap to say 1.5 GB at most, or buying more memory. You can get 8 GB for about $100.
Your machine will start swapping. As long as each java process uses only a small part of the memory it has allocated, you won't notice the effect, but if they all garbage collect at the same time, accessing all of their memory, your hard disk will have 100% utilization and the machine will "feel" very, very slow.

Practical limitations of JVM memory and CPU usage?

Let's say money was not a limiting factor, and I wanted to write a Java program that ran on a single powerful machine.
The goal would be to make the Java program run as fast as possible without having to swap or go to disk for anything.
Let's say that this computer has:
1 TB of RAM (64 16GB DIMMs)
64 processor cores (8 8-core processors)
running 64-bit Ubuntu
Could a single instance of a java program running in a JVM take advantage of this much RAM and processors?
Are there any practical considerations that might limit the usage and efficiency?
OS process (memory & threads) limitations?
JVM memory/heap limitations?
JVM thread limitations?
Thanks,
Galen
A single instance can try to acces all the memory, however NUMA regions mean that things such as GC perform badly accessing memory in another region. This is getting faster and JVM has some NUMA support but it needs to improve if you want scalability. Even so you can get 256 MB of heap and use 700 of native/direct memory without this issue. ;)
The biggest limitation if you have loads of memory is that arrays, collections and ByteBuffer (for memory mapped files) are all limited to a size of 2 billion. (2^31-1)
You can work around these problems with custom collections, but its really something Java should support IMHO.
BTW: You can buy a Dell R910 with 1 TB of memory and 24 cores/48 threads with Ubuntu for £40K.
BTW: I only have experience of JVMs up to 40 GB in size.
First of all, the Java program itself: a poorly designed code wouldn't use that much computer-power. Badly implemented threads, for example, could make your performance be slow.
OS is a limiting factor, too: not all OS can handle well that amount of memory.
I think the JVM can deal with that amount of memory, since the OS supports it.

Java Memory Ocupation

i made a server side aplication that uses 18mb of non-heap and around 6mbs of head of a max of 30mbs. i set the max of heap with -Xms and Xmx, the problem is that when i run the program on ubuntu server it takes arround 170mbs instead of 18+30 or atleast 100Mbs in the max. Some one know how to put VM only getting 100MBs?
The JVM uses heap, other memory like thread stacks and share libraries. The shared libraries can be relatively large but they don't use real memory unless they are actually used. If you run JVMs they are shared between them but you cannot see this in the process information.
In a modern PC, 1 GB of memory costs around $100 so reducing every last MB many not be seen to be as important as it used to be.
In response to your comment
i have made some tests with Jconsole
and VMvisual. Xmx 40mbs Xms 25. the
problem is that iam restricted to
512mbs since its a VPS and i can't pay
for it now. The other thing is that
with 100mbs each i could put atleast 3
process's running.
The problem is, you are going about it the wrong way. Don't try and get your VM super small so you can run 3 VMs. Combine everything into 1 VM. If you have 512 memory, then make 1 VM with 256MB of heap, and let it do everything. You can have 10s or 100s of threads in a single VM. In most cases, this will perform better and use less total memory than trying to run many small VMs.

Categories

Resources