Today, I verified a small problem that I had been confused for a while. The code is not described too much.
Public class test {
Private Static final runtime s_runtime = runtime. getruntime ();
Private Static long usedmemory (){
Return s_runtime.totalmemory ()-s_runtime.freememory ();
}
Private Static void rungc () throws exception {
Long usedmem1 = usedmemory (), usedmem2 = long. max_value;
For (INT I = 0; (usedmem1 <usedmem2) & (I <500); ++ I ){
S_runtime.runfinalization ();
S_runtime.gc ();
Thread. currentthread (). Yield ();
Usedmem2 = usedmem1;
Usedmem1 = usedmemory ();
}
}
Public static void main (string [] ARGs) throws exception {
System. Out. println (test. usedmemory ());
For (INT I = 0; I <1000; I ++ ){
A A = new A (); // B = new B ();
}
System. Out. println (test. usedmemory ());
Test. rungc ();
System. Out. println (test. usedmemory ());
}
}
Class {
Private string;
Private string B;
Private string C;
Private string D;
Private string E;
Private string F;
Private string g;
Private string h;
Private string I;
Private string J;
}
Class B {
Private string;
Private string B;
Private string C;
Private string D;
Private string E;
Private string F;
Private string g;
Private string h;
Private string I;
Private string J;
Public String geta (){
Return;
}
Public void Seta (string ){
This. A =;
}
Public String getb (){
Return B;
}
Public void SETB (string B ){
This. B = B;
}
Public String GETC (){
Return C;
}
Public void SETC (string c ){
This. C = C;
}
Public String getd (){
Return D;
}
Public void setd (string d ){
This. d = D;
}
Public String Gete (){
Return E;
}
Public void sete (string e ){
This. E = E;
}
Public String getf (){
Return F;
}
Public void SETF (string f ){
This. f = F;
}
Public String getg (){
Return g;
}
Public void setg (string g ){
This. G = g;
}
Public String geth (){
Return h;
}
Public void Seth (string h ){
This. H = h;
}
Public String Geti (){
Return I;
}
Public void SETI (string I ){
This. I = I;
}
Public String getj (){
Return J;
}
Public void setj (string J ){
This. j = J;
}
}
The result is as follows:
Result
201640
245512
136040 or 136200
Result B
201640
247192
136200