为什么我的Oracle JVM为一个简单的"Hello World"程序创建所有这些对象? [英] Why does my Oracle JVM create all these objects for a simple 'Hello World' program?

查看:60
本文介绍了为什么我的Oracle JVM为一个简单的"Hello World"程序创建所有这些对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩jmap,发现简单的"Hello World" Java程序创建了数千个对象.以下是在启动时创建的对象 Oracle JVM更新131 的截断列表:

I was playing around with jmap and found that simple "Hello World" Java program creates thousands of objects. Here is truncated list of objects Oracle JVM update 131 creates on startup:

 num     #instances         #bytes  class name
----------------------------------------------
   1:           402        4903520  [I
   2:          1621         158344  [C
   3:           455          52056  java.lang.Class
   4:           194          49728  [B
   5:          1263          30312  java.lang.String
   6:           515          26088  [Ljava.lang.Object;
   7:           115           8280  java.lang.reflect.Field
   8:           258           4128  java.lang.Integer
   9:            94           3760  java.lang.ref.SoftReference
  10:           116           3712  java.util.Hashtable$Entry
  11:           126           3024  java.lang.StringBuilder
  12:             8           3008  java.lang.Thread
  13:            74           2576  [Ljava.lang.String;
  14:            61           1952  java.io.File
  15:            38           1824  sun.util.locale.LocaleObjectCache$CacheEntry
  16:            12           1760  [Ljava.util.Hashtable$Entry;
  17:            53           1696  java.util.concurrent.ConcurrentHashMap$Node
  18:            23           1472  java.net.URL
  19:            14           1120  [S
  20:             2           1064  [Ljava.lang.invoke.MethodHandle;
  21:             1           1040  [Ljava.lang.Integer;
  22:            26           1040  java.io.ObjectStreamField
  23:            12           1024  [Ljava.util.HashMap$Node;
  24:            30            960  java.util.HashMap$Node
  25:            20            800  sun.util.locale.BaseLocale$Key

我知道JVM从JAR文件中加载类,并且希望看到java.lang.Classjava.lang.String[Ljava.lang.Object.我也很清楚258个java.lang.Integer对象:这是Integer缓存.

I know that the JVM loads classes from JAR files and expect to see java.lang.Class, java.lang.String and [Ljava.lang.Object. 258 java.lang.Integer objects are clear tp me too: this is the Integer cache.

但是java.lang.reflect.Field? Hashtable?许多StringBuilder? java.util.concurrent.ConcurrentHashMap?这是哪里来的?

But java.lang.reflect.Field? Hashtable? Many StringBuilders? java.util.concurrent.ConcurrentHashMap? Where does this come from?

程序非常简单:

public class Test {
    public static void main(String[] args) throws IOException {
        System.out.println("Hello world");
        System.in.read();
    }
}

JVM详细信息:

java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

Ubuntu 16.04.

Ubuntu 16.04.

推荐答案

您可以通过使用-XX:+TraceBytecodes标志运行应用程序来自己找到答案.
该标志可在调试Hotspot JVM的版本中使用.

You can find the answer yourself by running the application with -XX:+TraceBytecodes flag.
This flag is available in debug builds of HotSpot JVM.

这是详细的火焰图(可单击SVG),显示了分配对象来自的堆栈跟踪.

Here is the detailed Flame Graph (clickable SVG) showing the stack traces where the allocated objects come from.

在我看来,启动资金的主要来源是

In my case the main sources of start-up allocations were

  • URLClassLoader和扩展ClassLoader
  • 区域设置缓存
  • UsageTrackerClient
  • URLClassLoader and Extension ClassLoader
  • Locale cache
  • UsageTrackerClient
  • MetaIndex registry
  • System Properties
  • Charset initialization

P.S. 脚本用于生成

P.S. The script used to generate the Flame Graph from TraceBytecodes output.

这篇关于为什么我的Oracle JVM为一个简单的"Hello World"程序创建所有这些对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