从临时 table 请求数据时,Spark 查询中的内存泄漏导致错误

Memory leak in Spark query causes error when requesting data from the temporary table

首先,我在 scala 代码中将数据添加到 tempTable:

resultIndexed.show(490, false)    
resultIndexed.registerTempTable("pivoted") 

然后读入python(导入省略):

%spark.pyspark

import bkzep
import numpy as np
from bokeh.io import output_notebook, show
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from bokeh.layouts import gridplot
from pyspark.sql.functions import col, coalesce, lit, monotonically_increasing_id
from pyspark.sql import DataFrame
from pyspark.sql.functions import *

output_notebook(notebook_type='zeppelin')


%pyspark

from pyspark.sql.functions import *

def plot_summaries(sensor, dfName):
    df = sqlContext.table(dfName)
    pdf = df.toPandas()
      
        

    #sample = pdf.sample(50)
    source = ColumnDataSource(pdf)
    #print(pdf)

    TOOLTIPS = [    
        ("month", "@month"),        
        ("day", "@day"),    
        ("hour", "@hour"),
        ("min", "@{min}{0.3f}"),
        ("avg", "@{avg}{0.3f}"),
        ("max", "@{max}{0.3f}"),
        ("median", "@{median}{0.3f}"),
        ("stddev", "@{stddev}{0.3f}"),    
    ]

    TOOLTIPS2 = [    
        ("month", "@month"),
        ("day", "@day"),    
        ("count", "@{count}{0.3f}"),    
    ]

    fig = figure(title="Hourly summaries of '{}'".format(sensor),  tooltips=TOOLTIPS)
    #fig.line(x='id', y='avg', source=source, color="orange")
    #fig.line(x='id', y='min', source=source, color="green")
    #fig.line(x='id', y='max', source=source, color="red")
    fig.line(x='id', y='median', source=source, color="blue")
    #fig.line(x='id', y='stddev', source=source, color="aquamarine")

    #fig2 = figure(title="Hourly summaries of '{}' counters".format(sensor),  tooltips=TOOLTIPS2)
    #fig2.line(x='id', y='count', source=source, color="orange")

    show(gridplot([fig], ncols=1, plot_width=1000, plot_height=400))
    #show(fig)

sensors = [
    "Water_Level_Sensor_stddev",
"Water_Level_Sensor_mean"
]


%pyspark

from pyspark.sql.functions import *

keyCol = "month_day_hour"

#for sensor in sensors:
plot_summaries("Water_Level_Sensor_stddev", "pivoted")    

然后我在执行过程中得到了错误:

/spark/python/lib/pyspark.zip/pyspark/sql/pandas/conversion.py:134: UserWarning: toPandas attempted Arrow optimization because 'spark.sql.execution.arrow.pyspark.enabled' is set to true, but has reached the error below and can not continue. Note that 'spark.sql.execution.arrow.pyspark.fallback.enabled' does not have an effect on failures in the middle of computation.
  An error occurred while calling o117.getResult.
: org.apache.spark.SparkException: Exception thrown in awaitResult: 
    at org.apache.spark.util.ThreadUtils$.awaitResult(ThreadUtils.scala:302)
    at org.apache.spark.security.SocketAuthServer.getResult(SocketAuthServer.scala:88)
    at org.apache.spark.security.SocketAuthServer.getResult(SocketAuthServer.scala:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:282)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 1157.0 failed 4 times, most recent failure: Lost task 0.3 in stage 1157.0 (TID 81153, 10.244.1.26, executor 12): org.apache.spark.util.TaskCompletionListenerException: Memory was leaked by query. Memory leaked: (671744)
Allocator(toBatchIterator) 0/671744/671744/9223372036854775807 (res/actual/peak/limit)


