Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Databricks · Databricks Spark Developer Associate

DataFrames, the SparkSession, Caching, and GC

Databricks Spark Developer Associate · Lesson 3 of 18 · 14:32

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the Databricks Certified Associate Developer for Apache Spark exam. In this lesson: DataFrames and Datasets in PySpark, the SparkSession, caching and storage levels (cache, persist, and unpersist), and garbage collection. Includes three original practice questions.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

The same table, five times

Coralpeak's analysts run five different reports on the same cleaned table of yesterday's events, and each report reads and cleans the raw files again from scratch. Some jobs also stall for seconds at a time, with almost no work done. This lesson covers DataFrames and Datasets, the SparkSession, caching with storage levels, and garbage collection.

DataFrames and Datasets

A Dataset is a distributed collection of typed objects, and it exists in Scala and Java, where the compiler checks the types. A DataFrame is a Dataset organized into named columns, like a table, and in Scala it is simply a Dataset of rows. Python has no typed Dataset, so in PySpark you always work with DataFrames, which are immutable and distributed: each transformation returns a new DataFrame. And the Catalyst optimizer of Spark SQL plans DataFrame code and SQL queries the same way, so both run equally fast.

The SparkSession

The SparkSession is the single entry point to Spark: you read data, run SQL, and create DataFrames through it. You build one with the SparkSession builder: set an app name and some options, then call getOrCreate, which returns the existing session if there is one, or creates a new one. In a Databricks notebook, a session named spark already exists, so you use it directly. And calling stop on the session ends it, with its underlying context, and releases the resources of the application.

Caching and storage levels

When several actions reuse the same DataFrame, Spark computes it again from the source each time, unless you persist it. cache keeps the DataFrame at the default storage level, which for DataFrames is memory and disk, deserialized: partitions that do not fit in memory go to disk. Caching is lazy: the data is stored by the first action that computes it, and later actions read it from the cache. persist takes a storage level when you need another one: memory only, disk only, memory and disk, or a replicated version that keeps two copies on two executors. When memory runs short, Spark evicts the least recently used partitions first, and unpersist frees a DataFrame that you no longer need.

Garbage collection

Executors run in a Java virtual machine, and the garbage collector frees the memory of objects that are no longer used. The heap has a young generation, for short lived objects, and an old generation, for long lived ones, and a full collection runs when the old generation is nearly full, which pauses the tasks. Its cost grows with the number of objects, so many small objects, or a large cache that leaves little room, mean long pauses that show up as GC time in the Spark UI. To reduce it, cache less or in serialized form, create fewer objects, for example with built in functions instead of UDFs, or use more partitions so that each task holds less data. And recent Spark versions use the G1 collector by default.

Recap

Let's recap. Datasets are typed, in Scala and Java, while PySpark uses DataFrames: immutable, distributed, with named columns. The SparkSession is the entry point, built with getOrCreate, and already named spark in Databricks notebooks. cache uses memory and disk for DataFrames, and it is lazy, filled by the first action. persist picks another storage level, Spark evicts the least recently used partitions, and unpersist frees memory. And garbage collection pauses grow with the number of objects, so cache less, create fewer objects, and spread the data over more partitions.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. A developer reads that Datasets check types at compile time, and wants them in a PySpark job. What should they know?

    • Datasets run faster than DataFrames in Python
    • PySpark turns every DataFrame into a typed Dataset
    • Datasets need the pandas API on Spark
    • The typed Dataset API exists in Scala and Java only
    Show the answer

    The typed Dataset API exists in Scala and Java only

    The typed Dataset API exists in Scala and Java, while Python works with DataFrames. “Datasets run faster than DataFrames in Python” is wrong, since Python has no typed Dataset to compare with. “PySpark turns every DataFrame into a typed Dataset” is wrong, since PySpark keeps DataFrames as they are, with no conversion to a typed Dataset. And “Datasets need the pandas API on Spark” is wrong, since the pandas API on Spark is a different API, with no typed Datasets either.

  2. Coralpeak calls cache() on the cleaned events, but the Storage tab of the Spark UI stays empty. Why?

    • cache() works on tables, not on DataFrames
    • No action has run yet, so nothing was computed
    • The default level keeps the data on disk only
    • The cluster has no free memory at all
    Show the answer

    No action has run yet, so nothing was computed

    Caching is lazy, so the data is stored only when the first action computes the DataFrame. “cache() works on tables, not on DataFrames” is wrong, since cache works on any DataFrame. “The default level keeps the data on disk only” is wrong, since the default level for DataFrames uses memory first, then disk, and the tab would show either one. And “The cluster has no free memory at all” is wrong, since without free memory Spark would use the disk, and in any case nothing has run yet.

  3. A job spends a large share of each task in GC time, and the executors hold a big cache of DataFrames that are rarely reused. What should the team try first?

    • Collect the results to the driver more often
    • Reduce the number of partitions
    • Unpersist the DataFrames that are rarely reused
    • Replace the built-in functions with Python UDFs
    Show the answer

    Unpersist the DataFrames that are rarely reused

    Freeing cached data that is rarely reused leaves more room for execution, and fewer objects for the collector. “Collect the results to the driver more often” is wrong, since collecting moves the pressure to the driver, and does not help the executors. “Reduce the number of partitions” is wrong, since fewer partitions make each task hold more data, which often makes garbage collection worse. And “Replace the built-in functions with Python UDFs” is wrong, since Python UDFs add serialization, while built in functions are lighter.