13 Replies - 331 Views - Last Post: 04 December 2017 - 05:49 AM Rate Topic: -----

#1 chloeCodes  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 05-January 17

java.lang.OutOfMemoryError: Java heap space Error

Posted 03 December 2017 - 04:53 PM

Hi!
I am trying to test a method I have written, and I get Java heap space error.
Here is the class which contains the test method (Testmethod1)
import java.util.ArrayList;
public class HelloAdderGeneral implements Transformer<String>
{
    private String greetingMessage;
    //The string str passed to the constructor is added to the greeting message
    public HelloAdderGeneral(String greetingMessage)
    {
        //Initialise instance variables that belong to the class
        this.greetingMessage=greetingMessage;
    }    
    //Implementing the method declared in Interface Transformer<T>
    public String transform(String str)
    {
        return str + " " + greetingMessage;    
    }
   
    public static void main(String[] args)
    {
       testMethod1();
       //testMethod2();
    }
    
    public static void testMethod1()
    {
      //HelloAdderGeneral hag = new HelloAdderGeneral("Nearly finished");
      //public static <T> ArrayList<T> applyDest(Transformer<T> tran,ArrayList<T> a)
      Transformer<String> tran = new HelloAdderGeneral("Will this work..");
      ArrayList<String> list = new ArrayList<String>();
      list.add("t");
      list.add("e");
      list.add("s");
      list.add("t"); 
      Transformers t = new Transformers();
      //Passing objects of this new class to applyDest()
      t.applyDest(tran,list);
      System.out.println(tran.transform("hmm"));
    }
    
    public static <T> void printList(ArrayList<T> list)
    {
        for(int i = 0; i<list.size(); i++)
        {
            System.out.println(list.get(i));
        }
    }
    
    public static void testMethod2()
    {
        
    }
}

Here is the Generic Interface that I'm trying to implement:
//The interface defines a Generic type
//It has a single type parameter T
interface Transformer<T>
{
    public T transform(T obj);
}


And this is the method in the Transformers class:
 public static <T> void applyDest(Transformer<T> tran,ArrayList<T> a)
 {
     for(int i = 0; i<a.size(); i++)
     //For each object in ArrayList a, transform method is called 
     //on tran object. 
         a.add(tran.transform(a.get(i)));
      // return a;
 }


I'm really confused as to how a small program can lead to java heap space error. I would really appreciate some guidance.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: java.lang.OutOfMemoryError: Java heap space Error

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12275
  • View blog
  • Posts: 45,364
  • Joined: 27-December 08

Re: java.lang.OutOfMemoryError: Java heap space Error

Posted 03 December 2017 - 04:55 PM

Please post the entire error message exactly as it appears.
Was This Post Helpful? 0
  • +
  • -

#3 chloeCodes  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 05-January 17

Re: java.lang.OutOfMemoryError: Java heap space Error

Posted 03 December 2017 - 04:58 PM

Hi!

At run time- I'm only getting the following error:

java.lang.OutOfMemoryError: Java heap space

Cheers
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12275
  • View blog
  • Posts: 45,364
  • Joined: 27-December 08

Re: java.lang.OutOfMemoryError: Java heap space Error

Posted 03 December 2017 - 04:59 PM

Please post the entire error (stack trace and all)! That will help us trace through your code.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6541
  • View blog
  • Posts: 26,471
  • Joined: 12-December 12

Re: java.lang.OutOfMemoryError: Java heap space Error

Posted 03 December 2017 - 05:01 PM

Yes, post the error details and tell us what line(s) they are referring to, although something that shouts out is that you are incrementing to a.size() but, within the loop, call a.add(). That doesn't sound good.
Was This Post Helpful? 2
  • +
  • -

#6 chloeCodes  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 05-January 17

Re: java.lang.OutOfMemoryError: Java heap space Error

Posted 03 December 2017 - 05:05 PM

Hi Andrew,

What is wrong with calling a.add(); within the for loop? I can't see to see the problem...

