Solved

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

Posted on 2010-09-16
7
1,054 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 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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:
This video teaches viewers about errors in exception handling.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now