hadoop报错Container is running beyond memory limits

写在前面

在我们运行hive时,有时会报内存使用超过了限制的异常,内容如下:

Container [pid=18578,containerID=container_e29_1554258701746_2799_01_000007] is running beyond physical memory limits. 
Current usage: 1.0 GB of 1 GB physical memory used; 2.7 GB of 2.1 GB virtual memory used.

那么应该如何解决呢?首先看一下yarn内存相关的概念。

相关概念

参数

这里贴出hadoop与内存相关的一些参数,他们的位置、默认值、以及描述。

yarn.nodemanager.resource.memory-mb in yarn-default.xml

  <property>
    <description>Amount of physical memory, in MB, that can be allocated 
    for containers. If set to -1 and
    yarn.nodemanager.resource.detect-hardware-capabilities is true, it is
    automatically calculated(in case of Windows and Linux).
    In other cases, the default is 8192MB.
    </description>
    <name>yarn.nodemanager.resource.memory-mb</name>
    <value>-1</value>
  </property>

yarn.nodemanager.vmem-pmem-ratio in yarn-default.xml

  <property>
    <description>Ratio between virtual memory to physical memory when
    setting memory limits for containers. Container allocations are
    expressed in terms of physical memory, and virtual memory usage
    is allowed to exceed this allocation by this ratio.
    </description>
    <name>yarn.nodemanager.vmem-pmem-ratio</name>
    <value>2.1</value>
  </property>

yarn.scheduler.maximum-allocation-mb in yarn-default.xml

  <property>
    <description>The maximum allocation for every container request at the RM
    in MBs. Memory requests higher than this will throw an
    InvalidResourceRequestException.</description>
    <name>yarn.scheduler.maximum-allocation-mb</name>
    <value>8192</value>
  </property>

yarn.scheduler.minimum-allocation-mb in yarn-default.xml

  <property>
    <description>The minimum allocation for every container request at the RM
    in MBs. Memory requests lower than this will be set to the value of this
    property. Additionally, a node manager that is configured to have less memory
    than this value will be shut down by the resource manager.</description>
    <name>yarn.scheduler.minimum-allocation-mb</name>
    <value>1024</value>
  </property>

mapreduce.map.memory.mb in mapred-default.xml

  <property>
    <name>mapreduce.map.memory.mb</name>
    <value>1024</value>
    <description>The amount of memory to request from the scheduler for each
    map task.
    </description>
  </property>

mapreduce.reduce.memory.mb in mapred-default.xml

  <property>
    <name>mapreduce.reduce.memory.mb</name>
    <value>1024</value>
    <description>The amount of memory to request from the scheduler for each
    reduce task.
    </description>
  </property>

mapreduce.map.java.opts in mapred-default.xml
注意:这个参数在默认配置文件中是注释掉的。

  <!-- This is commented out so that it won't override mapred.child.java.opts.
    <property>
      <name>mapreduce.map.java.opts</name>
      <value></value>
      <description>Java opts only for the child processes that are maps. If set,
      this will be used instead of mapred.child.java.opts.
      </description>
  </property>
  -->

mapreduce.reduce.java.opts in mapred-default.xml
注意:这个参数在默认配置文件中是注释掉的。

  <!-- This is commented out so that it won't override mapred.child.java.opts.
    <property>
      <name>mapreduce.reduce.java.opts</name>
      <value></value>
      <description>Java opts only for the child processes that are reduces. If set,
      this will be used instead of mapred.child.java.opts.
      </description>
    </property>
  -->
参数如何生效

首先,上面的描述已经基本描述清楚了参数的基本作用。然后我们来说一下他们之间的关系。

yarn.nodemanager.resource.memory-mb:设置所在的节点有多少内存可以被container申请。

yarn.nodemanager.vmem-pmem-ratio:虚拟内存和物理内存的比例,container申请的是物理内存,有了这个比例,虚拟内存也可以计算出来。当container使用的内存超过物理内存或超过虚拟内存时,yarn会抛异常,并且把这个container给kill掉。

yarn.scheduler.maximum-allocation-mb/yarn.scheduler.minimum-allocation-mb:一个container可以从***ResourceManager***申请到的最大/最小内存。

mapreduce.map.memory.mb:指定一个map任务的内存大小,由于map任务是运行在container中的,所以这个值应该在yarn.scheduler.maximum-allocation-mb/yarn.scheduler.minimum-allocation-mb之间,如果小于或大于container的值,那么就去container的最小/最大值。

mapreduce.reduce.memory.mb:同mapreduce.map.memory.mb。一般reduce的内存大小配置为map内存的两倍。

mapreduce.map.java.opts:ie:***-Xmx1024m***。jvm参数,一般设置的略微小于mapreduce.map.memory.mb。

mapreduce.reduce.java.opts:同mapreduce.map.java.opts。

解决方法

在yarn上运行的任务,内存溢出和在物理机上是一个道理。

在物理机上运行的程序,物理内存是8G,程序不断的申请内存,则内存溢出了。那么我们是怎么解决的呢?
①加内存(物理内存或虚拟内存)。
②优化程序,是程序占用更小的内存。

在yarn上的任务,也是这个思路。
①加内存,通过上面的参数可以增大一个map task或reduce task可以使用的内存。
②减小task使用的内存。在map/reduce编程中,task使用的内存大小与它处理的数据成正相关,所以我们需要减小一个task所处理的数据量。

a> map

一个map task任务处理的是一个split的数据。通过下面的参数设置一个split的最大和最小数据量:

SET mapred.max.split.size=62914560;

同时可以加下下面的配置进行合并小文件,提高效率:

set mapred.min.split.size=41943040;
set mapred.min.split.size.per.node=41943040;
set mapred.min.split.size.per.rack=41943040;
set hive.input.format=org.apache.hadoop.hive.ql.io.CombineHiveInputFormat;

通过 default_num = total_size / dfs.block.size就能获取默认的map个数。也可以通过***mapred.map.tasks***参数来设置map个数,这个参数只有在大于default_num时才生效。

set mapred.map.tasks=10;

b> reduce

默认一个reduce处理256M的数据量。通过***hive.exec.reducers.bytes.per.reducer***设置一个reduce处理的数据量。

  <property>
    <name>hive.exec.reducers.bytes.per.reducer</name>
    <value>256000000</value>
    <description>size per reducer.The default is 256Mb, i.e if the input size is 1G, it will use 4 reducers.</description>
  </property>

也可以通过下面的参数设置hive task的数量:

In order to limit the maximum number of reducers:
  set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
  set mapreduce.job.reduces=<number>

补充

参数

不管是hdfs参数,还是yarn、hive的参数,都可以使用如下代码获取参数的值。

hive> set hive.exec.reducers.bytes.per.reducer;
hive.exec.reducers.bytes.per.reducer=67108864
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值