Java面向对象编程中的内存管理与类继承
1. Java垃圾回收机制
Java的垃圾回收器运行在后台线程,通常在系统空闲时(如等待用户输入)进行大部分工作。只有当可用内存严重不足时,垃圾回收器才会在高优先级任务执行时运行,不过这种情况并不常见,因为低优先级线程会在后台清理资源。
2. Java中的内存泄漏
Java支持垃圾回收,这大大减少了内存泄漏的发生。内存泄漏指的是内存被分配后却从未被回收。虽然垃圾回收会回收所有未使用的对象,但如果存在对未使用对象的有效(但未使用)引用,仍然可能发生内存泄漏。
以下是一个内存泄漏的示例代码:
public static void main(String args[]) {
int big_array[] = new int[100000];
// Do some computations with big_array and get a result.
int result = compute(big_array);
// We no longer need big_array. It will get garbage collected when there
// are no more references to it. Since big_array is a local variable,
// it refers to the array until this method returns. But this method
// doesn't return. So we've
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



