Solved

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

Posted on 2010-09-16
7
1,079 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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.
A solution for Fortify Path Manipulation.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

624 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