What's the maximum amount of heap space that one can allocate for java on a 64-bit platform? Is it unlimited?
Theoretically 264, but there might be limitations (obviously)
According to this FAQ it's only limited by memory and swap space on the local system:On 64-bit VMs, you have 64 bits of addressability to work with resulting in a maximum Java heap size limited only by the amount of physical memory and swap space your system provides.
See also Why can't I get a larger heap with the 32-bit JVM?
Also keep in mind you need to set the max heap via command line. Without an -Xmx command. without it Java uses 64mb + 30% = 83.2mb as a default max heap on 64 bit machines according the same FAQ.
java -Xmx1000g myClass
works fine on my machine. But it dosn't seem to yet support the 't' modifier so you can't specify max memory in terabytes yet :)
If you could make every atom in the universe into a byte of RAM, you could allocate it in a 64 bit address space.
Actually, that's a slight exaggeration.
There are 10^80 atoms in the universe (according to WolframAlpha), and 2^64 bytes of address space in a 64 bit system, so you'd only be able to address 1 out of every 5x10^60 atoms. But if you have 18 qintillion bytes of RAM, you'd probably need a couple of quantum black holes to power it with.
This probably depends on the system your VM is running in. If you are running a AMD x64 architecture the address space of currently shipped processors uses 48 Bits, not 64. This results in a theoretical maximum of roughly 256 TB. (See http://en.wikipedia.org/wiki/X86-64)
I am not a specialist in VMs, but any modern OS typically will give as much memory as there is physical RAM plus available virtual memory. Probably thats what the VM will pass to your application depending on its configuration.
With recent VMs from Sun, the practical heap limit size is usually 512 times the available physical and/or virtual memory. Even if the theoretical limit is much higher, the VM will allocate 1 byte for maangement purposes for each 512 bytes of heap memory on startup, so 1TB of heap will immediately require 2GB for the memory management.
In theory its between 2^63 and 2^64 bytes.
In practice it is limited by the amount of physical memory and swap space available on your machine. And the physical memory is in turn limited by your chipset (i.e. the number of address pins on the physical memory address bus) and motherboard (i.e. the number and size of the DIMM sockets).
Related
I want to chose a value for maximum JVM heap size. I have some questions about it.
1) Is it usually related with the available physical memory size on the system? If so, is there any formula about this?
2) For the 32-bit JVM, can I set a value larger than 4GB even if the physical memory is very large?
3) Do I need to consider some impact of OS (e.g. Windows, Linux)?
Thanks
1.) Yes amount of memory you can allocate to JVM is related to physical memory available on your system. There is no fixed formula, however there is a rule of thumb that you can use to decide the memory allocation size for your process. There is memory recommendation for different Operating Systems. So (PhysicalMemory - Recommended OS memory) can be allocated to the java process. It varies for 32-bit vs 64-bit OS. If you don't have any other process then this formula will work fine. However if you have other memory consuming processes then you need to take into account memory allocation of those processes as well.
e.g. Windows 7 has following recommended requirement 1 gigabyte (GB) RAM (32-bit) or 2 GB RAM (64-bit). So, if you have 4GB physical memory then you can allocate 2GB to your process assuming 2 GB is used by OS.
2.) For 32-bit JVM you can not set the value larger than 4GB. In fact if your total physical memory is 4GB. You cannot allocate more than 2GB to your Java process. Checkout this answer for more detailed explanation
3.) Yes you should consider OS impact. Check the recommended memory configuration for different Operations Systems and decide memory for your Java process.
Hope this helps.
I am using 32 bit version of Java in windows 7 64 bits. The system has a RAM of 6 GB. But when JVM is allocated memory by OS, it doesnot goes beyond 1.5 GB (same as in 32 bit OS). What are the possible reasons, that JVM is not allowed sufficient memory. And if possible how to fix it? I cannot upgrade to 64 bit JVM.
A 32-bit process on Windows is still subject to the same limits as running on a 32-bit Windows OS. See the answers to this question.
How much memory can a 32 bit process access on a 64 bit operating system?
This guidance from Oracle suggests that a 32-bit JVM can use approximately 1.5GB.
The maximum theoretical heap limit for the 32-bit JVM is 4G. Due to various additional constraints such as available swap, kernel address space usage, memory fragmentation, and VM overhead, in practice the limit can be much lower. On most modern 32-bit Windows systems the maximum heap size will range from 1.4G to 1.6G.
As indicated by this article:
The default maximum heap space is 1/2 of physical memory of size upto 192 bytes and 1/4th of physical memory for size up to 1G.
I know you have more than 1G, but maybe this also applies for you
32 bit application will not be able to use more that 4GB of RAM. In practice, it will not be able to use more than 3GB because it needs some virtual memory space reserved for operating system.
Besides that, by default JVM allocates up to a quarter of available RAM. If you want to override this use option:
java -XX:DefaultMaxRAMFraction=1
It should use all available RAM that is feasible for 32 bit application.
Source: http://jvm-options.tech.xebia.fr/#
To be able to use more memory you have to upgrade to 64 bits. Unfortunately a 64 bit JVM cannot load 32 bit dlls, so the calls to the dll must be made from a different process, and you have to use an rpc mechanism to talk with the process instead of using the dll directly. It's a lot. of work but it can be done.
I know the default Java heap memory is 128 MB. Since it is the default, I want to know whether this memory get automatically changed according to the RAM size. For an example, for a machine with 128 MB RAM, heap memory 128 is too much and it should be automatically changed. Because if an application use all of 128 heap, PC will end up in a trouble.
please help.
In Java 1.6 update 18 (and later), if unspecified, the default heap-size in a client JVM follows these rules:
The default maximum heap size is half of the physical memory up to a
physical memory size of 192 megabytes and otherwise one fourth of the
physical memory up to a physical memory size of 1 gigabyte.
For
example, if your machine has 128 megabytes of physical memory, then
the maximum heap size is 64 megabytes, and greater than or equal to 1
gigabyte of physical memory results in a maximum heap size of 256
megabytes.
Taken from the 1.6.0_18 update notes
In previous releases of Java heap size was NOT variable by default.
No, it won't be automatically changed unless you ask for it on startup using the -Xmx option.
You can't get more than 2 GB on a 32 bit machine in any case. If you need more than that, you'll have to get a 64 bit operating system with lots of RAM.
You can specify JVM heap size by using aptions -Xmx and -Xms
-Xmx - Max Size.
-Xms - Min Size.
You specify like
-Xmx64m -Xms32m (for 64 and 32 MB)
No. It is not automatically changed. Look here for more on HotSpot JVM memory options .
More Here on handling Java OutOfMemoryError.
Heap is part of RAM.But still there is a limit that we can not define heap size beyond some percentage of RAM.For example:- i have 32 bit winodws xp OS and 4
GB RAM. But i can not declare the heap size more than 1600 MB. My question here why we can not declare heap size to some large value say 3GB which
is lower than my 4GB RAM(as in 32 Bit can ustilize up to 4gb of RAM)? This is true for single process.
I mean i can start two tomcat or any other java process
allocating 1600MB heap size to each but i can not allocate 3200MB to single process.what is the reason that behind that?
32-bit windows only allows 2GB of address space to a single process (without special extensions enabled.) The OS keeps the other 2GB for itself. Then on top of that Heap is not all of the memory that JVM needs. There is permgen space, and the memory that the code of the JVM itself uses.
If its not 64 bit 4GB cannot be allocated.
I have set the default memory limit of Java Virtual Machine while running Java Application like this...
java -mx128m ClassName
I Know this will set maximum memory allocation pool to 128MB, but I don't know what the benefit is, of specifying this memory limit of JVM?
Please enlighten me in this issue...
On Sun's 1.6 JVM, on a server-class machine (meaning one with 2 CPUs and at least 2GB of physical memory) the default maximum heap size is the smaller of 1/4th of the physical memory or 1GB. Using -Xmx lets you change that.
Why would you want to limit the amount of memory Java uses? Two reasons.
Firstly, Java's automatic memory management tends to grab as much memory from the operating system as possible, and then manage it for the benefit of the program. If you are running other programs on the same machine as your Java program, then it will grab more than its fair share of memory, putting pressure on them. If you are running multiple copies of your Java program, they will compete with each other, and you may end up with some instances being starved of memory. Putting a cap on the heap size lets you manage this - if you have 32 GB of RAM, and are running four processes, you can limit each heap to about 8 GB (a bit less would be better), and be confident they will all get the right amount of memory.
Secondly (another aspect of the first, really), if a process grabs more memory than the operating system can supply from physical memory, it uses virtual memory, which gets paged out to disk. This is very slow. Java can reduce its memory usage by making its garbage collector work harder. This is also slow - but not as slow as going to disk. So, you can limit the heap size to avoid the Java process being paged, and so improve performance.
There will be a default heap size limit defined for the JVM. This setting lets you override it, usually so that you can specify that you want more memory to be allocated to the java process.
This sets the maximum Heap Size. The total VM might be larger
There is always a limit because this parameter has a default value (at least for the Oracle/Sun VM)
So the benefit might either be: you can give the memory to the app that it actually needs in order to work (efficiently) or if you come from the other direction: (somewhat) limit the maximum memory used in order to manage the distribution of resources among different applications on one machine.
There already has been a question about java and memory SO: Java memory explained
A very nice article about Java memory is found here. It gives an overview of the memory, how it is used, how it is cleaned and how it can be measured.
The defaults of the memory are (prior java 6):
-Xms size in bytes Sets the initial size of the Java heap. The
default size is 2097152 (2MB). The values must be a multiple of, and
greater than, 1024 bytes (1KB). (The -server flag increases the
default size to 32M.)
-Xmn size in bytes Sets the initial Java heap size for the Eden
generation. The default value is 640K. (The -server flag increases
the default size to 2M.)
-Xmx size in bytes Sets the maximum size to which the Java heap can
grow. The default size is 64M. (The -server flag increases the
default size to 128M.) The maximum heap limit is about 2 GB (2048MB).
Another source (here) states that in Java 6 the default heap size depends on the amount of system memory.
I assume this should help avoid high memory consumption (due to bugs or due to many allocations and deallocations). You would use this if you design for a low-memory system (such as an old computer with little amounts of RAM, mobile phones, etc.).
Alternatively, use this to increase the default memory limit, if it is not enough for you and you are getting OutOfMemoryExceptions for normal behavior.