Reputation: 341
I have created following class and ran on 15 terminals with command "java -Xms500m Class1".
I was expecting it to not allow me after 4 or 5 separate executions. I was expecting something like cannot create more JVMs but all programs are running. If each JVM creation requires 500Mb of initial heap memory (my RAM is 2GB) then the maximum limit of JVM creation should be four.
public class Class1
{
public static void main(String[] args)
{
int i=0;
while(true)
{
try
{
Thread.currentThread().sleep(100);
System.out.println("hi-"+i);
}
catch (InterruptedException e)
{
}
i++;
if(i == 1000000)
{
break;
}
}
}
Thanks, Amaresh
Upvotes: 4
Views: 2507
Reputation: 10458
You are able to create JVMs with accumulative heap size bigger than your physical memory because of virtual memory.
In both Unix and Windows, because hard drive are used as memory, you can create 4GB + sizeof(hard drive space assigned to be used as memory) JVMs. So if you have 4GB of RAM and you assigned 2G of your hard drive to be used as memory, you can create 12 JVMs using java -Xmx500 -Xms500.
In Linux the swap space is use as memory.
In windows the disk amount to be used as memory can be configured at System Properties->Advanced->Performance->Settings
Upvotes: 2
Reputation: 3316
Try using -Xms500m
as well. That will set the initial memory.
You were setting the "maximum" memory - but your code doesn't use much memory at all.
Your OS probably also has virtual memory, so it will go a little more than 2GB.
Also, remember that the 500m
is the heap size. The actual JVM will take a little more memory than the heap alone.
Upvotes: 1
Reputation: 3260
-Xmx
is the maximum heap size, -Xms
is the initial heap size. See here for more information about setting the heap size correctly.
Upvotes: 4