Introduction
Memory management helps Spark use computer memory efficiently. Garbage collection cleans up unused data to keep Spark fast and stable.
spark.conf.set("spark.executor.memory", "4g") spark.conf.set("spark.memory.fraction", 0.6) spark.conf.set("spark.memory.storageFraction", 0.5)
spark.conf.set("spark.executor.memory", "2g")
spark.conf.set("spark.memory.fraction", 0.8)
spark.conf.set("spark.memory.storageFraction", 0.3)
from pyspark.sql import SparkSession spark = SparkSession.builder.appName("MemoryExample").getOrCreate() # Set executor memory to 3 GB spark.conf.set("spark.executor.memory", "3g") # Set memory fraction to 70% spark.conf.set("spark.memory.fraction", 0.7) # Create a simple DataFrame data = [(1, "apple"), (2, "banana"), (3, "cherry")] df = spark.createDataFrame(data, ["id", "fruit"]) # Cache the DataFrame to use memory storage df.cache() # Show the DataFrame result = df.collect() spark.stop() print(result)
val spark = SparkSession.builder().appName("Test").getOrCreate()
spark.conf.set("spark.executor.memory", "512m")
val data = spark.range(100000000).cache()
data.count()Pass the test and this pattern moves unknown