I am thinking about how much a class resides in memory in case it is not being accessed by other objects in memory?
for example suppose I have some class like this:
public class OrderNumber {
private static long counter = 0;
public static long getOrderNumber(){
if (counter >= 100) {
return counter = 1;
}
return ++counter;
}
}
And I call its static method from another class:
long number = OrderNumber.getOrderNumber();
each time I call it, it returns an incremental number, 1, 2, 3, 4, …
So, My question is What is the probability of this method to return the initial value where it is supposed to return the sequenced value ??
Right now, the answer is forever. Class definitions are put into the permanent generation, and remain there until the program terminates.
Note, this is a problem for dynamic languages on the JVM, like JRuby, which create classes on the fly, because these classes waste space. I believe there are changes in the works that will resolve this problem – for example, the G1 collector