wood burning stoves 2.0*
The moose likes Java in General and the fly likes JVM and swapping out Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "JVM and swapping out " Watch "JVM and swapping out " New topic
Author

JVM and swapping out

guptabandhu roy
Greenhorn

Joined: Mar 22, 2008
Posts: 9
Hi guys ,
I was wondering ... when the underlying OS runs out of RAM it swaps pages out . So how come the JVM throws out of memory errors ?

Secondly let us take a scenario of opening a text file . I read each line of the file , split it based on a delimiter , put it into a Vector , and then create a JTable to display the file . However for large files I am running into out of memory errors .

However when I open the same file in Excel , it takes a bit to load , but opens just fine and when scrolling , scrolls smoothly . How ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Java by default only uses 64MB. You can change this using the -Xmx JVM flag. The maximum for this flag is 1.5GB for 32-bit JVMs but that's a limitation by Windows, not the JVM. 64-bit JVMs have no limitation that I am aware of.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
guptabandhu roy
Greenhorn

Joined: Mar 22, 2008
Posts: 9
Hi guys ,

Thanks for the answer .

Java by default only uses 64MB. You can change this using the -Xmx JVM flag. The maximum for this flag is 1.5GB for 32-bit JVMs but that's a limitation by Windows, not the JVM. 64-bit JVMs have no limitation that I am aware of.


Ok but even then when the JVM sees that it has used up lets say the default 64 MB shouldn't it swap out objects to make more space for new objects ? That way we wouldn't run out of memory ... ever . And isn't that the concept behind virtual memory ... giving the programmer the illusion of unlimited address space far greater than the actual available RAM .
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

guptabandhu roy wrote:
Ok but even then when the JVM sees that it has used up lets say the default 64 MB shouldn't it swap out objects to make more space for new objects ? That way we wouldn't run out of memory ... ever .


No. OS swapping is used to get an application to use more memory than that is available. So, in this example, it is possible to configure, with swapping, to get the JVM to run with 64MB on a machine with less than 64MB.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

guptabandhu roy wrote:
And isn't that the concept behind virtual memory ... giving the programmer the illusion of unlimited address space far greater than the actual available RAM .


No. The concept of virtual memory is to give applications continuous memory. As far as all the applications are concerned, they believe that they aren't sharing it.

But yes, with virtual memory, it is possible with swapping to make an application believe that it has lots more memory than that is available. However, you still need to configure your application to use it, and the OS to swap it.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40


and BTW, NEVER EVER, I MEAN ABSOLUTELY NEVER configure a JVM to use swapped memory.

The garbage collector is the worst case scenario for swap. It touches all the oldest memory, making swapping completely inefficient. Accessing memory, which is supposed to take pico-seconds, will jump to nano-seconds or micro-seconds, making GC pauses many many times longer. And making your java program completely unusable.

Henry
 
jQuery in Action, 2nd edition
 
subject: JVM and swapping out