This discussion is archived
6 Replies Latest reply: Jan 31, 2013 2:47 PM by rp0428 RSS

Urgent: How to find out the size in bytes of java object

608499 Newbie
Currently Being Moderated
Hi Experts,
We've a requirement that we need to find out the size of a java object at run time, is there a direct java API to get the size of a composite object in memory?

Here is my requirement: We are adding string objects (which is an xml string of considerable size) into a List. After reaching certain size limit (lets say 5MB) of the list i need to put this data into DB as a CLOB. So every time I add a string object to the list, I need to see if the total size of the list exceeds the limit and if yes, flush the results into DB.

I am not sure if java has a direct API for this, if not what is the beast way to do it, it s critical requirement for us.
It would be really great if someone could help us out.

Thank you,
-Shibu.
  • 1. Re: Urgent: How to find out the size in bytes of java object
    Tolls Journeyer
    Currently Being Moderated
    There isn't an API for this.
    The closest you'll get is to change your requirement slightly and use String.length() as your criteria for flushing.
  • 2. Re: Urgent: How to find out the size in bytes of java object
    976999 Newbie
    Currently Being Moderated
    Shibu,
    You may find this little lib usefull http://sizeof.sourceforge.net/ . It's a very simple implementation of C++ keyword sizeof for Java 5+.

    Cheers!
  • 3. Re: Urgent: How to find out the size in bytes of java object
    jtahlborn Expert
    Currently Being Moderated
    cezary_u wrote:
    Shibu,
    You may find this little lib usefull http://sizeof.sourceforge.net/ . It's a very simple implementation of C++ keyword sizeof for Java 5+.
    that library does look pretty nifty. however it's pretty clear that the OP does not need anything so complex. As Tolls already said, the OP simply needs to track the length of the currently held Strings.
  • 4. Re: Urgent: How to find out the size in bytes of java object
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    jtahlborn wrote:
    however it's pretty clear that the OP does not need anything so complex. As Tolls already said, the OP simply needs to track the length of the currently held Strings.
    This will also match the OPs needs better since he will transfer the strings rather than the Java objects to the database.

    bye
    TPD
  • 5. Re: Urgent: How to find out the size in bytes of java object
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    user605496 wrote:
    Here is my requirement: We are adding string objects (which is an xml string of considerable size) into a List. After reaching certain size limit (lets say 5MB) of the list i need to put this data into DB as a CLOB.
    Does that mean you create a new Clob (and a new Table entry) for every 5MB of data?

    bye
    TPD
  • 6. Re: Urgent: How to find out the size in bytes of java object
    rp0428 Guru
    Currently Being Moderated
    >
    We've a requirement that we need to find out the size of a java object at run time, is there a direct java API to get the size of a composite object in memory?

    Here is my requirement: We are adding string objects (which is an xml string of considerable size) into a List. After reaching certain size limit (lets say 5MB) of the list i need to put this data into DB as a CLOB. So every time I add a string object to the list, I need to see if the total size of the list exceeds the limit and if yes, flush the results into DB.

    I am not sure if java has a direct API for this, if not what is the beast way to do it, it s critical requirement for us.
    It would be really great if someone could help us out.
    >
    Could you explain your actual requirement a little more fully.

    1. Is each individual string a separate XML string? Or is it just a fragment? Why would you just concatenate them together into a CLOB? That won't produce valid XML and it won't let you easily separate the pieces again later. So provide a simple example showing some strings and how you need to manipulate them.

    2. Are you using these xml strings in Java at all? Or are you just loading them into the database?

    For example if you are just loading them into the database you don't need to create a list. Just create a CLOB in Java and append each string to the CLOB. Before you append each one you can check to see if the new string will put you over your length limit. If it will then you write the CLOB to the database, empty the CLOB and start appending again to the new clob instance.

    If you explain what you are really trying to do we might be able to suggest some better ways to do it.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points