GC.STV
Avoid "static" collection; it can grow without bounds
Description
This rule flags any occurrence of "static" collection.
Static variables that can hold large number of objects (e.g., static variables of type Vector or Hashtable) are prime candidates for memory leaks.
Example
package GC;
import java.util.Vector;
public class STV {
public static Vector vector = new Vector (5,5);
}
class VectorClass {
void method(Object o) {
STV.vector.add(o);
}
}
Repair
If a static variable is necessary, set a maximum size and make sure that Vector does not exceed the limit.
package GC;
import java.util.Vector;
public class STV {
public static void addVector(Object o) {
// checks size of the Vector before calling 'add()'.
if(vector.size() < MAX) {
vector.add(o);
}
}
void method(Object o) {
addVector(o);
}
public static Vector vector = new Vector (5,5);
public static final int MAX = 5;
}
|