Skip to content

Commit 6890863

Browse files
committed
Fix memory leak on empty inputs.
1 parent d246e29 commit 6890863

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

sql/catalyst/src/main/java/org/apache/spark/sql/execution/UnsafeExternalRowSorter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ private void cleanupResources() {
112112
Iterator<InternalRow> sort() throws IOException {
113113
try {
114114
final UnsafeSorterIterator sortedIterator = sorter.getSortedIterator();
115+
if (!sortedIterator.hasNext()) {
116+
// Since we won't ever call next() on an empty iterator, we need to clean up resources
117+
// here in order to prevent memory leaks.
118+
cleanupResources();
119+
}
115120
return new AbstractScalaRowIterator() {
116121

117122
private final int numFields = schema.length();
@@ -141,7 +146,7 @@ public InternalRow next() {
141146
);
142147
row.backingArray = rowDataCopy;
143148
row.pointTo(rowDataCopy, PlatformDependent.BYTE_ARRAY_OFFSET, numFields, objPool);
144-
sorter.freeMemory();
149+
cleanupResources();
145150
return row;
146151
}
147152
} catch (IOException e) {

0 commit comments

Comments
 (0)