Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete an RDD in PySpark for the purpose of releasing resources?

Tags:

If I have an RDD that I no longer need, how do I delete it from memory? Would the following be enough to get this done:

del thisRDD 

Thanks!

like image 929
Ego Avatar asked Jan 16 '15 18:01

Ego


People also ask

How do I get rid of RDD in Spark?

You should call thisRDD. unpersist() to remove the cached data. Actually here, you won't have any data cached, it would be only marked as 'to be cached' in the RDD execution plan. You can easily check the persisted data and the level of persistence in the Spark UI using the address http://<driver_node>:4040/storage.

How can one manually remove an RDD instead of waiting for it to fall out of the cache?

If you would like to manually remove an RDD instead of waiting for it to fall out of the cache, use the RDD. unpersist() method. And pay attention to unpersist the df after the end of the lineage, so after the last action that involves the cached df.

Can we delete RDD?

No, del thisRDD is not enough, it would just delete the pointer to the RDD. You should call thisRDD. unpersist() to remove the cached data.


2 Answers

No, del thisRDD is not enough, it would just delete the pointer to the RDD. You should call thisRDD.unpersist() to remove the cached data.

For you information, Spark uses a model of lazy computations, which means that when you run this code:

>>> thisRDD = sc.parallelize(xrange(10),2).cache() 

you won't have any data cached really, it would be only marked as 'to be cached' in the RDD execution plan. You can check it this way:

>>> print thisRDD.toDebugString() (2) PythonRDD[6] at RDD at PythonRDD.scala:43 [Memory Serialized 1x Replicated]  |  ParallelCollectionRDD[5] at parallelize at PythonRDD.scala:364 [Memory Serialized 1x Replicated] 

But when you call an action on top of this RDD at least once, it would become cached:

>>> thisRDD.count() 10 >>> print thisRDD.toDebugString() (2) PythonRDD[6] at RDD at PythonRDD.scala:43 [Memory Serialized 1x Replicated]  |       CachedPartitions: 2; MemorySize: 174.0 B; TachyonSize: 0.0 B; DiskSize: 0.0 B  |  ParallelCollectionRDD[5] at parallelize at PythonRDD.scala:364 [Memory Serialized 1x Replicated] 

You can easily check the persisted data and the level of persistence in the Spark UI using the address http://<driver_node>:4040/storage. You would see there that del thisRDD won't change the persistence of this RDD, but thisRDD.unpersist() would unpersist it, while you still would be able to use thisRDD in your code (while it won't persist in memory anymore and would be recomputed each time it is queried)

like image 168
0x0FFF Avatar answered Sep 27 '22 20:09

0x0FFF


Short answer: The following code should do the trick:

import gc del thisRDD gc.collect() 

Explanation:

Even if you are using PySpark, your RDD's data is managed on the Java side, so first let's ask the same question, but for Java instead of Python:

If I'm using Java, and I simply release all references to my RDD, is that sufficient to automatically unpersist it?

For Java, the answer is YES, the RDD will be automatically unpersisted when it is garbage collected, according to this answer. (Apparently that functionality was added to Spark in this PR.)

OK, what happens in Python? If I remove all references to my RDD in Python, does that cause them to be removed on the Java side?

PySpark uses Py4J to send objects from Python to Java and vice-versa. According to the Py4J Memory Model Docs:

Once the object is garbage collected on the Python VM (reference count == 0), the reference is removed on the Java VM

But take note: Removing the Python references to your RDD won't cause it to be immediately deleted. You have to wait for the Python garbage collector to clean up the references. You can read the Py4J explanation for details, where they recommend the following:

A call to gc.collect() also usually works.

OK, now back to your original question:

Would the following be enough to get this done:

del thisRDD 

Almost. You should remove the last reference to it (i.e. del thisRDD), and then, if you really need the RDD to be unpersisted immediately**, call gc.collect().

**Well, technically, this will immediately delete the reference on the Java side, but there will be a slight delay until Java's garbage collector actually executes the RDD's finalizer and thereby unpersists the data.

like image 42
Stuart Berg Avatar answered Sep 27 '22 20:09

Stuart Berg