内存溢出和内存泄露
内存溢出 out of memory
内存泄露 memory leak
样例测试代码
问题的排查
CPU使用率过高
样例测试代码
负载过高使用率低
内存溢出和内存泄露
内存溢出 out of memory
是指程序在申请内存时,没有足够的内存空间供其使用,出现out of memory
内存泄露 memory leak
是指程序在申请内存后,无法释放已申请的内存空间,一次内存泄露危害可以忽略,但内存泄露堆积后果很严重,无论多少内存,迟早会被占光。memory leak会最终会导致out of memory!较为常见的有java.lang.OutOfMemoryError: Java heap space
样例测试代码
public class TestHeapOOM {
static class Key {
Integer id;
Key(Integer id) {
this.id = id;
}
@Override
public int hashCode() {
return id.hashCode();
}
}
public static void main(String[] args) {
Map<Key, String> m = new HashMap<>();
while (true) {
for (int i = 0; i < 10000; i++) {
Key key = new Key(i);
if (!m.containsKey(key)) {
m.p