Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

Posted on 2010-09-16
7
Medium Priority
?
1,091 Views
Last Modified: 2013-12-29
Hi,

I get the following exception:


Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
      at java.util.regex.Matcher.<init>(Matcher.java:207)
      at java.util.regex.Pattern.matcher(Pattern.java:888)
      at java.util.regex.Pattern.split(Pattern.java:997)
      at java.lang.String.split(String.java:2293)
      at java.lang.String.split(String.java:2335)
      at com.mba.excessreturncalculator.ExcessReturnCalculator.loadRate(ExcessReturnCalculator.java:996)
      at com.mba.excessreturncalculator.ExcessReturnCalculator.loadStockData(ExcessReturnCalculator.java:706)
      at com.mba.excessreturncalculator.ExcessReturnCalculator.main(ExcessReturnCalculator.java:641)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)

Is there any option to improve the memory consumption of the following code (it's only a part of a big class):

    private void loadRate(String inputRateDir, Map<String, String> rateMap, String delimiter) {
        File rateDir = new File (inputRateDir);
        if (!rateDir.exists()) {
            System.out.println(inputRateDir + " does not exist. Exiting.");
            return;
        }

        if (rateDir.getName().equals("335")) {
            int y = 10;
        }

        File[] files = rateDir.listFiles();
        for (File file : files) {
            try {
                BufferedReader bufferedReader = new BufferedReader(new FileReader(file.getPath()));
                String line;
                // 0. Transformation
                // 1. Rate
                // 2. Date

                // Skipping the first 2 lines.
                bufferedReader.readLine();
                bufferedReader.readLine();
                boolean finish = false;
                while ((line = bufferedReader.readLine())!= null && !finish) {
                    String[] tokens = line.split(delimiter);
                    if (tokens.length > 2 && tokens[0] != null
                            && tokens[1] != null && tokens[2] != null) {
                        // Skipping tokens[0] which is Transformation
                        String rate = tokens[1];
                        String dateToSort = convertDateToSortable(tokens[2]);
                        rateMap.put(dateToSort, rate);
                    }
                    else {
                        finish = true;
                    }
                }
            }
            catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }

        int v = 80;

        if (rateDir.getName().equals("335")) {

        }

        if (DEBUG) {
            System.out.println("rateMap:");
            printMap(rateMap);
            System.out.println("-----------------------------------------------------");
        }
    }

0
Comment
Question by:HPBSM
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 33696515
You should be closing the files in a finally block. Actually i don't see at all ...
0
 
LVL 92

Expert Comment

by:objects
ID: 33697090
not really, looks like you need to increase the amount of memory available to your application

java -Xmx1g .....
0
 
LVL 92

Expert Comment

by:objects
ID: 33697111
you can try this:


                        String rate = new String(tokens[1]);
                        String dateToSort = convertDateToSortable(new String(tokens[2]));

Also some profileing will bive you a better indication of where the memory is being used
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 16

Expert Comment

by:Valeri
ID: 33699384
Are you getting this exception always on this line ExcessReturnCalculator.java:996 line 996 or not?
0
 
LVL 4

Expert Comment

by:msk_apk
ID: 33710362
when you dont close the file, definitely it occupies some memory. But when you repeatedly call that function, it might lead to too many open files problem than the OOM. But certainly it could eat up some memory. With respect to the code above only place where memory could keep increasing is the addition of variable in rateMap. But it also depends on the number of variables added into the map.

One way to debug the issue is, you can run this code as a separate java program. If you are able to reproduce this issue when you are running this as a java program, then the problem exist in this code and map could be the culprit. In this case, you can either increase the java memory usage through -Xmx variable. If you dont want to increase this memory consumption, then we shall limit the number of entries in the map based on the available memory.

If you are unable to reproduce this issue then problem could be there is other part of your program. Use OptimizeIt and java's default profiling mechanism to find where exactly the memory is leaking.
0
 

Expert Comment

by:player8
ID: 33726038
Objects,

         String rate = new String(tokens[1]);
         String dateToSort = convertDateToSortable(new String(tokens[2]));

 I guess you are thinking about the heap memory and garbage collection.It will be great if you could explain us difference between this approach and original problematic version.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33849118
:-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question