3. 什么是内存泄漏
• JAVA 存在内存泄漏
C 的例子:
int *pi; void foo() {
pi = (int*) malloc(8*sizeof(int)); // oops, memory leak of 4 ints
// use pi
free(pi); // foo() is done with pi
}
void main() {
pi = (int*) malloc(4*sizeof(int));
foo();
pi[0] = 10; // oops, pi is now a dangling pointer
}
4. Java 的例子
import java.io.IOException;
import java.util.HashSet;
import java.util.Random;
import java.util.Vector;
public class LeakExample {
static Vector myVector = new Vector();
static HashSet pendingRequests = new HashSet();
public void slowlyLeakingVector(int iter, int count) {
for (int i=0; i<iter; i++) {
for (int n=0; n<count; n++) {
myVector.add(Integer.toString(n+i));
}
for (int n=count-1; n>0; n--) {
// Oops, it should be n>=0
myVector.removeElementAt(n);
}
}
}
5. Java 的例子
public void leakingRequestLog(int iter) {
Random requestQueue = new Random();
for (int i=0; i<iter; i++) {
int newRequest = requestQueue.nextInt();
pendingRequests.add(new Integer(newRequest));
// processed request, but forgot to remove it
// from pending requests
}
}
public void noLeak(int size) {
HashSet tmpStore = new HashSet();
for (int i=0; i<size; ++i) {
String leakingUnit = new String("Object: " + i);
tmpStore.add(leakingUnit);
}
// Though highest memory allocation happens in this
// function, but all these objects get garbage
// collected at the end of this method, so no leak.
}
6. Java 的例子
public static void main(String[] args) throws IOException {
LeakExample javaLeaks = new LeakExample();
for (int i=0; true; i++) {
try { // sleep to slow down leaking process
Thread.sleep(1000);
} catch (InterruptedException e) { /* do nothing */ }
System.out.println("Iteration: " + i);
javaLeaks.slowlyLeakingVector(1000,10);
javaLeaks.leakingRequestLog(5000);
javaLeaks.noLeak(100000);
}
}
}