Previous exception in task: sun.misc.Unsafe or java.nio.DirectByteBuffer.<init>(long, int) not available
    io.netty.util.internal.PlatformDependent.directBuffer(PlatformDependent.java:490)
    io.netty.buffer.NettyArrowBuf.getDirectBuffer(NettyArrowBuf.java:243)
    io.netty.buffer.NettyArrowBuf.nioBuffer(NettyArrowBuf.java:233)
    io.netty.buffer.ArrowBuf.nioBuffer(ArrowBuf.java:245)
    org.apache.arrow.vector.ipc.message.ArrowRecordBatch.computeBodyLength(ArrowRecordBatch.java:222)
    org.apache.arrow.vector.ipc.message.MessageSerializer.serialize(MessageSerializer.java:240)
    org.apache.arrow.vector.ipc.message.MessageSerializer.serialize(MessageSerializer.java:226)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.$anonfun$next(ArrowConverters.scala:118)
    scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
    org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1377)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.next(ArrowConverters.scala:121)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.next(ArrowConverters.scala:97)
    scala.collection.Iterator.foreach(Iterator.scala:941)
    scala.collection.Iterator.foreach$(Iterator.scala:941)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.foreach(ArrowConverters.scala:97)
    scala.collection.generic.Growable.$plus$plus$eq(Growable.scala:62)
    scala.collection.generic.Growable.$plus$plus$eq$(Growable.scala:53)
    scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:105)
    scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:49)
    scala.collection.TraversableOnce.to(TraversableOnce.scala:315)
    scala.collection.TraversableOnce.to$(TraversableOnce.scala:313)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.to(ArrowConverters.scala:97)
    scala.collection.TraversableOnce.toBuffer(TraversableOnce.scala:307)
    scala.collection.TraversableOnce.toBuffer$(TraversableOnce.scala:307)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.toBuffer(ArrowConverters.scala:97)
    scala.collection.TraversableOnce.toArray(TraversableOnce.scala:294)
    scala.collection.TraversableOnce.toArray$(TraversableOnce.scala:288)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.toArray(ArrowConverters.scala:97)
    org.apache.spark.sql.Dataset.$anonfun$collectAsArrowToPython(Dataset.scala:3562)
    org.apache.spark.SparkContext.$anonfun$runJob(SparkContext.scala:2193)
    org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:90)
    org.apache.spark.scheduler.Task.run(Task.scala:127)
    org.apache.spark.executor.Executor$TaskRunner.$anonfun$run(Executor.scala:446)
    org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1377)
    org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:449)
    java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.base/java.lang.Thread.run(Unknown Source)
    at org.apache.spark.TaskContextImpl.invokeListeners(TaskContextImpl.scala:145)
    at org.apache.spark.TaskContextImpl.markTaskCompleted(TaskContextImpl.scala:124)
    at org.apache.spark.scheduler.Task.run(Task.scala:137)
    at org.apache.spark.executor.Executor$TaskRunner.$anonfun$run(Executor.scala:446)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1377)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:449)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.base/java.lang.Thread.run(Unknown Source)

