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
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
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
TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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

719 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