Hotspot Metaspace

新项目碰到了Metaspace空间不足导致fullgc,所以便想弄清楚Metaspace初始大小,什么时候扩容,扩容多少等等…
首先讲解下在GC日志中,Metaspace这一行的 used ,capacity ,committed ,reserved 具体都代表什么?还有class space 这一行的 used ,capacity ,committed ,reserved 具体都代表什么?

 Metaspace       used 13009K, capacity 13344K, committed 13568K, reserved 1060864K
  class space    used 1584K, capacity 1690K, committed 1792K, reserved 1048576K

Metaspace.hpp源码中的
上面的图是Metaspace.hpp源码中的,我只是截了个图,这只是表达Metaspace有哪些构成对于日志中的参数并未解释,于是去搜了官网,对于Metaspace这一行的参数解释如下。
在这里插入图片描述
上图来自 from

used:加载的类的空间量。
capacity:当前分配块的元数据的空间。
committed:空间块的数量。
reserved:是jvm启动时根据参数和操作系统预留的内存大小。

class space是用于放class的内存的和。

Metaspace初始化大小代码如下

void MetaspaceGC::initialize() {
  // Set the high-water mark to MaxMetapaceSize during VM initializaton since
  // we can't do a GC during initialization.
  _capacity_until_GC = MaxMetaspaceSize;
}

MaxMetaspaceSize字段的定义是在globals.hpp文件,至于如何初始化的我便跟不了,我也是个彩笔,也意味着初始化大小我也不清楚。
每次垃圾回收完成后都会调用MetaspaceGC::compute_new_size,实现动态调整Metaspace大小的功能,以下是源码我只复制如何扩容的代码。

void MetaspaceGC::compute_new_size() {
  assert(_shrink_factor <= 100, "invalid shrink factor");
  uint current_shrink_factor = _shrink_factor;
  _shrink_factor = 0;

  const size_t used_after_gc = MetaspaceAux::committed_bytes();
  const size_t capacity_until_GC = MetaspaceGC::capacity_until_GC();
  //MinMetaspaceFreeRatio是GC完成后需要保证的Metaspace最低的空闲空间比例,默认     是40%	
  const double minimum_free_percentage = MinMetaspaceFreeRatio / 100.0;
  const double maximum_used_percentage = 1.0 - minimum_free_percentage;

  const double min_tmp = used_after_gc / maximum_used_percentage;
  size_t minimum_desired_capacity =
    (size_t)MIN2(min_tmp, double(max_uintx));
  // Don't shrink less than the initial generation size
  minimum_desired_capacity = MAX2(minimum_desired_capacity,
                                  MetaspaceSize);

  if (PrintGCDetails && Verbose) {
    gclog_or_tty->print_cr("\nMetaspaceGC::compute_new_size: ");
    gclog_or_tty->print_cr("  "
                  "  minimum_free_percentage: %6.2f"
                  "  maximum_used_percentage: %6.2f",
                  minimum_free_percentage,
                  maximum_used_percentage);
    gclog_or_tty->print_cr("  "
                  "   used_after_gc       : %6.1fKB",
                  used_after_gc / (double) K);
  }


  size_t shrink_bytes = 0;
  //如果当前的capacity_until_GC小于期望值,则扩容
  if (capacity_until_GC < minimum_desired_capacity) {
    size_t expand_bytes = minimum_desired_capacity - capacity_until_GC;
    //64位系统8字节对齐
    expand_bytes = align_size_up(expand_bytes, Metaspace::commit_alignment());
    //MinMetaspaceExpansion表示扩容时最低的扩展值,默认是256k,低于此值不扩容
    if (expand_bytes >= MinMetaspaceExpansion) {
      size_t new_capacity_until_GC = 0;
      bool succeeded = MetaspaceGC::inc_capacity_until_GC(expand_bytes, &new_capacity_until_GC);
      assert(succeeded, "Should always succesfully increment HWM when at safepoint");

      Metaspace::tracer()->report_gc_threshold(capacity_until_GC,
                                               new_capacity_until_GC,
                                               MetaspaceGCThresholdUpdater::ComputeNewSize);
      if (PrintGCDetails && Verbose) {
        gclog_or_tty->print_cr("    expanding:"
                      "  minimum_desired_capacity: %6.1fKB"
                      "  expand_bytes: %6.1fKB"
                      "  MinMetaspaceExpansion: %6.1fKB"
                      "  new metaspace HWM:  %6.1fKB",
                      minimum_desired_capacity / (double) K,
                      expand_bytes / (double) K,
                      MinMetaspaceExpansion / (double) K,
                      new_capacity_until_GC / (double) K);
      }
    }
    return;
  }
 ......
 }

used_after_gc和capacity_until_GC这两个变量有木有熟悉感,分别对应了上面所说的日志中Metaspace used和capacity,余下的重要的代码都已注释。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值