java内存被释放的小例子
先贴代码:
StringBuilder dada = null; for(int i=0; i<1000; i++){ dada = new StringBuilder(); for(int j=0; j<1000; j++){ dada.append("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); } } System.out.println("释放前"); StringBuilder dada1 = new StringBuilder() ; dada1.append("1"); dada = dada1; System.out.println("释放后");
放在内层,可见每次使用内存是74,000K多的内存,而,如果放到循环的外面,
StringBuilder dada = dada = new StringBuilder();; for(int i=0; i<1000; i++){ for(int j=0; j<1000; j++){ dada.append("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); } }
就只会将数据一致累加的去处理,这样数据就大量的堆积起来了,
查看内存可看出是下面一种写法的内存是220,000K多的内存.