Why The World Needs Flarion. Read More

The Infamous Spark Out Of Memory Error

Why does it happen? How to avoid it?
By
Ran Reichman
read time
December 24, 2024

Apache Spark is widely used for processing massive datasets, but Out of Memory (OOM) errors are a frequent challenge that affects even the most experienced teams. These errors consistently disrupt production workflows and can be particularly frustrating because they often appear suddenly when scaling up previously working jobs. Below we'll explore what causes these issues and how to handle them effectively.

Causes of OOM and How to Mitigate Them

Resource-Data Volume Mismatch

The primary driver of OOM errors in Spark applications is the fundamental relationship between data volume and allocated executor memory. As datasets grow, they frequently exceed the memory capacity of individual executors, particularly during operations that must materialize significant portions of the data in memory. This occurs because:

  • Data volumes typically grow exponentially while memory allocations are adjusted linearly
  • Operations like joins and aggregations can create intermediate results that are orders of magnitude larger than the input data
  • Memory requirements multiply during complex transformations with multiple stages
  • Executors need substantial headroom for both data processing and computational overhead

Mitigations:

  • Monitor memory usage patterns across job runs to identify growth trends and establish predictive scaling
  • Implement data partitioning strategies to process data in manageable chunks
  • Use appropriate executor sizing via the instruction --executor-memory 8g
  • Enable dynamic allocation with spark.dynamicAllocation.enabled=true, automatically adjusting the number of executors based on workload

JVM Memory Management

Spark runs on the JVM, which brings several memory management challenges:

  • Garbage collection pauses can lead to memory spikes
  • Memory fragmentation reduces effective available memory
  • JVM overhead requires additional memory allocation beyond your data needs
  • Complex management between off-heap and on-heap memory

Mitigations:

  • Consider native alternatives for memory-intensive operations. Spark operations implemented in C++ or Rust can provide the same results with less resource usage compared to JVM code.
  • Enable off-heap memory with spark.memory.offHeap.enabled=true, allowing Spark to use memory outside the JVM heap and reducing garbage collection overhead
  • Optimize garbage collection with -XX:+UseG1GC, enabling the Garbage-First Garbage Collector, which handles large heaps more efficiently

Configuration Mismatch

The default Spark configurations are rarely suitable for production workloads:

  • Default executor memory settings assume small-to-medium datasets
  • Memory fractions aren't optimized for specific workload patterns
  • Shuffle settings often need adjustment for real-world data distributions

Mitigations:

  • Monitor executor memory metrics to identify optimal settings
  • Set the more efficient Kyro Serializer with  spark.serializer=org.apache.spark.serializer.KryoSerializer

Data Skew and Scaling Issues

Memory usage often scales non-linearly with data size due to:

  • Uneven key distributions causing certain executors to process disproportionate amounts of data
  • Shuffle operations requiring significant temporary storage
  • Join operations potentially creating large intermediate results

Mitigations:

  • Monitor partition sizes and executor memory distribution
  • Implement key salting for skewed joins
  • Use broadcast joins for small tables
  • Repartition data based on key distribution
  • Break down wide transformations into smaller steps
  • Leverage structured streaming for very large datasets

Conclusion

Out of Memory errors are an inherent challenge when using Spark, primarily due to its JVM-based architecture and the complexity of distributed computing. The risk of OOM can be significantly reduced through careful management of data and executor sizing, leveraging native processing solutions where appropriate, and implementing comprehensive memory monitoring to detect usage patterns before they become critical issues.

Related Posts

Apache Spark 4.0 marks a significant milestone in the framework's evolution toward columnar processing. With enhanced Apache Arrow integration, improved UDF support, and refined plugin architectures, Spark has taken meaningful steps forward. Yet understanding both the advances and the remaining gaps reveals why the journey toward truly efficient columnar processing continues.

The Promise and Reality of Columnar Improvements

Spark 4.0's headline improvements center on Apache Arrow integration. The framework now offers direct DataFrame-to-Arrow conversions, Arrow-optimized Python UDFs achieving up to 1.9x performance improvements, and cleaner APIs for columnar data exchange. These changes particularly benefit PySpark users, who've long suffered from serialization overhead when moving data between JVM and Python processes.

Yet examining Spark's architecture reveals a fundamental reality: the core execution engine remains predominantly row-oriented. While Spark provides hooks for columnar execution through its plugin architecture, the built-in operators - projections, filters, joins, aggregations - still process data row by row through optimized Java code generation. This isn't an oversight but a deliberate design choice that prioritizes compatibility and stability over pure performance.

