对象实例化
创建对象的六步骤
虚拟机遇到一条new指令 首先去检查这个指令的参数能否在Metaspace的常量池 中定位到一个类的符号引用 并且检查这个符号引用代表的类是否已经被加载 解析和初始化 (即判断类元信息是否存在)
如果没有 那么在双亲委派模式下 使用当前类加载器以ClassLoader+包名+类名为key 查找对应的class文件 如果没有找到 则抛出 ClassNotFoundException异常
如果找到 则进行类加载 并生成对应的class类对象
为对象分配内存(首先计算对象占用空间大小 接着在堆中划分一块内存给新对象 如果实例成员变量 是引用变量 仅分配引用变量空间 即可 即四个字节大小)
如果内存规整 指针碰撞
意思是 所有用过的内存在一边 空闲的内存在另一边 中间放着一个指针作为分界点的指示器
分配内存就仅仅是把指针往空闲那边挪动一段与对象大小相等的距离.如果垃圾收集器选择的是 Serial ParNew这种基于压缩算法的 虚拟机采用这种分配方式 一般使用带有compat(整理)过程的收集器时,使用指针碰撞
如果内存不规整 已使用的内存和未使用的内存相互交错 虚拟机采用的是 空闲列表法
意思是虚拟机维护了一个列表 记录上哪些内存块是可用的 再分配的时候从列表中找到一块足够大的空间划分给对象实例 并更新列表上的内容 这种分配方式 称作 空闲列表法(Free List) CMS算法
选择哪种分配方式由java堆是否规整决定 而是否 规整又由所采用的垃圾收集器是否带有压缩整理功能决定
处理并发 安全为题
CAS失败重试 区域加锁保证更新的原子性
每个线程先分配一块TLAB
初始化 分配到的空间 所有属性设置默认值 保证对象实例字段在不赋值的时候可以使用
设置对象的对象头
将对象的所属类(即类的元数据信息)对象的hashCode 和对象的gc 信息 锁信息 等数据存储在对象的对象头中 这个过程的具体设置方式取决于JVM实现
执行init 进行初始化
在java程序的视角来看 初始化才正式开始 初始化成员变量 执行实例化代码块 调用类的构造方法 并把堆内对象的首地址赋值给引用变量
由此一般来说 (由字节码中是否跟随由invokespecial)指令所决定 new指令之后 会接着执行方法 把对象按照程序员的意愿 进行初始化 这样一个真正的可用的对象 才算完全创建出来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package com.atguigu.java;public class Customer { int id = 1001 ; String name; Account acct; { name = "匿名客户" ; } public Customer () { acct = new Account (); } } class Account {}
对象的内存布局 对象在堆内存中的存储布局可以划分为三个部分:
对象头 ,实例数据 ,和对齐填充
对象头
并不是 都有
实例数据 说明:它是对象真正存储的有效信息 包括程序代码中定义的各种类型的字段 包括从父类继承下来的和本身拥有的字段
规则:
相同宽度的字段总是被分配到一起
父类中定义的变量会出现在子类之前
如果CompactFiled参数为true (默认为true) 子类的窄变量可能插入到父类变量的空隙
对齐填充 不是必须的 也没有特别含义 仅仅起到占位符的作用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class Customer { int id = 1001 ; String name; Account acct; { name = "匿名客户" ; } public Customer () { acct = new Account (); } } class Account {}
对象的访问定位 JVM是如何通过栈帧中的对象引用访问到其内部的对象实例 的呢?
定位 通过reference 方位
访问的方式主要两种: 句柄访问 直接指针
句柄访问
直接指针(HotSpot采用)
效率高
记录的对象垃圾回收的时候需要修改指向的对象
直接内存(Direct Memory)
NIO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class BufferTest { private static final int BUFFER = 1024 * 1024 * 1024 ; public static void main (String[] args) { ByteBuffer byteBuffer = ByteBuffer.allocateDirect(BUFFER); System.out.println("直接内存分配完毕,请求指示!" ); Scanner scanner = new Scanner (System.in); scanner.next(); System.out.println("直接内存开始释放!" ); byteBuffer = null ; System.gc(); scanner.next(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 public class BufferTest1 { private static final String TO = "F:\\test\\异界BD中字.mp4" ; private static final int _100Mb = 1024 * 1024 * 100 ; public static void main (String[] args) { long sum = 0 ; String src = "F:\\test\\异界BD中字.mp4" ; for (int i = 0 ; i < 3 ; i++) { String dest = "F:\\test\\异界BD中字_" + i + ".mp4" ; sum += directBuffer(src,dest); } System.out.println("总花费的时间为:" + sum ); } private static long directBuffer (String src,String dest) { long start = System.currentTimeMillis(); FileChannel inChannel = null ; FileChannel outChannel = null ; try { inChannel = new FileInputStream (src).getChannel(); outChannel = new FileOutputStream (dest).getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocateDirect(_100Mb); while (inChannel.read(byteBuffer) != -1 ) { byteBuffer.flip(); outChannel.write(byteBuffer); byteBuffer.clear(); } } catch (IOException e) { e.printStackTrace(); } finally { if (inChannel != null ) { try { inChannel.close(); } catch (IOException e) { e.printStackTrace(); } } if (outChannel != null ) { try { outChannel.close(); } catch (IOException e) { e.printStackTrace(); } } } long end = System.currentTimeMillis(); return end - start; } private static long io (String src,String dest) { long start = System.currentTimeMillis(); FileInputStream fis = null ; FileOutputStream fos = null ; try { fis = new FileInputStream (src); fos = new FileOutputStream (dest); byte [] buffer = new byte [_100Mb]; while (true ) { int len = fis.read(buffer); if (len == -1 ) { break ; } fos.write(buffer, 0 , len); } } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null ) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null ) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } long end = System.currentTimeMillis(); return end - start; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public class BufferTest2 { private static final int BUFFER = 1024 * 1024 * 20 ; public static void main (String[] args) { ArrayList<ByteBuffer> list = new ArrayList <>(); int count = 0 ; try { while (true ){ ByteBuffer byteBuffer = ByteBuffer.allocateDirect(BUFFER); list.add(byteBuffer); count++; try { Thread.sleep(100 ); } catch (InterruptedException e) { e.printStackTrace(); } } } finally { System.out.println(count); } } }