Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JVM and swapping out

 
guptabandhu roy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20497
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
guptabandhu roy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20907
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20907
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20907
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic