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) {
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.
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,
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.