I have a JsonString that needs to be written in file to proceed with further processing. I've written the below code for file writing.
val json = "{}" // sample Json.
val jsonString = new StringBuilder()
val formatjson = PrettyParams.nospace.copy(preserveOrder = true).pretty(json)
jsonString.append(formatjson)
val tmpFileIS = new ByteArrayInputStream(
jsonString.toString().getBytes()
)
try {
IOUtils.copyLarge(tmpFileIS, tmpBufferedOutputStream)
tmpBufferedOutputStream.flush()
} catch {
case e: Exception => {
e.printStackTrace()
throw e
}
} finally {
tmpFileIS.close()
}
I'm getting below OOM issue, exactly at the line of where I've written jsonString.toString().getBytes()
java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOfRange(Arrays.java:3664)
It looks like the JSON content's been generated is over the heap memory and throwing an error.
I should not be increasing a heap memory this time nor allocate more heap memory all the time for this kind of rare instance.
So, do I need to split the JSON contents into pieces and write it a file?
Hope it could solve the issue and be memory efficient.
but does anyone have a better solution for reading a String
byte to byte or byte[]
to feed ByteArrayInputStream
?