GC辅助,Help GC,把变量赋值为NULL会帮助GC吗?

package com.test;

import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

import com.google.common.collect.Lists;

/**
 * {@link https://blog.csdn.net/qq_42945742/article/details/84107531}
 * {@link https://blog.csdn.net/u011381576/article/details/79755069}
 * 
 * javac -g:vars GcTest
 * javap -verbose GcTest
 * java -verbose:gc GcTest
 * 
* * @author * @date 2021/01/20 */ public class GcTest { public static void main(String[] args) { List list = Lists.newArrayListWithCapacity(20); System.out.println("==="); IntStream.range(0, 5).boxed().forEach(i -> { ByteArrayOutputStream bas = new ByteArrayOutputStream(64 * 1024 * 1024); list.add(bas); System.out.println("=====!\r\n"); }); System.out.println("===1"); System.gc(); System.out.println("===2"); ByteArrayOutputStream[] basArray = list.stream().toArray(ByteArrayOutputStream[]::new); List list2 = Arrays.asList(basArray); System.gc(); System.out.println("===2"); basArray = null; list.clear(); System.gc(); System.out.println("===3"); list2 = null; System.gc(); System.out.println("===4"); } private static void test3() { { byte[] placeholder = new byte[64 * 1024 * 1024]; } int a = 0; System.gc(); System.out.println("===="); } private static void test2() { { int b = 0; byte[] placeholder = new byte[64 * 1024 * 1024]; } int a = 0; System.gc(); System.out.println("===="); } private static void test1() { { int b = 0; byte[] placeholder = new byte[64 * 1024 * 1024]; } ByteArrayOutputStream bas = new ByteArrayOutputStream(64 * 1024 * 1024); for (int i = 0; i < 64 * 1024 * 1024; i++) { bas.write(i); } byte[] xx = bas.toByteArray(); byte[] yy = Arrays.copyOf(xx, xx.length); System.out.println("===="); int a = 0; System.gc(); System.out.println("===="); bas = null; System.gc(); System.out.println("===="); yy = null; System.gc(); System.out.println("===="); xx = null; System.gc(); System.out.println("===="); } }

你可能感兴趣的:(GC辅助,Help GC,把变量赋值为NULL会帮助GC吗?)