Also, at run-time the only error I'm getting is: java.lang.OutOfMemoryError: Java heap space
Thanks

This post has been edited by chloeCodes: 03 December 2017 - 05:05 PM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6541
  • View blog
  • Posts: 26,471
  • Joined: 12-December 12

Re: java.lang.OutOfMemoryError: Java heap space Error

Posted 03 December 2017 - 05:07 PM

What is the size of a collection if you keep adding to it?
Was This Post Helpful? 2
  • +
  • -

#8 chloeCodes  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 05-January 17

Re: java.lang.OutOfMemoryError: Java heap space Error

Posted 03 December 2017 - 05:07 PM

Hi,

I just closed the packaged, and opened and then ran again:

The new Run-time errors are:
java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3332)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:448)
at java.lang.StringBuilder.append(StringBuilder.java:136)
at HelloAdderGeneral.transform(HelloAdderGeneral.java:14)
at HelloAdderGeneral.transform(HelloAdderGeneral.java:2)
at Transformers.applyDest(Transformers.java:21)
at HelloAdderGeneral.testMethod1(HelloAdderGeneral.java:36)
at HelloAdderGeneral.main(HelloAdderGeneral.java:19)
Thank you!
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12275
  • View blog
  • Posts: 45,364
  • Joined: 27-December 08

Re: java.lang.OutOfMemoryError: Java heap space Error

Posted 03 December 2017 - 05:08 PM

There should be a stack trace, which tells you what line(s) are causing the issues. The entire error message might look like:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at main.java.OutOfMemoryErrorExample.main(OutOfMemoryErrorExample.java:8)



Please post the entire error message! This will help us help you.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6541
  • View blog
  • Posts: 26,471
  • Joined: 12-December 12

Re: java.lang.OutOfMemoryError: Java heap space Error

Posted 03 December 2017 - 05:11 PM

So which is line 124? Which is line 14? trace through your code. I would think "ensureCapacityInternal" is also a clue.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12275
  • View blog
  • Posts: 45,364
  • Joined: 27-December 08

Re: java.lang.OutOfMemoryError: Java heap space Error

Posted 03 December 2017 - 05:13 PM

See your applyDest() method:
public static <T> void applyDest(Transformer<T> tran,ArrayList<T> a)
{
    for(int i = 0; i<a.size(); i++)
    //For each object in ArrayList a, transform method is called 
    //on tran object. 
        a.add(tran.transform(a.get(i)));
     // return a;
}



Each time you add an element to your ArrayList, a.size() increases. So if a.size() > 0 when applyDest() is invoked, the loop condition: i<a.size() will always be true. So you have an infinite loop. The OutOfMemoryError is caused by the fact that you don't stop adding new elements to the ArrayList.
Was This Post Helpful? 2
  • +
  • -

#12 chloeCodes  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 05-January 17

Re: java.lang.OutOfMemoryError: Java heap space Error

Posted 04 December 2017 - 02:42 AM

Hi Macos,

That makes complete sense for me. I need to do this operation:
 a.add(tran.transform(a.get(i)));
 }
, but I can't think of another boolean expression for the for loop?

Cheers
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6541
  • View blog
  • Posts: 26,471
  • Joined: 12-December 12

Re: java.lang.OutOfMemoryError: Java heap space Error

Posted 04 December 2017 - 03:08 AM

The comment is:

//For each object in ArrayList a, transform method is called
//on tran object.

For each element you are calling the transform method, but you are also adding the transformed item onto the ArrayList: is this your intention? That is, are you expecting the ArrayList to be doubled in size?

If so, then you could store the initial size() in a variable and use this variable to control the number of iterations.

If you should not be adding to the ArrayList then just remove the .add() part of the statement.
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12275
  • View blog
  • Posts: 45,364
  • Joined: 27-December 08

Re: java.lang.OutOfMemoryError: Java heap space Error

Posted 04 December 2017 - 05:49 AM

Should this be an insertion operation or an update operation? If you want to transform Strings, an update operation on the ArrayList seems more appropriate.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1