Where Columnar Support Actually Stands

Understanding Spark 4.0's columnar capabilities requires distinguishing between data format and execution model. Spark has long supported columnar storage formats like Parquet. However, during actual computation, most operations convert this columnar data back to rows for processing.

Built-in SQL expressions execute through Catalyst's code generation, producing tight Java loops that process one row at a time. Complex expressions involving conditionals, nested functions, or custom logic follow this row-wise pattern. The JVM's JIT compiler optimizes these loops well, but they fundamentally lack the vectorized operations that define true columnar processing.

UDF support presents a mixed picture. Pandas UDFs genuinely operate on columnar data, leveraging NumPy's vectorized operations. The new Arrow-optimized Python UDFs improve data transfer efficiency but don't change the scalar execution model - they still process individual values, just with better serialization. Scala and Java UDFs remain entirely row-based, forcing any columnar data to convert back to rows for execution.

The architectural split becomes clear when examining memory management. Spark uses its own ColumnVector implementations for internal operations, not pure Arrow format. Converting between Spark's internal format and Arrow involves either copying or wrapping data, adding overhead that pure columnar engines avoid.

The Performance Gap That Remains

The practical implications become evident in production workloads. Join operations still rely on sort-merge or hash algorithms implemented in Java without SIMD optimization. Aggregations process groups row by row rather than operating on entire column chunks. String operations, mathematical expressions, and date manipulations all follow the same pattern - optimized Java code that processes individual values rather than vectors.

Native columnar engines demonstrate what's possible with true vectorized execution. By leveraging SIMD instructions and processing entire column batches simultaneously, these engines achieve significant speedups - often 2x or more - on the same hardware. This isn't because Spark's code is poorly written; it's because columnar execution with hardware vectorization fundamentally outperforms row-wise processing for analytical workloads.

The memory efficiency gap proves equally significant. Native columnar engines process data in its compressed form, maintaining compression through operations wherever possible. Spark's row-wise operations require decompression and materialization, increasing memory pressure and triggering more frequent garbage collection. For workloads pushing memory limits - a common scenario given how frequently Spark jobs encounter OOM errors - this efficiency difference can determine whether jobs complete successfully.

The Path Forward: Complementary Solutions

Spark 4.0's columnar improvements represent genuine progress, particularly for Python workflows and data interchange scenarios. Yet the core execution engine's row-based nature means achieving optimal columnar performance requires additional components.

Organizations increasingly deploy hybrid architectures that leverage Spark's strengths - distributed orchestration, fault tolerance, broad connector support - while delegating performance-critical operations to specialized columnar engines. Whether through native code execution or hardware acceleration, these complementary technologies fill the gaps in Spark's columnar story. This is precisely where solutions like Flarion's Arrow-based columnar processing provide value - plugging directly into existing Spark deployments to accelerate workloads without requiring code changes, while maintaining the distributed capabilities teams already rely on.

Understanding both Spark 4.0's advances and its limitations enables informed architectural decisions. While Spark takes important steps toward columnar processing, the journey toward truly efficient columnar execution often requires recognizing where additional acceleration provides essential value. For teams facing today's performance challenges - growing datasets, tightening SLAs, and mounting infrastructure costs - combining Spark's orchestration capabilities with purpose-built columnar acceleration delivers the performance modern data platforms demand.

The landscape of data processing has evolved dramatically over the past few years. As datasets grow exponentially, query engines are adapting beyond traditional batch processing. Today's most innovative engines incorporate streaming capabilities to process data incrementally, enabling analysis of datasets larger than available memory while maintaining high performance. Among the leading contenders - Apache DataFusion, Polars, and DuckDB - the approaches to streaming differ significantly, with DataFusion emerging as the clear frontrunner for true streaming applications.

The Evolution of Streaming Query Execution

The term "streaming" has become somewhat ambiguous in the data processing world, spanning several distinct capabilities:

  1. Pipelined execution: Processing data in small chunks through a query plan
  2. Out-of-core processing: Handling datasets larger than available memory
  3. Continuous processing: Executing long-running queries on never-ending data streams
  4. Real-time ingestion: Continuously incorporating new data from external sources

While all three engines we're examining implement some form of streaming, they vary dramatically in their approach and capabilities. DuckDB and Polars primarily focus on the first two points—efficient execution of traditional queries—while DataFusion uniquely addresses all four aspects, providing a foundation for true streaming applications.

