huge bunch of objects

K

Katrin Tomanek

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
 
C

Christophe Vanfleteren

Katrin said:
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
 
T

Thomas Weidenfeller

Katrin said:
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
 
A

Andy Fish

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top