Driver stacktrace:
    at org.apache.spark.scheduler.DAGScheduler.failJobAndIndependentStages(DAGScheduler.scala:2059)
    at org.apache.spark.scheduler.DAGScheduler.$anonfun$abortStage(DAGScheduler.scala:2008)
    at org.apache.spark.scheduler.DAGScheduler.$anonfun$abortStage$adapted(DAGScheduler.scala:2007)
    at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)
    at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)
    at org.apache.spark.scheduler.DAGScheduler.abortStage(DAGScheduler.scala:2007)
    at org.apache.spark.scheduler.DAGScheduler.$anonfun$handleTaskSetFailed(DAGScheduler.scala:973)
    at org.apache.spark.scheduler.DAGScheduler.$anonfun$handleTaskSetFailed$adapted(DAGScheduler.scala:973)
    at scala.Option.foreach(Option.scala:407)
    at org.apache.spark.scheduler.DAGScheduler.handleTaskSetFailed(DAGScheduler.scala:973)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.doOnReceive(DAGScheduler.scala:2239)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:2188)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:2177)
    at org.apache.spark.util.EventLoop$$anon.run(EventLoop.scala:49)
    at org.apache.spark.scheduler.DAGScheduler.runJob(DAGScheduler.scala:775)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:2099)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:2194)
    at org.apache.spark.sql.Dataset.$anonfun$collectAsArrowToPython(Dataset.scala:3560)
    at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1377)
    at org.apache.spark.sql.Dataset.$anonfun$collectAsArrowToPython(Dataset.scala:3564)
    at org.apache.spark.sql.Dataset.$anonfun$collectAsArrowToPython$adapted(Dataset.scala:3541)
    at org.apache.spark.sql.Dataset.$anonfun$withAction(Dataset.scala:3618)
    at org.apache.spark.sql.execution.SQLExecution$.$anonfun$withNewExecutionId(SQLExecution.scala:100)
    at org.apache.spark.sql.execution.SQLExecution$.withSQLConfPropagated(SQLExecution.scala:160)
    at org.apache.spark.sql.execution.SQLExecution$.$anonfun$withNewExecutionId(SQLExecution.scala:87)
    at org.apache.spark.sql.SparkSession.withActive(SparkSession.scala:764)
    at org.apache.spark.sql.execution.SQLExecution$.withNewExecutionId(SQLExecution.scala:64)
    at org.apache.spark.sql.Dataset.withAction(Dataset.scala:3616)
    at org.apache.spark.sql.Dataset.$anonfun$collectAsArrowToPython(Dataset.scala:3541)
    at org.apache.spark.sql.Dataset.$anonfun$collectAsArrowToPython$adapted(Dataset.scala:3540)
    at org.apache.spark.security.SocketAuthServer$.$anonfun$serveToStream(SocketAuthServer.scala:130)
    at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1377)
    at org.apache.spark.security.SocketAuthServer$.$anonfun$serveToStream(SocketAuthServer.scala:132)
    at org.apache.spark.security.SocketAuthServer$.$anonfun$serveToStream$adapted(SocketAuthServer.scala:127)
    at org.apache.spark.security.SocketFuncServer.handleConnection(SocketAuthServer.scala:104)
    at org.apache.spark.security.SocketFuncServer.handleConnection(SocketAuthServer.scala:98)
    at org.apache.spark.security.SocketAuthServer$$anon.$anonfun$run(SocketAuthServer.scala:60)
    at scala.util.Try$.apply(Try.scala:213)
    at org.apache.spark.security.SocketAuthServer$$anon.run(SocketAuthServer.scala:60)
Caused by: org.apache.spark.util.TaskCompletionListenerException: Memory was leaked by query. Memory leaked: (671744)
Allocator(toBatchIterator) 0/671744/671744/9223372036854775807 (res/actual/peak/limit)


