Skip to content

Commit

Permalink
Improve ClassSet to avoid memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonlee committed Jun 3, 2022
1 parent 322fe76 commit ad3699f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ internal class ArchivedClassSet<ClassFile, ClassParser : ClassFileParser<ClassFi
override val parser: ClassParser
) : AbstractClassSet<ClassFile, ClassParser>() {

private val zip = ZipFile(location)

private val classes: Map<String, ClassFile> by lazy {
zip.entries().iterator().asIterable().parallelStream().filter(CLASS_ENTRY_FILTER).map { entry ->
parser.parse(zip.getInputStream(entry))
}.collect(toMap(parser::getClassName) { it })
ZipFile(location).use { zip ->
zip.entries().iterator().asIterable().parallelStream().filter(CLASS_ENTRY_FILTER).map { entry ->
parser.parse(zip.getInputStream(entry))
}.collect(toMap(parser::getClassName) { it })
}
}

constructor(location: String, parser: ClassParser) : this(File(location).takeIf {
Expand All @@ -47,7 +47,7 @@ internal class ArchivedClassSet<ClassFile, ClassParser : ClassFileParser<ClassFi

override fun iterator(): Iterator<ClassFile> = this.classes.values.iterator()

override fun close() = zip.close()
override fun close() = Unit

override fun toString(): String = this.location.canonicalPath

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ internal class DirectoryClassSet<ClassFile, ClassParser : ClassFileParser<ClassF

override fun toString(): String = this.location.canonicalPath

override fun close() {
}
override fun close() = Unit

}

0 comments on commit ad3699f

Please sign in to comment.