Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2010-09-16
Medium Priority
Last Modified: 2013-12-29

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.");

        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.
                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) {

        int v = 80;

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


        if (DEBUG) {

Question by:HPBSM
LVL 86

Accepted Solution

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

Expert Comment

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

java -Xmx1g .....
LVL 92

Expert Comment

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 16

Expert Comment

ID: 33699384
Are you getting this exception always on this line ExcessReturnCalculator.java:996 line 996 or not?

Expert Comment

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.

Expert Comment

ID: 33726038

         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.
LVL 86

Expert Comment

ID: 33849118

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

564 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