Object creation overhead

From: Richard (richard_at_praxistech.com)
Date: 11/26/03


Date: Wed, 26 Nov 2003 18:04:31 GMT


Hi all,

I am looking for some help on understanding the overhead associated with
object creation in Java.

I am writing an application where I have written a class to encapsulate some
text data.

The class is contains these private variables:

    // private variables
    private String id = null;
    private String subject = null;
    private String from = null;
    private String date = null;
    private String bytes = null;
    private String lines = null ;
    private boolean isread;
    private String server = null;
    private int port;
    private String group = null;

Now I create each object with no parameters passed into the constructor, and
manually populate 6 of the String variables indicated above. Also the
server, port and group variables are populated for each object instance.

Of course there are accessor methods to deal with accessing the private
variables, in total about 29 methods for this class. The Class implements
Serializable and Comparable.

Now I am trying to figure out when one of these puppies is created, how much
memory does an instance take up. I am not sure how to correctly do this.

I determine the size of all the text contained by the object by using the
following method:

   myObject.toString().getBytes()).length
  
where toString has been overridden in the class to create a StringBuffer.
The above method reports an average size of 233 bytes.

I create about 82,000 of these objects and place them into an ArrayList.
When I cacluate the following:

Runtime.getRuntime().totalMemory()

before and after the ArrayList creation and divide the difference by
ArrayList.size(), I come to the conclusion that each object in the
ArrayList is about 2300 bytes.

I want to reduce the size of this, since this ArrayList is using roughly
172MB. I use ArrayList.trimToSize() but it doesn't seem to do much....

How can I accurately determine each object in the ArrayLists size? That is
how much memory the class consumes and not just some of its variables?

Any suggestions on how to reduce the memory footprint of this class?

Thanks in advance,
Rich

p.s. in a similar app written in c++, the same List consumes about 18MB, and
I am trying to get a similar memory footprint as this....



Relevant Pages

  • Re: Arrays
    ... Private CarArray As New ArrayList ... Private NoOfCarSalesArray As New ArrayList ... Here is my code for the button that adds these values to both the arraylists that i show u above. ...
    (microsoft.public.dotnet.general)
  • Re: Slicing Routine!
    ... arraylists containing each segment. ... private void button1_Click ... }TIA ...
    (microsoft.public.dotnet.languages.csharp)