Calculate the memory occupied by Java objects

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.