Loading a map using Properties class

I have a map with 75000 entries and each entry value will be of size 10kb on average.

I load this map into memory using Properties class . But due to the size of the map , I get OutOfMemoryException when the RAM on the host is small.

One option that i have is to read the entries in batches (like 10,000) into memory instead of loading the complete map. Read the next 10k after processing the initial 10k.

Is there any way to accomplish this using Properties class.

Also, is there any better approach of loading the map entries in this manner?

Thanks and Regards,
Sujith


Don't use Properties, which is legacy

  1. Divide entries into multiple files

  2. Read each file in sequence, load and process using Preferences

Example code:

package com.mypack.test;

import java.io.*;
import java.util.*;
import java.util.prefs.Preferences;

public class PreferencesExample {

    public static void main(String args[]) throws FileNotFoundException {
        Preferences ps = Preferences.userNodeForPackage(PreferencesExample.class);
        // Load file object
        File fileObj = new File("d:\\data.xml");
        try {
            FileInputStream fis = new FileInputStream(fileObj);
            ps.importPreferences(fis);
            System.out.println("Prefereces:"+ps);
            System.out.println("Get property1:"+ps.getInt("property1",10));

        } catch (Exception err) {
            err.printStackTrace();
        }
    }
}

Sample xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE preferences SYSTEM 'http://java.sun.com/dtd/preferences.dtd'>
<preferences EXTERNAL_XML_VERSION="1.0">
<root type="user">
<map />
<node name="com">
  <map />
  <node name="mypack">
    <map />
    <node name="test">
      <map>
        <entry key="property1" value="80" />
        <entry key="property2" value="Red" />
      </map>
    </node>
  </node>
</node>
</root>
</preferences>

I assume you don't need to load all the properties at the same time, but you rather need to iterate through all properties. Personally, I would go with parsing manually the file line by line and working in a stream-like fashion. If there is a lib for processing very large property I don't know it.