• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

two JVM under one Processor

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks Jeff..

I hope i understood to a great extent and this helps me.
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm, I completely missed that part. My bad.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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".
 
Rob Spoor
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 80652
476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the default maximum heap for the JVM has been changed and is more than 64MB.
 
Rob Spoor
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is indeed. It's apparently now 25% of the system memory, with a maximum of 1GB.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic