1

I have built a custom hashmap using two arrays. One contains keys another values. Now, I see sometimes JVM can't able to allocate required memory for those arrays and throws exception. Is there any method to solve this using page swapping or any other method ?

Code:

public class Test1{
public static void main(String args[]){


    try {

   int arrays[]  = new int[50000000] ;
for ( int i = 0; i < 50000000 ; i++)
        {
        arrays[i] = i ;
        }

    }

catch(Exception e){
        System.out.println(e) ;
    }
}
}

Edit:

    public void load() {
        try {
            FileChannel channel2 = new RandomAccessFile(str1, "r").getChannel();
            MappedByteBuffer mbb2 = channel2.map(FileChannel.MapMode.READ_ONLY, 0, channel2.size());
            mbb2.order(ByteOrder.nativeOrder());
            assert mbb2.remaining() == savenum * 8;
            for (int i = 0; i < savenum; i++) {
                long l = mbb2.getLong();
                keys[i] = l;
            }
            channel2.close();

            FileChannel channel3 = new RandomAccessFile(str2, "r").getChannel();
            MappedByteBuffer mbb3 = channel3.map(FileChannel.MapMode.READ_ONLY, 0, channel3.size());
            mbb3.order(ByteOrder.nativeOrder());
            assert mbb3.remaining() == savenum * 4;
            for (int i = 0; i < savenum; i++) {
                int l1 = mbb3.getInt();
                values[i] = l1;
            }
            channel3.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public void save() {
        try {
            FileChannel channel = new RandomAccessFile(str1, "rw").getChannel();
            MappedByteBuffer mbb = channel.map(FileChannel.MapMode.READ_WRITE, 0, savenum * 8);
            mbb.order(ByteOrder.nativeOrder());

            for (int i = 0; i < savenum; i++) {
                mbb.putLong(keys[i]);
            }
            channel.close();

            FileChannel channel1 = new RandomAccessFile(str2, "rw").getChannel();
            MappedByteBuffer mbb1 = channel1.map(FileChannel.MapMode.READ_WRITE, 0, savenum * 4);
            mbb1.order(ByteOrder.nativeOrder());

            for (int i = 0; i < savenum; i++) {
                mbb1.putInt(values[i]);
            }
            channel1.close();
        } catch (Exception e) {
            System.out.println("IOException : " + e);
        }
    }
Arpssss
  • 3,850
  • 6
  • 36
  • 80
  • 1
    Try starting the JVM with more heap space? – Tudor Jul 03 '12 at 19:18
  • 1
    Did you consider a SQL database? – BalusC Jul 03 '12 at 19:18
  • 1
    Page swapping is something the operating system does. Make sure you don't have any actual memory leaks first. (As a first step, profile the memory use of the application with a huge maximum heap size and check if it settles at some point.) – millimoose Jul 03 '12 at 19:19
  • @Tudor, all-time doing this is problematic. – Arpssss Jul 03 '12 at 19:20
  • @Arpssss Why would it be problematic? You can't expect every single app to make do with the defaults. Besides it's what, one switch in a launch script? – millimoose Jul 03 '12 at 19:21
  • @AdelBoutros, code is similar like http://stackoverflow.com/questions/10064422/java-on-memory-efficient-key-value-store – Arpssss Jul 03 '12 at 19:21
  • @BalusC, No. Because I think it will make slower. – Arpssss Jul 03 '12 at 19:23
  • @Arpssss What's your use case? Just out of curiosity, I'd like to see what would need more throughput than KyotoCabinet provides. (Seeing as even SQL databases on HDD RAID are plenty fast for datasets that size.) Personally, I'd just use that and deploy on SSDs. – millimoose Jul 03 '12 at 19:24
  • @Arpssss Also… how exactly do you expect swapping pages of memory to and from disk to have performance characteristics any different to a disk-backed database?! – millimoose Jul 03 '12 at 19:27
  • @millimoose, I don't need throughput more than KyotoCabinet . Actually, when I have tried with that it gives really worst performance to me. Tuning params is really an issue. Another point after 30/40 million it became worse. – Arpssss Jul 03 '12 at 19:28
  • @Arpssss Worst compared to what? We still don't know what you're doing, why does it require keeping a dataset in-memory, and why disk-backed databases would perform badly for a dataset that seems like it should fit into physical RAM easily. (Or why you're doing something that insanely performance-intensive in Java.) – millimoose Jul 03 '12 at 19:31
  • @millimoose, look. I get many suggestion on disk backed maps. Actully, I have say key-value of 100 millions. But memory can store 95 million. So, for 5 million, I don't want to make it disk baked. Because with disk backed solutions my experience is not good (may be I have make mistakes). – Arpssss Jul 03 '12 at 19:33
  • @millimoose, I have url set of 125 millions (nearly). Whose key are some part, value are another part. I have to insert/lookup in a fast way. It is required for our server. – Arpssss Jul 03 '12 at 19:36
  • @millimoose, only reason "why does it require keeping a dataset in-memory" of it is: I have not found a nice fast solution for disk backed map. An as data sets are smaller and "seems like it should fit into physical RAM easily" I have not care about storing in disk. – Arpssss Jul 03 '12 at 19:42

1 Answers1

1

Modifying your code to use an IntBuffer and LongBuffer

class LongIntParallelHashMultimap {
    private static final int NULL = 0;
    private final FileChannel channel1, channel2;
    private final LongBuffer keys;
    private final IntBuffer values;
    private final int capacity;
    private int size;

    public LongIntParallelHashMultimap(int capacity, String basename) throws IOException {
        assert (capacity & (capacity - 1)) == 0 : "Capacity " + capacity + " must be a power of 2";
        this.capacity = capacity;
        channel1 = new RandomAccessFile(basename + ".keys", "rw").getChannel();
        keys = channel1.map(FileChannel.MapMode.READ_WRITE, 0, capacity * 8).order(ByteOrder.nativeOrder()).asLongBuffer();
        // load keys into memory
        for(int i=0;i<capacity;i+=512) keys.get(i);

        channel2 = new RandomAccessFile(basename + ".values", "rw").getChannel();
        values = channel2.map(FileChannel.MapMode.READ_WRITE, 0, capacity * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
        for(int i=0;i<capacity;i+=1024) values.get(i);
    }

    public void put(long key, int value) {
        long key1 = key + 1;
        int index = indexFor(key1);
        while (keys.get(index) != NULL) {
            index = successor(index);
        }
        values.put(index, value);
        keys.put(index, key1);
        ++size;
    }

    /**
     * Uses a pre-allocated array and return the count of matches.
     */
    public int get(long key, int[] hits) {
        long key1 = key + 1;
        int index = indexFor(key1);
        int hitIndex = 0;

        while (keys.get(index) != NULL) {
            if (keys.get(index) == key1) {
                hits[hitIndex] = values.get(index);
                ++hitIndex;
            }
            index = successor(index);
        }

        return hitIndex;
    }

    private int indexFor(long key) {
        return Math.abs((int) ((key * 5700357409661598721L) & (capacity - 1)));
    }

    private int successor(int index) {
        return (index + 1) & (capacity - 1);
    }

    public int size() {
        return size;
    }

    public void close() {
        try {
            channel1.close();
            channel2.close();
        } catch (IOException ignored) {
        }
        try {
            ((DirectBuffer) keys).cleaner().clean();
            ((DirectBuffer) values).cleaner().clean();
        } catch (Throwable notSupportedOnThisPlatform) {
        }
    }
}

To allow the OS to swap the pages of your data structure you need to use off heap memory or memory mapped files. The memory mapped files are easier to manage and can be up to the size of your hard drive in size.

long heap = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
RandomAccessFile raf = new RandomAccessFile("array.dat", "rw");
IntBuffer map = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1 << 30).order(ByteOrder.nativeOrder()).asIntBuffer();
for (int i = 0; i < map.capacity(); i++)
    map.put(i, i);
long heap2 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
System.out.printf("Wrote %,d int values, heap used %,d bytes approx%n", map.capacity(), heap2 - heap);

prints

Wrote 268,435,456 int values, heap used 0 approx

BTW: You need a 64-bit JVM to map this data all at once. If you have a 32-bit JVM you will need to move the mapping around (which is a pain so use a 64-bit JVM if you can)

If you have a 200 MB array, this should fit into a 32-bit JVM.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks. But how to use memory mapped files for swapping arrays ? – Arpssss Jul 03 '12 at 19:24
  • You store the arrays data in the mapped ByteBuffer(s) and the OS will swap the pages of that data as it needs to. – Peter Lawrey Jul 03 '12 at 19:25
  • Ok. Is this can perform fast (theoretically) ? Another thing, previously I think, swapping is done by JVM. – Arpssss Jul 03 '12 at 19:43
  • Its as fast as accessing a primitive array or an object. The caches of the CPU do most of the work. There is a small overhead but you benefit from being able to layout your data in memory how you want. The swapping is done be the OS so writes you perform can continue even after the application exits or crashes. – Peter Lawrey Jul 03 '12 at 19:45
  • 1
    I have used memory mapped files to store data efficiently for up to 10x the physical memory of the computer. – Peter Lawrey Jul 03 '12 at 19:46
  • Thanks. Can you kindly give a pointer or pseudo code or a simple code to make it clear how to use it ? – Arpssss Jul 03 '12 at 19:47
  • You could have a look at my library which uses data structures mapped to memory mapped files. I have used something similar to this to implement a hash table. https://github.com/peter-lawrey/Java-Chronicle – Peter Lawrey Jul 03 '12 at 19:52
  • Can you kindly point to that code which uses data structures mapped to memory mapped files and/or the hash table code. Its a too large project to find :). – Arpssss Jul 03 '12 at 20:02
  • How to map it is depends on what your data is. What I do is create a wrapper which looks like a JavaBean but actually stores the data in ByteBuffer. For an `int[]` is fairly trivial. – Peter Lawrey Jul 03 '12 at 20:32
  • Thanks. Now, I have to try this for http://stackoverflow.com/questions/10064422/java-on-memory-efficient-key-value-store code :). – Arpssss Jul 03 '12 at 21:12
  • The compilication with Hash tables is growing them. If you know a maximum size in advance this can improve performance (and simplify the code) Do you have a 64-bit JVM? – Peter Lawrey Jul 04 '12 at 07:40
  • Its worth nothing that a hash table assumes random access to get best performance. If you have a data structure where part of it is outside of main memory, you can't use any structure which requires random access. Can you say anything about the access behaviour of the keys? – Peter Lawrey Jul 04 '12 at 07:42
  • I know the maximum size and also using 64 bit JVM. And access behaviour of the keys are completely random depending on dataset. – Arpssss Jul 04 '12 at 08:14
  • If I use above mentioned HashTable code along with your IntBuffer, is it can be a good one ? Need your comment on this. – Arpssss Jul 04 '12 at 08:16
  • That is basically what I would do. You can use LongBuffer for the long values. The problem you have is that for genuinely random behaviour, all you data must be in memory. You have to have enough memory. However, in almost all real world applications, access is not statistically random and this is not actually a problem, esp if you understand what the behaviour is and optimise for it. – Peter Lawrey Jul 04 '12 at 08:23
  • Thanks again. Peter, another issue is with: Array fill with NULL of your code. Currently, I am filling IntBuffer it with NULL by looping upto maximum size. And then inserting key-values. Is that OK. Or have nice another way to do that ? – Arpssss Jul 04 '12 at 08:27
  • And the above posted IntBuffer code: does it stores those arrays in the file ? Or I have to store IntBuffer by writing a code ? – Arpssss Jul 04 '12 at 08:30
  • The array starts with 0 values, you can't set it to `null`. You can start setting key-values immediately. – Peter Lawrey Jul 04 '12 at 08:51
  • All the data you place in the LongBuffer or IntBuffer is available to the OS immediately. If you write a value and then crash the JVM, the data is not lost. If you crash the OS or pull the power, then it can be lost. – Peter Lawrey Jul 04 '12 at 08:54
  • You can expect a random read/write to take 5 to 50 ns depending on whether the data is in cache or main memory. If the data is on disk, your speed is determined by the drive and how busy its is. An SSD can be 100 micro-seconds and a HDD can be 8 milli-seconds. This is way arranging the data not randomly, but to suit the access behaviour will mean you get 5 ns more often and 8 ms less often. – Peter Lawrey Jul 04 '12 at 08:56
  • Peter, in above code in map methods argument, if I put a long number greater than INT.MAXVALUE instead of (1 << 30). map method takes it, but throws **java.lang.IllegalArgumentException: Size exceeds Integer.MAX_VALUE** exception in later part. – Arpssss Jul 04 '12 at 14:10
  • For example: if I put 1000000000 * 4, it throws exception though 1000000000 number of int does not exceed INT.MAXVALUE. – Arpssss Jul 04 '12 at 14:14
  • If you want more than 1 << 30 bytes, I suggest creating more than one. i.e. have an `IntBuffer[]` each starting `(long) n << 30` with a length of `1 << 30` – Peter Lawrey Jul 04 '12 at 14:18
  • Peter, my coding with the above IntBuffer code is done. Issue is, this performs nice when put and get method is done together. But, when I only perform put and then close the application and then perform get operation, it became worse. – Arpssss Jul 04 '12 at 19:25
  • What do you mean exactly? Do you mean slower than you expect for JVM which hasn't warmed up? – Peter Lawrey Jul 04 '12 at 19:33
  • I mean slower than (http://stackoverflow.com/questions/11156945/java-store-and-upload-arrays-to-from-memory-to-disk) - means create hash table and load-storing that. – Arpssss Jul 04 '12 at 19:37
  • How to make it faster means to warm up or by any other way ? – Arpssss Jul 04 '12 at 19:38
  • I am confused. Why are you copying the array? – Peter Lawrey Jul 04 '12 at 19:41
  • I have to make hash-table persistent to use for future use. – Arpssss Jul 04 '12 at 19:42
  • So if you want to make it fast or persistent you shouldn't be copying the buffers, like I have suggested to you in two answers now. – Peter Lawrey Jul 04 '12 at 19:45
  • I am little bit confused. If I want to make it persistent (for future use) and also fast for future case, I have to do it using previous method: means create hash table and load-storing that using Mapped Byte Buffer. You mean this ? – Arpssss Jul 04 '12 at 19:55
  • Yes, You need to use the mapped ByteBuffer directly, not use a copy of it. – Peter Lawrey Jul 05 '12 at 06:47
  • Peter, some of our colleague says, it is possible to reduce the loading time of hashmap without pre-read it by following approach: Start working with it at the first second by using an approach such as it will try to load map into main memory until which it will response by page swapping. Do you think it is possible to implement in Java ? If so, can it will be faster ? – Arpssss Jul 10 '12 at 13:13
  • I don't understand what you mean. Can you reword it? – Peter Lawrey Jul 10 '12 at 13:31
  • Yah. They says NoSQL databases creates a hashtable in disk, in such a way thus they can start lookup from the first second. How to do that ? Have you any idea on this ? – Arpssss Jul 10 '12 at 13:32
  • Thats is exactly what I have been talking to you about. ;) You can access the data within a milli-second or less. – Peter Lawrey Jul 10 '12 at 13:35
  • Using a memory mapped file allows you to access any part without pre-reading. This is how NoSQL databases do it, and how you can do it in Java. – Peter Lawrey Jul 10 '12 at 13:38
  • But, for me the memory mapped files (code here: http://stackoverflow.com/questions/11398762/custom-hashmap-code-issue - my post's code, last portion SAVE(), LOAD() )doest not gives that much performance. Does it requires any modification ? – Arpssss Jul 10 '12 at 13:41
  • "Yes, You need to use the mapped ByteBuffer directly, not use a copy of it. " – Peter Lawrey Jul 10 '12 at 13:47
  • "So if you want to make it fast or persistent you shouldn't be copying the buffers, like I have suggested to you in two answers now." – Peter Lawrey Jul 10 '12 at 13:47
  • Can you kindly illustrate that one by little bit modifying the code or commenting on the code ? What it means "use the mapped ByteBuffer directly" ? – Arpssss Jul 10 '12 at 13:50
  • I have created an Edit section in the above post to give what code I am using, Or you can post your answer on http://stackoverflow.com/questions/11398762/custom-hashmap-code-issue. – Arpssss Jul 10 '12 at 14:19
  • I have added an edit based on your example. You can see that there is no load and close is trivial. – Peter Lawrey Jul 10 '12 at 15:34
  • Thanks a lot Peter. It works fine. Now, I have to deploy it in my application. Before that, I want some suggestions of you on this: 1) In my application keys can have value 0 but don' have value negative. So, in previous case I use Array Fill to fill with NULL = - 1 value. To accommodate this in your example, I just use for loop to fill key LongBuffer with -1 value. Is there any other approach ? 2) Your code seems take little memory footprint, should I insert (600/700) * 2 (load factor 0.5) millions on key-values to this or do it for 150 millions key-values as our memory permits. – Arpssss Jul 10 '12 at 17:55
  • By default, the values will be 0. Using another value would mean needing to fill all the entries. If you add 1 to the key or use `~key` this would allow you to use 0. – Peter Lawrey Jul 10 '12 at 17:55
  • This structure generally allows up to a load factor of about 0.5. – Peter Lawrey Jul 10 '12 at 17:56
  • So, we can do it by, ((key * 5700357409661598721L) & (capacity - 1))+1). Right ? – Arpssss Jul 10 '12 at 17:56
  • Is it good to insert so many key-value pairs (even more than memory) ? – Arpssss Jul 10 '12 at 17:57
  • You won't be able to insert more key-value pairs than the size allows. When you try to add one more value it will search endlessly for a free slot. See edit to handle +1 The performance of inserts will degrade after it is half full so make the size larger than double the size you will ever need. – Peter Lawrey Jul 10 '12 at 18:03
  • No. Actually, I want to know is it good to work with a large capacity say 2 pow 31 (for my 8 GB RAM, it seems, your code can allocate such a long array). But, it needs lots of page swapping. That's why I am asking, from your experience, is it good to work with large capacity or work with divided key-value database means with low capacity two database ? – Arpssss Jul 10 '12 at 18:11
  • If you need it to be so large compared to the main memory, you need to find a way to group together keys which are more likely to be used. e.g. you can increase collisions to reduce the amount of page swapping. – Peter Lawrey Jul 10 '12 at 19:29
  • I have used your code in our application. But, when we run it next day (means copy of data-set is not available to OS), performance is not nice. Just 5000 lookups/sec. While previous one is much much faster (means using copy of bytebuffer). I have to perform millions of lookups. – Arpssss Jul 11 '12 at 01:57
  • You can force the data into memory first if you need that to be the case. (by scanning every 4 KB of data to bring in each page) Grouping the data into less blocks will also improve performance. – Peter Lawrey Jul 11 '12 at 06:51
  • Peter, how to "force the data into memory first" ? Is that will be faster than "using copy of bytebuffer" ? Can you help how to do that ? – Arpssss Jul 11 '12 at 15:14
  • You can read every 1024th int or 512th long. This will take some time if the data is not in memory, but the limitation is the speed of your disk subsystem. Do you want to ensure the data is in memory and are willing to wait for it, or do you want to access the memory immediately? Taking a copy will mean you have two copies of your data in memory. Do you have enough memory for that? – Peter Lawrey Jul 11 '12 at 16:08
  • No peter, I have not enough memory to do that means keeping two copies of data in memory (if another one is MappedByteBuffer copy, then yes, because it takes less space) . But, I want to ensure the data is in memory and are willing to wait for it (but in a very fast manner, more faster than my OP). How to do that ? – Arpssss Jul 11 '12 at 16:15
  • "You can read every 1024th int or 512th long." That will read every page into memory. (Thank god for copy-and-paste) – Peter Lawrey Jul 11 '12 at 17:31
  • But, it is not possible to load full data in memory before starting by using IntBuffer? Anyway to do by forcing it ? And what you just suggested: "You can read every 1024th int or 512th long.", how to do that ? Is that faster ? – Arpssss Jul 11 '12 at 17:36
  • Actually, I want to ensure that either full data or large part of it is in memory thus swapping time will be less. And I want to do it in more faster way than OP. Can you kindly inform your suggestion by using little bit of code to clarify. – Arpssss Jul 11 '12 at 17:45
  • Added a loop for you in the example. As have have said before, this won't make your hard drive any faster. Nothing you can do in software will. – Peter Lawrey Jul 11 '12 at 18:35