矩陣可能真的很大,有時甚至比一個數組中可以容納的更大。 您可以通過具有多個數組來擴展最大大小,但這會使堆大小確實很大且效率低下。 一種替代方法是在內存映射文件上使用包裝器。 內存映射文件的優點是它們對堆的影響很小,并且可以由操作系統相當透明地交換出來。
巨大的矩陣
此代碼支持double的大型矩陣。 它將文件分區為1 GB映射。 (由于Java一次不支持2 GB或更大的映射,這是我的寵兒;)
import sun.misc.Cleaner;
import sun.nio.ch.DirectBuffer;import java.io.Closeable;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;public class LargeDoubleMatrix implements Closeable {private static final int MAPPING_SIZE = 1 << 30;private final RandomAccessFile raf;private final int width;private final int height;private final List mappings = new ArrayList();public LargeDoubleMatrix(String filename, int width, int height) throws IOException {this.raf = new RandomAccessFile(filename, "rw");try {this.width = width;this.height = height;long size = 8L * width * height;for (long offset = 0; offset < size; offset += MAPPING_SIZE) {long size2 = Math.min(size - offset, MAPPING_SIZE);mappings.add(raf.getChannel().map(FileChannel.MapMode.READ_WRITE, offset, size2));}} catch (IOException e) {raf.close();throw e;}}protected long position(int x, int y) {return (long) y * width + x;}public int width() {return width;}public int height() {return height;}public double get(int x, int y) {assert x >= 0 && x < width;assert y >= 0 && y < height;long p = position(x, y) * 8;int mapN = (int) (p / MAPPING_SIZE);int offN = (int) (p % MAPPING_SIZE);return mappings.get(mapN).getDouble(offN);}public void set(int x, int y, double d) {assert x >= 0 && x < width;assert y >= 0 && y < height;long p = position(x, y) * 8;int mapN = (int) (p / MAPPING_SIZE);int offN = (int) (p % MAPPING_SIZE);mappings.get(mapN).putDouble(offN, d);}public void close() throws IOException {for (MappedByteBuffer mapping : mappings)clean(mapping);raf.close();}private void clean(MappedByteBuffer mapping) {if (mapping == null) return;Cleaner cleaner = ((DirectBuffer) mapping).cleaner();if (cleaner != null) cleaner.clean();}
}public class LargeDoubleMatrixTest {@Testpublic void getSetMatrix() throws IOException {long start = System.nanoTime();final long used0 = usedMemory();LargeDoubleMatrix matrix = new LargeDoubleMatrix("ldm.test", 1000 * 1000, 1000 * 1000);for (int i = 0; i < matrix.width(); i++)matrix.set(i, i, i);for (int i = 0; i < matrix.width(); i++)assertEquals(i, matrix.get(i, i), 0.0);long time = System.nanoTime() - start;final long used = usedMemory() - used0;if (used == 0)System.err.println("You need to use -XX:-UseTLAB to see small changes in memory usage.");System.out.printf("Setting the diagonal took %,d ms, Heap used is %,d KB%n", time / 1000 / 1000, used / 1024);matrix.close();}private long usedMemory() {return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();}
}
通過以下測試,該測試將一百萬*一百萬矩陣的每個對角線值寫入。 這太大了,無法希望在堆上創建。
Setting the diagonal took 314,819 ms, Heap used is 2,025 KB$ ls -l ldm.test
-rw-rw-r-- 1 peter peter 8000000000000 2011-12-30 12:42 ldm.test
$ du -s ldm.test
4010600 ldm.test
在Java進程中,虛擬內存為8,000,000,000,000字節或?7.3 TB! 這行得通,因為它僅在您使用的頁面中分配或分配頁面。 因此,盡管文件大小幾乎為8 TB,但實際使用的磁盤空間和內存為4 GB。
使用100K * 100K矩陣的較小文件大小,您將看到類似以下的內容。 它仍然是一個80 GB的矩陣,使用了很小的堆空間。 ;)
Setting the diagonal took 110 ms, Heap used is 71 KB$ ls -l ldm.test
-rw-rw-r-- 1 peter peter 80000000000 2011-12-30 12:49 ldm.test
$ du -s ldm.test
400000 ldm.test
參考:在Vanilla Java博客上,使用我們的JCG合作伙伴 Peter Lawrey 的巨大內存矩陣的內存映射文件
相關文章 :
- 如何在Java中獲得類似于C的性能
- Java中的低GC:使用原語而不是包裝器
- 回收對象以提高性能
- 改善Java應用程序性能的快速技巧
- Java Secret:加載和卸載靜態字段
- 具有GlassFish和一致性的高性能JPA –第1部分
翻譯自: https://www.javacodegeeks.com/2012/01/using-memory-mapped-file-for-huge.html