huge bunch of objects

Discussion in 'Java' started by Katrin Tomanek, Jun 17, 2004.

  1. Hi again,

    assume I have a java programm that will create about 500000 (half a
    million !) objects (which are of a very easy type, just about 10
    instance string-variables).

    Do u think there will be a memory problem (on a "normal" pc with 256-512
    MB RAM) or any other constraints or problems due to that big amount of
    objects ?

    thanx for your answers.

    Katrin
     
    Katrin Tomanek, Jun 17, 2004
    #1
    1. Advertising

  2. Katrin Tomanek wrote:

    > Hi again,
    >
    > assume I have a java programm that will create about 500000 (half a
    > million !) objects (which are of a very easy type, just about 10
    > instance string-variables).
    >
    > Do u think there will be a memory problem (on a "normal" pc with 256-512
    > MB RAM) or any other constraints or problems due to that big amount of
    > objects ?
    >
    > thanx for your answers.
    >
    > Katrin


    You'll very likely have problems:

    Let's do the calculations, assuming you're on a 32 bit platform.

    Each object has an overhead of about 12 bytes:
    <http://www-106.ibm.com/developerworks/ibm/library/i-garbage1/>

    You have 500.000 custom Objects, that each have references to 10 String
    objects, which hold a char array object. We'll also need to count the
    reference to each object (32 bit)

    So you need about (500.000 + (500.000 * 10) + (500.000 * 10)) * (12B + 4B) =
    +-160MB for the Objects and their refs.

    But you also need to keep the instance fields of the Strings themselves in
    count:

    3 ints and a char array (we've already counted the ref + the overhead of the
    char array, now we need to count the contents of that char array. Let's
    assume an average String length of 10 chars (a 16 bit primitive):

    (10 * 4B) + 12B = 52B / String
    5.000.000 * 52B = 247MB for the actual String data.

    So you'll need a minimum of 160 + 247 = 407MB to hold all those objects in
    memory at once.

    This could be much less if many of the Strings were the same. The best case
    scenario, in which you always reference the same, empty String, only takes
    about 500.000 * (12B + 4B) + 500.000 * 40B = +- 26.7MB

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Jun 18, 2004
    #2
    1. Advertising

  3. Katrin Tomanek wrote:
    > Hi again,
    >
    > assume I have a java programm that will create about 500000 (half a
    > million !) objects (which are of a very easy type, just about 10
    > instance string-variables).


    Don't do it :) Consider a design where you need less objects. Perhaps
    your objects are to fine-grained? At least consider having a look at the
    flyweight design pattern. It might be possible that you can externalize
    the state (maybe even to a back end DB) of the objects to reduce your
    memory consumption.

    > Do u think there will be a memory problem (on a "normal" pc with 256-512
    > MB RAM) or any other constraints or problems due to that big amount of
    > objects ?


    Why don't you just try? It should be trivial to create a test program
    that instantiates 500000 objects in a loop, and adds them to a list or
    array.

    /Thomas
     
    Thomas Weidenfeller, Jun 18, 2004
    #3
  4. Katrin Tomanek

    Andy Fish Guest


    >
    > Don't do it :)
    >


    I would say, do it only if you really need to.

    If you need very high speed access to these objects such that they must be
    in RAM, and if you are going to deploy this app only on a system that is
    dedicated to running that app.

    After all, if it is a server and provides a valuable service to many users,
    a few hundred bucks on extra RAM chips is no big deal.
     
    Andy Fish, Jun 18, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nayt Grochowski
    Replies:
    2
    Views:
    588
    Nayt Grochowski
    Dec 3, 2003
  2. David
    Replies:
    4
    Views:
    5,993
    Vidar Petursson
    May 21, 2004
  3. Sony Antony

    A bunch of RMI questions

    Sony Antony, Jul 1, 2003, in forum: Java
    Replies:
    5
    Views:
    3,020
    Sony Antony
    Jul 2, 2003
  4. Replies:
    1
    Views:
    427
    Bryce
    Sep 13, 2004
  5. Replies:
    3
    Views:
    530
Loading...

Share This Page