DataFusion's Native Streaming Architecture

Apache DataFusion, the Rust-based query engine at the heart of the Apache Arrow ecosystem, was designed with streaming as a core architectural principle. Most physical operators in DataFusion support an "Unbounded" execution mode specifically for handling infinite streams.

DataFusion's streaming architecture delivers several key advantages:

Streaming-First Design: While other engines adapted batch processing for streaming, DataFusion incorporates streaming principles natively. Its physical execution plan includes operators like StreamTableExec and SymmetricHashJoinExec specifically designed for unbounded data. This fundamental design choice enables true continuous query execution.

Streaming Join Support: Where traditional engines struggle with joins on streaming data, DataFusion's SymmetricHashJoinExec operator efficiently joins unbounded streams on the fly. This critical capability unlocks complex real-time analytics that would otherwise require batch window processing.

Arrow Integration: DataFusion processes data in Arrow record batches, providing memory-efficient, zero-copy operations on columnar data. This tight integration with Arrow gives DataFusion significant performance advantages when streaming data between systems or components.

Low-Level API Flexibility: DataFusion provides the foundational building blocks needed to construct sophisticated streaming applications. While higher-level functionality like watermarking is still emerging, its extensible architecture allows developers to implement these capabilities directly.

Polars and DuckDB: Streaming Capabilities

Both Polars and DuckDB offer capabilities related to data processing, though with important limitations for true streaming:

Polars' Streaming Status: Polars previously implemented a streaming execution mode that processed data in batches. However, it's worth noting that this streaming engine has been deprecated, and while the Polars team is working on a new streaming implementation, it's not currently something to build production systems on. Polars continues to excel at single-node workloads where memory isn't a significant constraint, offering exceptional performance for data transformation and analytics.

DuckDB Pipelined Execution: DuckDB employs a vectorized, pipelined execution model that processes data in small chunks (vectors) through query operators. This approach is particularly effective for quick in-memory operations and can handle streaming workloads efficiently when the data volumes definitively fit in memory. DuckDB's columnar architecture and parallel execution make analytical queries remarkably fast for these scenarios.

Neither engine is designed for continuous streaming of unbounded data. Both lack built-in stream ingestion capabilities and don't maintain persistent state across query executions. Each query runs to completion on the data available at execution time.

Choosing the Right Tool for Your Streaming Needs

Understanding the key differences in streaming capabilities helps select the right tool for specific use cases:

For True Streaming Applications: DataFusion stands out when you need continuous processing of unbounded data streams. Its ability to handle streaming joins, process Kafka data directly through StreamTableExec, and maintain state between batches makes it ideal for real-time applications with continuous data flows.

For Large Dataset Processing: Polars and DuckDB excel when processing large files or datasets that don't fit in memory. Their streaming execution modes efficiently handle out-of-core processing for analytics, ETL, and data transformation tasks with excellent performance.

Use Case Examples:

  • Real-time analytics pipeline: DataFusion provides the foundation for building systems that continuously ingest from Kafka and maintain up-to-date results.
  • Large log file analysis: Polars and DuckDB can efficiently process multi-gigabyte log files on modest hardware, even if the files exceed available memory.
  • Periodic batch processing: For scheduled ETL jobs that process accumulated data at intervals, Polars and DuckDB offer simpler implementation with excellent performance.

Each engine shines in its intended domain. DataFusion excels at true streaming while Polars and DuckDB deliver outstanding performance for analytical workloads and large dataset processing.

The Future of Streaming Query Engines

As data volumes continue growing and real-time analytics becomes increasingly critical, each engine is evolving to better serve its core use cases:

DataFusion continues advancing its streaming capabilities with ongoing development focused on:

  • Native watermarking support for proper event-time processing
  • Built-in state checkpointing for fault tolerance
  • Enhanced connector ecosystem for popular streaming sources

Polars and DuckDB continue to optimize their engines for analytical performance within their target domains, with Polars working on a new streaming engine and DuckDB enhancing its vectorized execution capabilities.

At Flarion, we believe in selecting the right tool for each specific task. We're always evaluating the strengths of different engines and are happy to give each one a chance in the domain where it shines. This pragmatic approach means using DataFusion when true streaming capabilities are required, while leveraging Polars for high-performance single-node analytics and DuckDB for quick in-memory operations.

Oops! Something went wrong while submitting the form.