Java-How to get the object size

Source: Internet
Author: User

Is there an easy way to get the size of a particular object?

So I found this thing:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/48/6B/wKiom1QH-Q6ieB0DAAHo9yLcc9U558.jpg "title=" Qq20140904132939.jpg "alt=" Wkiom1qh-q6ieb0daaho9ylcc9u558.jpg "/>



Light looked at the declaration of the method, this is not to get the size of the object, so did not think too much, thought that the problem became simple, got a Premain method initialized instrumentation:

public class Objectsizeutils {private static instrumentation Inst = null;    public static void Premain (String Agentargs, Instrumentation inst) {objectsizeutils.inst = inst;        } public static Long SizeOf (Object o) {if (inst = = null) throw new IllegalStateException ("not initialized yet");    return Inst.getobjectsize (o); }}


Export jar and add in MANIFEST.MF:

Premain-class:pac.testcase.utils.objectsizeutilscan-redefine-classes:false


Write a random entity class that you can use to test:

Class person{private String name;    Private String life;    Public String GetName () {return name;}    public void SetName (String name) {this.name = name;}    Public String Getlife () {return life;} public void Setlife (String life) {this.life = Life;}}



Well, add-javaagent in the run parameter and execute to see what will be output:

@Test public void Testsizeutils () {Person person = new person (' Alvez ', ' Make me Death before ' make Me old ');        Logger.info (string.valueof (sizeOf (person)));        Logger.info (String.valueof (SizeOf (Person.getname ()));    Logger.info (String.valueof (SizeOf (Person.getlife ())); } static final Logger Logger = Loggerfactory.getlogger (Objectsizeutils.class);


Results....

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/48/6B/wKiom1QIARLjYjPyAACmhy1CJkY720.jpg "title=" Result.jpg "alt=" Wkiom1qiarljyjpyaacmhy1cjky720.jpg "/>



Why are the attributes of the person larger than the person? Wait, name and life size seems to have problems ....

Go back to the textbook to find that things are not what I want, I need to put the objects inside the size of all add up to achieve the effect I want ....

It seems that some things need to be solved in person, so:

Public class objectsizeutils {    private static instrumentation  inst = null;    public static void premain (String  Agentargs, instrumentation inst)  {         Objectsizeutils.inst = inst;    }    public static  long sizeof (Object o)  throws illegalaccessexception {    if (inst == null)  throw new illegalstateexception ("Not initialized yet");         sizecounter sizecounter = new sizecounter ( );         sizeof (O, sizecounter);         return sizecounter.getsize ();     }    public  static void sizeof (OBJECT&NBsp;o,sizecounter sizecounter)  throws IllegalAccessException {         if (o!=null) {             Sizecounter.addsize (Inst.getobjectsize (o));             class c = o.getclass ();             field[] fields = c.getdeclaredfields ();             for  (Field f : fields)  {                 int mod = f.getmodifiers ();                 if (Modifier.isStatic (MoD)  | |  f.gettype (). Isprimitive ()) {                     continue;                 }                 f.setaccessible (True);                 object o1 = f.get (o);                 sizeof (O1,sizecounter);             }        }     }    }class sizecounter{        private  long size;    public long getsize ()  {         return size;    }    public void  setsize (long size)  {        this.size = size;    }     public void addsize (long size) {         this.setsize (This.getsize ()  + size);     }}



This code is not complete, there are many factors are not taken into account, the general idea is almost like this.



This article is from the Alvez. -99.9% 0b/s "blog, be sure to keep this source http://alvez.blog.51cto.com/7711135/1548815

Java-How to get the object size

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.