什么是内存抖动
是指在短时间内有大量的对象被创建或者被回收的现象,内存抖动出现原因主要是频繁(很重要)在循环里创建对象(导致大量对象在短时间内被创建,由于
新对象是要占用内存空间的而且是很频繁,
内存抖动的影响
如果抖动很频繁,会导致垃圾回收机制频繁运行(短时间内产生大量对象,需要大量内存,而且还是频繁抖动,就可能会需要回收内存以用于产生对象,垃圾
回收机制就自然会频繁运行了)。综上就是频繁内存抖动会导致垃圾回收频繁运行。
什么是内存泄漏
是指某一段内存在程序里功能上已经不需要了,但是垃圾回收机制回收内存时检测那段内存还是被需要的,不能被回收,这种在程序中在没有使用的但是又不能被
回收的内存就是被泄漏的内存,那为什么会这样呢?
正常的话应该是程序里不需要的内存就可以被回收,这是垃圾回收机制(GC)做的事呀,如果垃圾回收机制正常运行的情况下,不应该这样啊,但是实际就是垃圾
回收机制正常的情况下发生的内存泄漏。
产生内存泄漏的原因
其实到这里java程序员就得知道垃圾回收机制中,判断一段内存是否是垃圾,是否可回收的条件,这个条件是通过检查这段内存是否存在引用和被引用关系,不存
在这关系时,就认为可回收,若还存在引用或被引用关系,就认为不可回收,现在就可以知道导致内存泄漏的原因是程序员没有将不用的内存去掉引用关系(因为
程序中大多内存石油对象指向的,所以去掉引用关系就是置空)。
内存泄漏的影响
内存泄漏会导致一些内存没法被正常利用,话句话就是可以使用内存变少了,这样轻则增加垃圾回收机制运行频率,重则内存溢出(当系统需要分配一段内存,但
是现有内存在垃圾回收运行后任然不足时,就会内存溢出);为避免内存泄漏,在写程序时已经确定不需要的引用型变量,就置空;虽然即使内存没泄露,也有可
能出现内存溢出,这时的内存溢出就是有别的问题导致的。