Commit b53e090
[SPARK-56302][CORE] Free task result memory eagerly during serialization on executor
### What changes were proposed in this pull request?
Eagerly null intermediate objects during task result serialization in `Executor` to reduce peak heap memory usage.
During result serialization in `TaskRunner.run()`, three representations of the result coexist on the heap simultaneously:
1. `value` — the raw task result object from `task.run()`
2. `valueByteBuffer` — first serialization of the result
3. `serializedDirectResult` — second serialization wrapping the above into a `DirectTaskResult`
Each becomes dead as soon as the next is produced, but none were released.
This PR nulls each reference as soon as it's no longer needed:
- `value = null` after serializing into `valueByteBuffer`
- `valueByteBuffer = null` and `directResult = null` after re-serializing into `serializedDirectResult`
All changes are confined to the executor side within `TaskRunner.run()`, where the variables are local and not exposed to other components.
### Why are the changes needed?
For tasks returning large results (e.g. `collect()` on large datasets), the redundant copies can roughly triple peak memory during serialization, increasing GC pressure or causing executor OOM. Eagerly freeing dead references lets the GC reclaim memory sooner.
### Does this PR introduce _any_ user-facing change?
No
### How was this patch tested?
Existing UTs
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code v2.1.88
Closes #55110 from ivoson/free-result-memory-asap.
Lead-authored-by: Tengfei Huang <tengfei.huang@databricks.com>
Co-authored-by: Tengfei Huang <tengfei.h@gmail.com>
Signed-off-by: yangjie01 <yangjie01@baidu.com>
(cherry picked from commit 3851cb5)
Signed-off-by: yangjie01 <yangjie01@baidu.com>1 parent a3b502e commit b53e090
File tree
1 file changed
+11
-3
lines changed- core/src/main/scala/org/apache/spark/executor
1 file changed
+11
-3
lines changedLines changed: 11 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| 60 | + | |
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
| |||
862 | 863 | | |
863 | 864 | | |
864 | 865 | | |
865 | | - | |
| 866 | + | |
866 | 867 | | |
867 | 868 | | |
868 | 869 | | |
| |||
917 | 918 | | |
918 | 919 | | |
919 | 920 | | |
920 | | - | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
921 | 924 | | |
922 | 925 | | |
923 | 926 | | |
| |||
961 | 964 | | |
962 | 965 | | |
963 | 966 | | |
964 | | - | |
| 967 | + | |
| 968 | + | |
965 | 969 | | |
966 | 970 | | |
967 | 971 | | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
968 | 976 | | |
969 | 977 | | |
970 | 978 | | |
| |||
0 commit comments