Why is Java HashMap slowing down?

I am trying to create a map with the contents of a file, and my code is as follows:

    System.out.println("begin to build the sns map....");
    String basePath = PropertyReader.getProp("oldbasepath");
    String pathname = basePath + "\\user_sns.txt";
    FileReader fr;
    Map<Integer, List<Integer>> snsMap = 
            new HashMap<Integer, List<Integer>>(2000000);
    try {
        fr = new FileReader(pathname);
        BufferedReader br = new BufferedReader(fr);
        String line; 
        int i = 1;
        while ((line = br.readLine()) != null) {
            System.out.println("line number: " + i);
            i++;

            String[] strs = line.split("\t");
            int key = Integer.parseInt(strs[0]);
            int value = Integer.parseInt(strs[1]);
            List<Integer> list = snsMap.get(key);
            //if the follower is not in the map
            if(snsMap.get(key) == null) 
                list = new LinkedList<Integer>();
            list.add(value);
            snsMap.put(key, list);
            System.out.println("map size: " + snsMap.size());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("finish building the sns map....");
    return snsMap;

The program runs very quickly, at first , but becomes much slower when the printed information:

 map size: 1138338
 line number: 30923602
 map size: 1138338
 line number: 30923603 
 ....

I am trying to find a mind with two System.out.println () sentences to judge the preliminary execution of BufferedReader and HashMap instead of the Java profiler. Sometimes it is necessary to obtain information about the card size after receiving information about the line number, and sometimes it takes some time to get information about the number of the number after receiving the card size. My question is: what makes my program slow? BufferedReader for a large file or HashMap for a large map?

+1
5

Eclipse, stdout/stderr, - , Eclipse . Eclipse.

, , 30 , , . - GC'ing, OutOfMemoryError.

+3

, , . , ( GC), , .

+2

, , , .

, System.out , . System.out - , , .

snsMap.put(key, list);

if. . put .

Java, Integer (, , API Java), (, , !). , , GNU trove, , . Trove - . TIntArrayList TIntObjectMap GNU. , , .

, a HashMap<Integer, List<Integer>> 3 * 16 . , , 2 * 16 . 1 + 30 ~ 1 . . GNU trove TIntObjectHash<TIntArrayList>, 4 + 4 + 16 4 , 144 . , , .

, Trove , , , int. int , 4 .

Java HashMap . : Entry, . - , Java. Integer, 16 (4 , 4 , 4 int, 4 ) AFAIK. 32- . , HashMap, , 16 () + 16 ( ) + 32 ( LinkedList) , .

Integer, 4 , , int. , , Java.

+2

- (, JProfile) , . , , .

0

, . HashTable - , , .

. BufferedReader File , ... .

: br.close()    file.close()

Please check the system processes in the task manager, perhaps the processes may be running in the background.

Sometimes eclipse is a real resource, so try running it from the console to check it out.

0
source

Source: https://habr.com/ru/post/1570143/


All Articles