1、在 Flink 中设置内存的方法是配置以下两个选项之一:
1)Total Flink memory:taskmanager.memory.flink.size+jobmanager.memory.flink.size
2)Total process memory:taskmanager.memory.process.size+jobmanager.memory.process.size
1、flink内存包含以下部分,Total Process Memory总内存(taskmanager/jobmanager.process.size),等于Total Flink Memory内存(flink管理的内存taskmanager/jobmanager.flink.size)+jvm进程消耗内存(JVM Metaspace+JVM Overhead),即 flink 管理部分 jvm 内存,jvm自身也消耗一部分内存。
以下操作中,flink-conf.xml配置参数如下:
jobmanager.memory.heap.size: 128m
taskmanager.memory.flink.size: 1024m
taskmanager.memory.managed.fraction: 0.2
jobmanager.memory.jvm-overhead.min: 64m
taskmanager.memory.network.fraction: 0.1
2、指定 jobmanager 内存
① 当我们指定jobmanager内存:-yjm(jobmanager.process.size)=512m时,内存的分配流程如下:
JVM Heap:128M,根据配置参数(jobmanager.memory.heap.size)
JVM Metaspace默认值: 256M
Off-Heap Memory默认值:128M
JVM Overhead:0M=512M-128M-256M-128M
解释了报错:JobManager memory configuration failed: Derived JVM Overhead size (0 bytes) is not in configured JVM Overhead range [64.000mb (67108864 bytes), 1024.000mb (1073741824 bytes)]
可以看到报错中说JVM Overhead 内存为0,不在最小值最大值内,因为我们的配置中指定了jobmanager.memory.jvm-overhead.min: 64m,0<64m,肯定失败。
② 当我们指定-yjm(jobmanager.process.size)=1024m时,参考①中可以得出如下图的内存分配:
3、指定 taskmanager 内存
① 下图是指定了:-ytm(taskmanager.process.size)=2048m,taskmanager.memory.flink.size=1024m时的内存占用,即:
ytm=Total Process Memory=Total Flink Memory(taskmanager.memory.flink.size)+JVM Metaspace+JVM Overhead,我们发现这里有一个问题Total Flink Memory=1024m且JVM Metaspace+JVM Overhead=1024m,也就是我们指定taskmanager.process.size参数时,就不要指定taskmanager.memory.flink.size参数,否则会导致JVM Overhead被分配过大的内存造成浪费。
② 当我们不指定taskmanager.memory.flink.size参数时,taskmanager内存分配结果和分配方式:
JVM Metaspace(默认):256m
Jvm Overhead:2048 * 0.1(默认)=205m
Framework Heap(默认):128M
Managed Memory:(2048m-256m(JVM Meataspace)-205m(Jvm Overhead))*0.2=317m
Network:(2048m-256m(JVM Meataspace)-205m)*0.1=159m
Framework Off-Heap(默认):128m
Task Heap:2048-(其他所有:256m+205m+128m+317m+159m+128m)=855m