Previous exception in task: sun.misc.Unsafe or java.nio.DirectByteBuffer.<init>(long, int) not available
    io.netty.util.internal.PlatformDependent.directBuffer(PlatformDependent.java:490)
    io.netty.buffer.NettyArrowBuf.getDirectBuffer(NettyArrowBuf.java:243)
    io.netty.buffer.NettyArrowBuf.nioBuffer(NettyArrowBuf.java:233)
    io.netty.buffer.ArrowBuf.nioBuffer(ArrowBuf.java:245)
    org.apache.arrow.vector.ipc.message.ArrowRecordBatch.computeBodyLength(ArrowRecordBatch.java:222)
    org.apache.arrow.vector.ipc.message.MessageSerializer.serialize(MessageSerializer.java:240)
    org.apache.arrow.vector.ipc.message.MessageSerializer.serialize(MessageSerializer.java:226)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.$anonfun$next(ArrowConverters.scala:118)
    scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
    org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1377)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.next(ArrowConverters.scala:121)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.next(ArrowConverters.scala:97)
    scala.collection.Iterator.foreach(Iterator.scala:941)
    scala.collection.Iterator.foreach$(Iterator.scala:941)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.foreach(ArrowConverters.scala:97)
    scala.collection.generic.Growable.$plus$plus$eq(Growable.scala:62)
    scala.collection.generic.Growable.$plus$plus$eq$(Growable.scala:53)
    scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:105)
    scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:49)
    scala.collection.TraversableOnce.to(TraversableOnce.scala:315)
    scala.collection.TraversableOnce.to$(TraversableOnce.scala:313)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.to(ArrowConverters.scala:97)
    scala.collection.TraversableOnce.toBuffer(TraversableOnce.scala:307)
    scala.collection.TraversableOnce.toBuffer$(TraversableOnce.scala:307)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.toBuffer(ArrowConverters.scala:97)
    scala.collection.TraversableOnce.toArray(TraversableOnce.scala:294)
    scala.collection.TraversableOnce.toArray$(TraversableOnce.scala:288)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.toArray(ArrowConverters.scala:97)
    org.apache.spark.sql.Dataset.$anonfun$collectAsArrowToPython(Dataset.scala:3562)
    org.apache.spark.SparkContext.$anonfun$runJob(SparkContext.scala:2193)
    org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:90)
    org.apache.spark.scheduler.Task.run(Task.scala:127)
    org.apache.spark.executor.Executor$TaskRunner.$anonfun$run(Executor.scala:446)
    org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1377)
    org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:449)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)
    at org.apache.spark.TaskContextImpl.invokeListeners(TaskContextImpl.scala:145)
    at org.apache.spark.TaskContextImpl.markTaskCompleted(TaskContextImpl.scala:124)
    at org.apache.spark.scheduler.Task.run(Task.scala:137)
    at org.apache.spark.executor.Executor$TaskRunner.$anonfun$run(Executor.scala:446)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1377)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:449)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

  warnings.warn(msg)
---------------------------------------------------------------------------
Py4JJavaError                             Traceback (most recent call last)
<ipython-input-127-51c79da6d64b> in <module>
      4 
      5 #for sensor in sensors:
----> 6 plot_summaries("Water_Level_Sensor_stddev", "pivoted")

<ipython-input-124-70e98889279f> in plot_summaries(sensor, dfName)
      5     df = sqlContext.table(dfName)
      6 
----> 7     pdf = df.toPandas()
      8         #.select("*") \
      9         #.orderBy("index") \

/spark/python/lib/pyspark.zip/pyspark/sql/pandas/conversion.py in toPandas(self)
    106                     # Rename columns to avoid duplicated column names.
    107                     tmp_column_names = ['col_{}'.format(i) for i in range(len(self.columns))]
--> 108                     batches = self.toDF(*tmp_column_names)._collect_as_arrow()
    109                     if len(batches) > 0:
    110                         table = pyarrow.Table.from_batches(batches)

/spark/python/lib/pyspark.zip/pyspark/sql/pandas/conversion.py in _collect_as_arrow(self)
    242         finally:
    243             # Join serving thread and raise any exceptions from collectAsArrowToPython
--> 244             jsocket_auth_server.getResult()
    245 
    246         # Separate RecordBatches from batch order indices in results

/spark/python/lib/py4j-0.10.9-src.zip/py4j/java_gateway.py in __call__(self, *args)
   1303         answer = self.gateway_client.send_command(command)
   1304         return_value = get_return_value(
-> 1305             answer, self.gateway_client, self.target_id, self.name)
   1306 
   1307         for temp_arg in temp_args:

/spark/python/lib/pyspark.zip/pyspark/sql/utils.py in deco(*a, **kw)
    126     def deco(*a, **kw):
    127         try:
--> 128             return f(*a, **kw)
    129         except py4j.protocol.Py4JJavaError as e:
    130             converted = convert_exception(e.java_exception)

