Improve the collection of Java program performance
Java uses garbage collection or recycling the memory that is no longer used without the need for your clear management. Garbage collection is automatic, but in some cases, there may be a way to assist this process. Imagine your stack you manage an object:
Public class stack {
Private static final int maxlen = 10;
Private Object Stk [] = new object [maxlen];
PRIVATE INT STKP = -1;
Public void push (Object P) {STK [ STKP] = P;}
Public Object Pop () {Return STK [STKP--];
}
Now consider having two elements in the stack, and you will take one from it, at this time, STK [0] in the stack will save an effective element, while STK [1] saves the elements just removed. That is, STK [1] is an object's reference, which may be referenced by any object, including a large number of data structures containing thousands of bytes. In this case, this structure cannot be collected by garbage, even if it may never be used.
In order to correct this problem, you can rewrite the POP method as follows:
Public Object Pop ()
{
Object P = STK [STKP];
STK [STKP -] = NULL;
Return P;
}
You didn't make this object itself, but only one reference to it is no longer valid. Stack objects themselves may have a long survival, while rewriting the POP method can help ensure that garbage collection is complete.