Skip to content

Commit

Permalink
[SPARK-49932][CORE] Use tryWithResource release `JsonUtils#toJsonSt…
Browse files Browse the repository at this point in the history
…ring` resources to avoid memory leaks

### What changes were proposed in this pull request?
The pr aims to use `tryWithResource` release `JsonUtils#toJsonString` resources to avoid memory leaks.

### Why are the changes needed?
Avoiding potential memory leaks.

### Does this PR introduce _any_ user-facing change?
No.

### How was this patch tested?
Pass GA.

### Was this patch authored or co-authored using generative AI tooling?
No.

Closes #48420 from panbingkun/SPARK-49932.

Authored-by: panbingkun <panbingkun@baidu.com>
Signed-off-by: yangjie01 <yangjie01@baidu.com>
  • Loading branch information
panbingkun authored and LuciferYang committed Oct 12, 2024
1 parent 1fb3d57 commit 6734d48
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@ import com.fasterxml.jackson.core.{JsonEncoding, JsonGenerator}
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.module.scala.DefaultScalaModule

import org.apache.spark.util.SparkErrorUtils.tryWithResource

private[spark] trait JsonUtils {

protected val mapper: ObjectMapper = new ObjectMapper().registerModule(DefaultScalaModule)
.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)
tryWithResource(new ByteArrayOutputStream()) { baos =>
tryWithResource(mapper.createGenerator(baos, JsonEncoding.UTF8)) { generator =>
block(generator)
}
new String(baos.toByteArray, StandardCharsets.UTF_8)
}
}
}

Expand Down

0 comments on commit 6734d48

Please sign in to comment.