Would Spark unpersist the RDD itself when it realizes it won't be used anymore?

后端 未结 2 1861
粉色の甜心
粉色の甜心 2020-12-05 07:02

We can persist an RDD into memory and/or disk when we want to use it more than once. However, do we have to unpersist it ourselves later on, or does Spark does some kind of

相关标签:
2条回答
  • 2020-12-05 07:18

    Yes, Apache Spark will unpersist the RDD when it's garbage collected.

    In RDD.persist you can see:

    sc.cleaner.foreach(_.registerRDDForCleanup(this))
    

    This puts a WeakReference to the RDD in a ReferenceQueue leading to ContextCleaner.doCleanupRDD when the RDD is garbage collected. And there:

    sc.unpersistRDD(rddId, blocking)
    

    For more context see ContextCleaner in general and the commit that added it.

    A few things to be aware of when relying on garbage collection for unperisting RDDs:

    • The RDDs use resources on the executors, and the garbage collection happens on the driver. The RDD will not be automatically unpersisted until there is enough memory pressure on the driver, no matter how full the disk/memory of the executors gets.
    • You cannot unpersist part of an RDD (some partitions/records). If you build one persisted RDD from another, both will have to fit entirely on the executors at the same time.
    0 讨论(0)
  • 2020-12-05 07:25

    As pointed out by @Daniel, Spark will remove partitions from the cache. This will happen once there is no more memory available, and will be done using a least-recently-used algorithm. It is not a smart system, as pointed out by @eliasah.

    If you are not caching too many objects you don't have to worry about it. If you cache too many objects, the JVM collection times will become excessive, so it is a good idea to unpersist them in this case.

    0 讨论(0)
提交回复
热议问题