jayunit100
jayunit100

Reputation: 17650

Paging space, the JVM, and Memory Mapped files (nio)

I recently read an article claiming that, with memory mapped files, paging space is conserved.

Question 1 : Why is this a significant benefit ?

That is, to clarify, I would pose this question:

Question 2 : If I hava a JVM, running with (say) 1G or RAM on a standard Linux distribution, and I open an 800MB file - what percentage of the JVM's memory will be taken up by paging space ?

And furthermore, what are the other resources that the file will take up ?

I guess the real question I'm asking is :

Question 3 : How do we define "paging space" when dealing with large files read by the JVM, and what are the key (non obvious) benefits Memory mapped files provide to an application (i.e. when are they ideal replacements to a regular old file stream) ?

I assume this is related to the way paging is implemented with the JVM file memory mapping.

Upvotes: 1

Views: 2086

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533870

Question 1 : Why is this a significant benefit ?

I assume it is referring to swap space. If you use the heap or direct memory it can be swapped to swap space. This is a very bad idea performance wise, but it could happen. Memory mapped files are paged to those file.

Saving swap space isn't such a big issue IMHO, but it does mean you don't have to re-configure your system because your application wants to use a lot of virtual memory.

You never want your heap to be swapped to disk.

Question 2 : If I hava a JVM, running with (say) 1G or RAM on a standard Linux distribution, and I open an 800MB file - what percentage of the JVM's memory will be taken up by paging space ?

Direct memory and memory mapped files don't count towards your heap. If your heap is using almost 1 G and your max is 1 G you can open a 800 MB file. In neither case will swap space be used.

Question 3 : How do we define "paging space" when dealing with large files read by the JVM, and what are the key (non obvious) benefits Memory mapped files ?

I don't know exactly what you mean by paging space. Perhaps you could define what paging space means to you.

The main benefit of memory mapped files is you can utilise a large amount of data and let the OS worry about which portions to read in or write out to disk. The area can be much larger than the physical memory and even larger than the amount of disk space you have.

An example of mapping an 8 TB file into Java memory http://vanillajava.blogspot.com/2011/12/using-memory-mapped-file-for-huge.html

Upvotes: 6

Related Questions