Skip to content

Commit

Permalink
[SPARK-49932][CORE] Strictly close the resources used in `JsonUtils#t…
Browse files Browse the repository at this point in the history
…oJsonString` to avoid memory leak
  • Loading branch information
panbingkun committed Oct 11, 2024
1 parent ed0a63f commit 088b614
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions common/utils/src/main/scala/org/apache/spark/util/JsonUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,21 @@ private[spark] trait JsonUtils {
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

def toJsonString(block: JsonGenerator => Unit): String = {
val baos = new ByteArrayOutputStream()
val generator = mapper.createGenerator(baos, JsonEncoding.UTF8)
block(generator)
generator.close()
baos.close()
new String(baos.toByteArray, StandardCharsets.UTF_8)
var baos: ByteArrayOutputStream = null
var generator: JsonGenerator = null
try {
baos = new ByteArrayOutputStream()
generator = mapper.createGenerator(baos, JsonEncoding.UTF8)
block(generator)
new String(baos.toByteArray, StandardCharsets.UTF_8)
} finally {
if (generator != null) {
generator.close()
}
if (baos != null) {
baos.close()
}
}
}
}

Expand Down

0 comments on commit 088b614

Please sign in to comment.