/spark/python/lib/py4j-0.10.9-src.zip/py4j/protocol.py in get_return_value(answer, gateway_client, target_id, name)
    326                 raise Py4JJavaError(
    327                     "An error occurred while calling {0}{1}{2}.\n".
--> 328                     format(target_id, ".", name), value)
    329             else:
    330                 raise Py4JError(

Py4JJavaError: An error occurred while calling o117.getResult.
: org.apache.spark.SparkException: Exception thrown in awaitResult: 
    at org.apache.spark.util.ThreadUtils$.awaitResult(ThreadUtils.scala:302)
    at org.apache.spark.security.SocketAuthServer.getResult(SocketAuthServer.scala:88)
    at org.apache.spark.security.SocketAuthServer.getResult(SocketAuthServer.scala:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:282)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 1157.0 failed 4 times, most recent failure: Lost task 0.3 in stage 1157.0 (TID 81153, 10.244.1.26, executor 12): org.apache.spark.util.TaskCompletionListenerException: Memory was leaked by query. Memory leaked: (671744)
Allocator(toBatchIterator) 0/671744/671744/9223372036854775807 (res/actual/peak/limit)


Previous exception in task: sun.misc.Unsafe or java.nio.DirectByteBuffer.<init>(long, int) not available
    io.netty.util.internal.PlatformDependent.directBuffer(PlatformDependent.java:490)
    io.netty.buffer.NettyArrowBuf.getDirectBuffer(NettyArrowBuf.java:243)
    io.netty.buffer.NettyArrowBuf.nioBuffer(NettyArrowBuf.java:233)
    io.netty.buffer.ArrowBuf.nioBuffer(ArrowBuf.java:245)
    org.apache.arrow.vector.ipc.message.ArrowRecordBatch.computeBodyLength(ArrowRecordBatch.java:222)
    org.apache.arrow.vector.ipc.message.MessageSerializer.serialize(MessageSerializer.java:240)
    org.apache.arrow.vector.ipc.message.MessageSerializer.serialize(MessageSerializer.java:226)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.$anonfun$next(ArrowConverters.scala:118)
    scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
    org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1377)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.next(ArrowConverters.scala:121)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.next(ArrowConverters.scala:97)
    scala.collection.Iterator.foreach(Iterator.scala:941)
    scala.collection.Iterator.foreach$(Iterator.scala:941)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.foreach(ArrowConverters.scala:97)
    scala.collection.generic.Growable.$plus$plus$eq(Growable.scala:62)
    scala.collection.generic.Growable.$plus$plus$eq$(Growable.scala:53)
    scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:105)
    scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:49)
    scala.collection.TraversableOnce.to(TraversableOnce.scala:315)
    scala.collection.TraversableOnce.to$(TraversableOnce.scala:313)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.to(ArrowConverters.scala:97)
    scala.collection.TraversableOnce.toBuffer(TraversableOnce.scala:307)
    scala.collection.TraversableOnce.toBuffer$(TraversableOnce.scala:307)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.toBuffer(ArrowConverters.scala:97)
    scala.collection.TraversableOnce.toArray(TraversableOnce.scala:294)
    scala.collection.TraversableOnce.toArray$(TraversableOnce.scala:288)
    org.apache.spark.sql.execution.arrow.ArrowConverters$$anon.toArray(ArrowConverters.scala:97)
    org.apache.spark.sql.Dataset.$anonfun$collectAsArrowToPython(Dataset.scala:3562)
    org.apache.spark.SparkContext.$anonfun$runJob(SparkContext.scala:2193)
    org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:90)
    org.apache.spark.scheduler.Task.run(Task.scala:127)
    org.apache.spark.executor.Executor$TaskRunner.$anonfun$run(Executor.scala:446)
    org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1377)
    org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:449)
    java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.base/java.lang.Thread.run(Unknown Source)
    at org.apache.spark.TaskContextImpl.invokeListeners(TaskContextImpl.scala:145)
    at org.apache.spark.TaskContextImpl.markTaskCompleted(TaskContextImpl.scala:124)
    at org.apache.spark.scheduler.Task.run(Task.scala:137)
    at org.apache.spark.executor.Executor$TaskRunner.$anonfun$run(Executor.scala:446)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1377)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:449)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.base/java.lang.Thread.run(Unknown Source)

