aspose file tools*
The moose likes Java in General and the fly likes two JVM under one Processor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "two JVM under one Processor" Watch "two JVM under one Processor" New topic
Author

two JVM under one Processor

siva venkata prasad
Greenhorn

Joined: Jul 14, 2011
Posts: 17
Hi,

Could any one please help me in understanding the following.

Is it possible to have two JVM processes running under single CPU at same time..?
If yes how heap memory is shared across two JVM's?

Thanks in advance
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

siva venkata prasad wrote:
Is it possible to have two JVM processes running under single CPU at same time..?


Are you asking if you can run two programs at the same time? ... Then yes. Most, if not all, modern day operating systems can run two programs at the same time -- and the JVM is just another program.

siva venkata prasad wrote:If yes how heap memory is shared across two JVM's?


In general, OS uses virtual memory so that each program can "share" the physical memory. The programs don't use the same physical memory at the same time, but with paging (and swapping), they can share the memory.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

siva venkata prasad wrote:
Is it possible to have two JVM processes running under single CPU at same time..?


Yes.

If yes how heap memory is shared across two JVM's?


It's not. Whether you have 1 CPU or a thousand, each JVM has its own heap, isolated from the rest.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Henry Wong wrote:
siva venkata prasad wrote:If yes how heap memory is shared across two JVM's?


In general, OS uses virtual memory so that each program can "share" the physical memory. The programs don't use the same physical memory at the same time, but with paging (and swapping), they can share the memory.


And note that there's nothing special about the JVM here. Having 2 JVMs running is no different than having 1 JVM and 1 web browser. Either way, we have 2 independent processes, each with its own view of the computer's memory, managed by the OS.
siva venkata prasad
Greenhorn

Joined: Jul 14, 2011
Posts: 17
Thanks for all your reply..

So shall i deduct from the above that if one JVM process will come across Out Of Memory error, it will not impact another JVM execution.. ?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

siva venkata prasad wrote:
So shall i deduct from the above that if one JVM process will come across Out Of Memory error, it will not impact another JVM execution.. ?


Not exactly.

If JVM1 gets OOME, the OOME itself has no direct bearing on how much heap is available to JVM2. However, if JVM1 has gotten OOME, it may be because it is using a lot of memory, and, while that JVM's heap is not shared with any other JVM's heap, all the JVMs' heaps come from the same total memory available on the system. So, if JVM1 is using a lot of the system's memory, there may not be enough total system memory left for JVM2--or for some other process like a web browser.

However, in most cases, due to virtual memory, a single app doesn't usually consume all of the system's available memory.

Additionally, even if memory is available, the OS may have to do a lot of paging to load JVM2's memory back in.

So, while it's not strictly 100% true that JVM1's memory use will have no impact on JVM2, in most cases the only impact you'll see is a possible slow down due to paging.
siva venkata prasad
Greenhorn

Joined: Jul 14, 2011
Posts: 17
Thanks Jeff.

Could you please clarify me on the below..

Two JVM's are running.. and each JVM process will create an object of certain type for every 2 seconds.. JVM1 ecncountered OOME error.. and so is it not necessarily that JVM2 will also get OOME because of certain operation taken by OS..? Also is heap memory is global or can also be shared/allocated to each JVM during the launch of JVM.. I Mean, is it possible to dedicate some heap memory for JVM 1 and some to JVM 2 independently...if yes please tell me how to ?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

siva venkata prasad wrote:Thanks Jeff.

Could you please clarify me on the below..

Two JVM's are running.. and each JVM process will create an object of certain type for every 2 seconds.. JVM1 ecncountered OOME error.. and so is it not necessarily that JVM2 will also get OOME because of certain operation taken by OS..?


JVM2 will not get an OOME just because JVM1 did. However, if you have 2GB of memory on your system, and no virtual memory, and JVM1 is using 1.5 GB of memory, and JVM2 tries to use more than 0.5 GB of memory, then it will get OOME.

Also is heap memory is global or can also be shared/allocated to each JVM during the launch of JVM.. I Mean, is it possible to dedicate some heap memory for JVM 1 and some to JVM 2 independently...if yes please tell me how to ?


You seem to be confusing terms here. "Heap" in a Java context refers specifically to the heap memory in the current JVM only, by definition. Each JVM has its own heap for use by your program, and that heap is not shared with any other JVM. That is "inside" the JVM, and private to it. It is memory that the JVM has obtained from the OS.

However, in terms of getting available memory from your OS, Each JVM is just another process, no different from a web browser or word processor or game of Solitaire. All the apps running--one or more JVMs, word processors, web browsers, games, etc.--all have to share that memory, and if a JVM asks the OS for memory but can't get it because it's all being used, then that the program running inside that JVM will face an OOME.

