Java memory usage and object creation

B

Brendan Guild

How much memory overhead is there in objects? I remember reading that
even an object from a class with just one variable takes several
bytes, but I cannot recall just how bad it is.

How much memory overhead is there in arrays? I'm thinking about having
potentially very long arrays, and wondering if I would get significant
savings by using several parallel arrays of primitives instead of an
array of objects. I would wrap the arrays so that when accessed an
object is created containing a reference to the arrays and an index,
so that the parallel arrays would be like an array of objects. But
this means creating lots of temporary objects and giving the gc more
work. How wasteful is that?
 
R

Robert Olofsson

Brendan Guild ([email protected]) wrote:
: How much memory overhead is there in objects? I remember reading that
: even an object from a class with just one variable takes several
: bytes, but I cannot recall just how bad it is.

I simple test:
java.vm.version = 1.4.2-b28
# M# size #GC
java.lang.Runtime 1 1 8 0
java.util.Hashtable$EmptyEnumerator 1 1 8 0
java.util.jar.JavaUtilJarAccessImpl 1 1 8 0
rabbit.proxy.Proxy$ConnectionFactory 1 1 8 0

Size is in bytes, these are the 4 smallest objects, but there are other
that also are 8 bytes.

Here is the code for the last entry (its an _static_ inner class):

private static class ConnectionFactory
implements RestartableThreadFactory {
public RestartableThread createThread () {
return new Connection ();
}
}

So my guess would be that Object takes 8 bytes, which also seems
to match: java.lang.Object 23 23 184 0

Note: how big an Object is depends on the jvm, some jvms may use
smaller objects.

/robo
 
R

Roedy Green

How much memory overhead is there in objects? I remember reading that
even an object from a class with just one variable takes several
bytes, but I cannot recall just how bad it is.

That will depend on the JVM. There are various sources of overhead.

1. memory may be allocated in some units larger than the byte, e.g. 16
bytes. You waste up to the cell boundary.

2. There may be a mechanism to allow objects to move during garbage
collection. This means there needs to a long table, indexed by object
number giving its physical address. That alone adds at least 4 bytes
per object.

3. There need to be some flags to use during garbage collection to
indicate whether an object has already been visited.

4. If objects cannot move, RAM may become fragmented. The holes are
not usable for large objects.

Your best bet is to do some experiments with YOUR objects and just see
how it behaves in real life.

There is not much you can do about it other than use a different JVM.

Perhaps there is one thing. Remember Caligula who would take a bite
out of a peach, then toss it aside and grab another? Some people
program like than constantly grabbing new objects when existing ones
would suffice just as well.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top