Is Spark's JVM overhead a dealbreaker versus native engines?
Summary
- JVM overhead in Apache Spark is real and documented: Java objects carry headers, garbage collection cost grows with the number of objects, and Python code exchanges data with the JVM. Whether it matters depends on your workload.
- Spark's engine was reworked around these costs: Project Tungsten manages memory explicitly in binary form, whole-stage code generation compiles chains of operators into a single Java function, and Parquet and ORC decoding is vectorized by default.
- Apache Gluten (with the Velox C++ library) and Apache DataFusion Comet are open-source plugins that run supported operators natively under Spark's APIs and fall back to Spark for the rest.
- Photon is the Databricks-native engine: for supported operations it replaces the JVM-based Spark SQL execution engine with a native C++ runtime.
- Measure how much of each plan runs natively and what falls back. This page makes no performance claims.
Is Spark's JVM overhead a dealbreaker versus native engines?
For SQL and DataFrame workloads, the choice isn't Spark or native: you can keep Spark's APIs and still run supported operators natively. The JVM adds documented costs, from object headers and garbage collection to serialization at the Python boundary, and Spark's engine addresses many of them with binary memory management, runtime code generation, vectorized readers and Apache Arrow. Open-source plugins and Databricks' Photon take another route, running supported operators in C++ or Rust and handing everything else back to Spark. The useful question is which parts of your workload stay on the JVM, measured on your own jobs.
Where JVM overhead comes from in Spark
Spark's tuning guide names them:
- Object overhead. Each distinct Java object carries an object header, and strings and linked collections such as
HashMapadd pointers and wrapper objects on top of the raw data. - Garbage collection. "The cost of garbage collection is proportional to the number of Java objects."
- Serialization. The default
spark.serializeris Java serialization. The guide recommends Kryo, which doesn't support allSerializabletypes and works best with classes registered in advance. - The Python boundary. Python UDFs and pandas conversions move data between JVM and Python processes.
What Spark's engine already does about it
- Project Tungsten. Announced in 2015, it set out to manage memory explicitly in binary form (building on
sun.misc.Unsafe), use cache-aware algorithms, and generate code at runtime. - Whole-stage code generation. Spark compiles chains of operators into a single Java function. In a physical plan, operators under whole-stage codegen are prefixed with a star and a code generation id, such as
*(1). - Vectorized, columnar reads.
spark.sql.parquet.enableVectorizedReaderandspark.sql.orc.enableVectorizedReaderboth default totrue, and cached tables use an in-memory columnar format. - Off-heap memory.
spark.memory.offHeap.enabled(defaultfalse) plus a positivespark.memory.offHeap.sizelets Spark use off-heap memory for certain operations. - Arrow for Python. Spark uses Apache Arrow to transfer data between JVM and Python processes, and since Spark 4.2, Arrow optimization is on by default for regular Python UDFs (
spark.sql.execution.pythonUDF.arrow.enabled).
To see what's compiled, inspect the plan:
df = spark.range(10).selectExpr("id + 1 AS next_id")
df.explain(mode="formatted") # starred operators run in whole-stage codegen
df.explain(mode="codegen") # physical plan plus the generated code
Apache Spark vs native engines: how they compare
| Aspect | Apache Spark (JVM) | Gluten with Velox | DataFusion Comet | Photon (Databricks) |
|---|---|---|---|---|
| Runtime | The JVM; whole-stage codegen produces Java functions | Velox, an open-source C++ execution library | Apache DataFusion, a Rust query engine that uses Apache Arrow | A native C++ runtime that processes data in columnar batches |
| How it plugs in | It is the engine | A Spark plugin that converts Spark physical plans to Substrait, then to native plans | A Spark plugin that translates supported operators for DataFusion and returns Arrow batches | Built into Databricks compute; on for serverless compute, SQL warehouses and serverless Lakeflow pipelines |
| Code changes | Not applicable | Configuration: the plugin, off-heap memory and a columnar shuffle manager | "No code changes required" | None for code that uses Spark APIs |
| Unsupported work | Not applicable | Falls back to vanilla Spark, with ColumnarToRow and RowToColumnar converters | "Falls back to the Spark engine" | Falls back to the Spark runtime; UDFs, RDD and Dataset APIs, and stateful streaming aren't supported |
| License and availability | Apache 2.0, Apache Software Foundation | Apache top-level project since March 2026; Velox is Apache 2.0 | Apache Software Foundation project, Apache 2.0 | Databricks-native engine |
When the JVM matters for your workload
- Which APIs you use. Native engines take over supported SQL and DataFrame operators. Photon's docs list UDFs, RDD APIs and Dataset APIs as unsupported.
- Fallback points. Gluten, Comet and Photon all fall back to Spark for unsupported operators. Read your real query plans to see where native and JVM stages meet.
- Memory sizing. Both plugins' setup guides set
spark.memory.offHeap.enabled=trueand aspark.memory.offHeap.size. Size that alongside the JVM heap, because off-heap allocation doesn't reduce heap usage. - Scale. If the data fits on one machine, an in-process engine such as DuckDB, which runs "completely embedded within a host process," or Polars, whose core is written in Rust, needs no cluster, which is a separate decision from swapping Spark's execution layer.
How Databricks approaches native execution
Photon is "the Databricks-native vectorized query engine," compatible with Apache Spark APIs. For supported operations it replaces the JVM-based Spark SQL execution engine with a native C++ runtime, which the docs say removes JVM garbage collection pauses and JIT warm-up from those operations. When it meets an unsupported operation, it "transparently falls back to the Spark runtime for the remainder of that operation."
FAQs
Does Spark 4 still run on the JVM?
Yes. Spark 4.2 runs on Java 17, 21 or 25 with Scala 2.13, and since Spark 4.0.0 it uses JDK 17 and the G1GC collector by default.
Does PySpark avoid the JVM?
No. PySpark drives Spark's JVM engine. Python workers run Python UDFs, and Arrow handles data transfer between the JVM and Python processes.
Is Photon open source?
Photon is the Databricks-native engine. Open-source native options for Apache Spark include Apache Gluten with Velox and Apache DataFusion Comet.
The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.