An OOME comes when your Java program ask the JVM for heap memory for object creation but cannot get it. It might be because your program is already using up all the heap that you told the JVM to allow it to have (with a -Xmx arg at startup time), and this could happen regardless of how much memory is available or used on the system. Or it might be that your program is not using all the memory specified by -Xmx, but it is using all that the JVM has asked the OS for so far, so the JVM has to ask the OS for more, but there's no more available.

Either way, your program cannot get the memory it needs--either because it has used up all that the JVM will allow it, or because there's no more available on the system.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

siva venkata prasad wrote:Also is heap memory is global or can also be shared/allocated to each JVM during the launch of JVM.. I Mean, is it possible to dedicate some heap memory for JVM 1 and some to JVM 2 independently...if yes please tell me how to ?


If by "heap" you mean the system memory, then you can do something like this:



The -Xms256m tells the JVM to allocate a minimum of 256 MB of heap to your program, and the -Xmx says to give it that much as a max. So, effectively, you're telling the JVM to give your program exactdly 256 MB of memory. As I understand it, that should cause it to allocate 256 MB from the OS right away. however, I'm not sure if it really works that way.
siva venkata prasad
Greenhorn

Joined: Jul 14, 2011
Posts: 17
Many thanks Jeff..

I hope i understood to a great extent and this helps me.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Jeff Verdegan wrote:JVM2 will not get an OOME just because JVM1 did. However, if you have 2GB of memory on your system, and no virtual memory, and JVM1 is using 1.5 GB of memory, and JVM2 tries to use more than 0.5 GB of memory, then it will get OOME.

Not necessarily. If possible the OS will use a page file (Windows) or swap partition (Linux / Unix) to use as "extra memory" on disk. If no more real memory is available, the OS will try to put some of its contents into the page file / onto the swap partition. That doesn't mean that you will never get an OOME in your situation, but it won't occur always.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rob Spoor wrote:
Jeff Verdegan wrote:JVM2 will not get an OOME just because JVM1 did. However, if you have 2GB of memory on your system, and no virtual memory, and JVM1 is using 1.5 GB of memory, and JVM2 tries to use more than 0.5 GB of memory, then it will get OOME.

Not necessarily. If possible the OS will use a page file (Windows) or swap partition (Linux / Unix) to use as "extra memory" on disk.


Hence my "no virtual memory" comment. I was trying to eliminate this possibility for simplicity's sake.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Hmmm, I completely missed that part. My bad.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

"Heap memory" is an over-loaded term.

As I understand it, the OS manages the Heap on the computer. When JVM starts, the OS assigns a chunk of its heap memory to the JVM. The JVM then allocates that memory as its heap and stack space. A second JVM would get its own chunk of the OS heap memory to use as it sees fit.

So simply saying "heap" isn't clear what you are referring to. I believe you only get an OOME when the JVM asks the OS for more memory, and the OS says "Nope - you've gotten all you're going to get".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

fred rosenberger wrote:I believe you only get an OOME when the JVM asks the OS for more memory, and the OS says "Nope - you've gotten all you're going to get".

I don't think that's true. The JVM has a fixed maximum amount of memory it can use. By default it's 64MB but it can be specified using the -Xmx flag. An OOME occurs when the JVM needs more than this amount. If the OS can't provide the memory there will possibly also be an OOME, but this is not the only cause of OOMEs.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
I think the default maximum heap for the JVM has been changed and is more than 64MB.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

It is indeed. It's apparently now 25% of the system memory, with a maximum of 1GB.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rob Spoor wrote:
fred rosenberger wrote:I believe you only get an OOME when the JVM asks the OS for more memory, and the OS says "Nope - you've gotten all you're going to get".

I don't think that's true. The JVM has a fixed maximum amount of memory it can use. By default it's 64MB but it can be specified using the -Xmx flag. An OOME occurs when the JVM needs more than this amount. If the OS can't provide the memory there will possibly also be an OOME, but this is not the only cause of OOMEs.


Right.

It can happen when the app has used all the heap as per -Xmx or the default, regardless of how much is available on the system.

It can happen when the JVM asks the OS for more but it's all used up, regardless of how little of the -Xmx amount the app is using (though this is less common with virtual memory).

And, I just found out recently, it can happen when GC is taking too much time and leaves too little free. I think the default values are if > 98% of the JVM's time is spent on GC and a full GC still leaves < 2% of -Xmx free, then OOME is thrown. I'm pretty sure the values are tweakable with command line args.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: two JVM under one Processor