Driver stacktrace:
    at org.apache.spark.scheduler.DAGScheduler.failJobAndIndependentStages(DAGScheduler.scala:2059)
    at org.apache.spark.scheduler.DAGScheduler.$anonfun$abortStage(DAGScheduler.scala:2008)
    at org.apache.spark.scheduler.DAGScheduler.$anonfun$abortStage$adapted(DAGScheduler.scala:2007)
    at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)
    at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)
    at org.apache.spark.scheduler.DAGScheduler.abortStage(DAGScheduler.scala:2007)
    at org.apache.spark.scheduler.DAGScheduler.$anonfun$handleTaskSetFailed(DAGScheduler.scala:973)
    at org.apache.spark.scheduler.DAGScheduler.$anonfun$handleTaskSetFailed$adapted(DAGScheduler.scala:973)
    at scala.Option.foreach(Option.scala:407)
    at org.apache.spark.scheduler.DAGScheduler.handleTaskSetFailed(DAGScheduler.scala:973)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.doOnReceive(DAGScheduler.scala:2239)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:2188)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:2177)
    at org.apache.spark.util.EventLoop$$anon.run(EventLoop.scala:49)
    at org.apache.spark.scheduler.DAGScheduler.runJob(DAGScheduler.scala:775)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:2099)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:2194)
    at org.apache.spark.sql.Dataset.$anonfun$collectAsArrowToPython(Dataset.scala:3560)
    at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1377)
    at org.apache.spark.sql.Dataset.$anonfun$collectAsArrowToPython(Dataset.scala:3564)
    at org.apache.spark.sql.Dataset.$anonfun$collectAsArrowToPython$adapted(Dataset.scala:3541)
    at org.apache.spark.sql.Dataset.$anonfun$withAction(Dataset.scala:3618)
    at org.apache.spark.sql.execution.SQLExecution$.$anonfun$withNewExecutionId(SQLExecution.scala:100)
    at org.apache.spark.sql.execution.SQLExecution$.withSQLConfPropagated(SQLExecution.scala:160)
    at org.apache.spark.sql.execution.SQLExecution$.$anonfun$withNewExecutionId(SQLExecution.scala:87)
    at org.apache.spark.sql.SparkSession.withActive(SparkSession.scala:764)
    at org.apache.spark.sql.execution.SQLExecution$.withNewExecutionId(SQLExecution.scala:64)
    at org.apache.spark.sql.Dataset.withAction(Dataset.scala:3616)
    at org.apache.spark.sql.Dataset.$anonfun$collectAsArrowToPython(Dataset.scala:3541)
    at org.apache.spark.sql.Dataset.$anonfun$collectAsArrowToPython$adapted(Dataset.scala:3540)
    at org.apache.spark.security.SocketAuthServer$.$anonfun$serveToStream(SocketAuthServer.scala:130)
    at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1377)
    at org.apache.spark.security.SocketAuthServer$.$anonfun$serveToStream(SocketAuthServer.scala:132)
    at org.apache.spark.security.SocketAuthServer$.$anonfun$serveToStream$adapted(SocketAuthServer.scala:127)
    at org.apache.spark.security.SocketFuncServer.handleConnection(SocketAuthServer.scala:104)
    at org.apache.spark.security.SocketFuncServer.handleConnection(SocketAuthServer.scala:98)
    at org.apache.spark.security.SocketAuthServer$$anon.$anonfun$run(SocketAuthServer.scala:60)
    at scala.util.Try$.apply(Try.scala:213)
    at org.apache.spark.security.SocketAuthServer$$anon.run(SocketAuthServer.scala:60)
Caused by: org.apache.spark.util.TaskCompletionListenerException: Memory was leaked by query. Memory leaked: (671744)
Allocator(toBatchIterator) 0/671744/671744/9223372036854775807 (res/actual/peak/limit)

如何避免?

不过重启Spark解释器后问题终于解决了