• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Out of memory error  RSS feed

 
Johny Kk
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting an OutOfMemoryError from the following method. Please find the method and error below. This method is invoked 1000's of times within the project. So I am confused whether this error is because of the input string size or OR due to recursive invocation.

public static InputStream stringToStream(String inputString) {

InputStream inputStream = null;

try {

InputStream inputStream = new ByteArrayInputStream(inputString.getBytes("UTF-8"));

} catch (UnsupportedEncodingException exp) {

exp.printStackTrace();

}

return inputStream;

}

Exception in thread "Thread-61" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.StringCoding.safeTrim(Unknown Source)
at java.lang.StringCoding.access$300(Unknown Source)
at java.lang.StringCoding$StringEncoder.encode(Unknown Source)
at java.lang.StringCoding.encode(Unknown Source)
at java.lang.String.getBytes(Unknown Source)

Thanks for reading this and will appreciate any inputs or advice.

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Addict Crazy on Java wrote:


Please check your private messages for an important administrative matter.
 
Sona Patel
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Johny,

OutOfMemory error comes when there are many objects in heap which has occupied almost all the memory. Possible reason is there are many objects which have not been garbage collected.

As i can see in the method, this method is creating InputStream object, and as per your description this method is called many times creating many objects of InputStream. You need to check if all the InputStream object created earlier are still being used. if not then check if they are being set to null or not. Making them null will help the garbage collector. There may be other methods in your code in which such objects are created and not set to null after use.

hope this helps

Regards,
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!