标签:int str tin 内存 sys 导致 instance esc 方法
public class SchoolServiceImpl { private static SchoolServiceImpl instance = new SchoolServiceImpl(); private static ClassServiceImpl classServiceImpl = ClassServiceImpl.getInstanse(); public static SchoolServiceImpl getInstanse() { if (instance == null) return instance = new SchoolServiceImpl(); return instance; } public int getSchoolCount() { return classServiceImpl.getClassesCount(); } public static void main(String[] args) { System.out.println(classServiceImpl.getClassesCount()); } }
public class ClassServiceImpl { /** * @param args */ private static ClassServiceImpl instance = new ClassServiceImpl(); public static ClassServiceImpl getInstanse() { if (instance == null) return instance = new ClassServiceImpl(); return instance; } private static SchoolServiceImpl schoolServiceImpl=SchoolServiceImpl.getInstanse(); public int getClassesCount() { System.out.println(classServiceImpl.toString()); return schoolServiceImpl.getSchoolCount(); } }
如上两个类彼此依赖当中的方法。导致死循环,终于导致内存溢出。
从中得到的教训:尽可能降低类的彼此依赖,做到单向依赖。
标签:int str tin 内存 sys 导致 instance esc 方法
原文地址:http://www.cnblogs.com/mfmdaoyou/p/6814120.html