• Post Reply Bookmark Topic Watch Topic
  • New Topic

Memory out of heap error.  RSS feed

 
Dhiren Dash
Greenhorn
Posts: 7
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, I am new to Java programming and new to this forum too. But I do know some stuff here and there. This is a question I have been trying to solve for about an hour,

Given a string, compute a new string where identical chars that are adjacent in
the original string are separated from each other by a "*".

My implementation,



Am am getting error at line 13. An exception actually.
I/P given = aaaa
Console:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
at java.lang.AbstractStringBuilder.replace(Unknown Source)
at java.lang.StringBuilder.replace(Unknown Source)
at com.tcs.dash.StringBuild.edit(StringBuild.java:13)
at com.tcs.dash.StringBuildExample.main(StringBuildExample.java:14)

Please can anyone tell me where am I going wrong??
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Getting out-of-memory errors with a simple loop? It is likely that you have an endless loop and a constantly growing string buffer.

Probably a good idea to print out the string buffer with each iteration, so that you can see what is going on.

Henry
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about how the for-loop will end.
 
Dhiren Dash
Greenhorn
Posts: 7
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I figured it out using the debug option in eclipse. Apparently, each 'a' was getting replaced by "a*a". So it ended up with a new 'a' to compare with the next upcoming 'a' in the string everytime hence resulting in an infinite loop. This is the right code,

 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job! And thanks for the well formatted code.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Easy mistake to make - you've always got to be careful iterating over something as you are changing it. You might have found it easier to iterate over the original String, as that's immutable.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Did you try using insert() rather than replace()?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!