Java: A platform for platforms
Sun's reorg may seem promising to shareholders but it's also a scramble for position. The question now is whether Sun can, or wants to, maintain its hold on Java technology. Especially with enterprise leaders like SpringSource and RedHat investing heavily in Java's future as a platform for platforms

Also see:

Discuss: Tim Bray on 'What Sun Should Do'

Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Memory matters

Discover how much memory an object consumes

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

First published on December 28, 2001. Updated on January 10, 2003.

Q Without buying an expensive memory profile, is there a simple way to see how much memory a Java object consumes?

I don't wish to see the memory allocations in realtime, so I can employ java.lang.Runtime's memory methods.

For instance, I could:

  1. Get the total amount of memory available to the VM:
    long totalMem = java.lang.Runtime.freeMemory();
    
    
  2. Explicitly set the object in question to null:
    Object myBigObject = null;
    
    
  3. Call the garbage collector

  4. Subtract:
    System.out.println("You just got rid of " + totalMem - java.lang.Runtime.freeMemory());
    
    


What do you think of this idea?

AUnfortunately, you cannot directly determine an object's size. However, in lieu of spending lots of lucre on a profiler, you can crudely approximate the size. While your approach is on the right track, I would change it a bit.

When you instantiate myBigObject and check the memory, the JVM may be performing other activities. So simply creating one object and checking the difference in memory size might be misleading. Instead, you can create a number of objects and take the average of the differences. By taking the average memory size, you can prevent some background JVM activity from overly skewing your results.

The following memory checking main is based upon "Question of the Week No. 107" from the Java Developer Connection:

public class Memory {
    private final static int _SIZE = 500;
    public static void main( String [] args ) throws Exception {
        Object[] array = new Object[_SIZE];
        Runtime.getRuntime().gc();
        long start = Runtime.getRuntime().totalMemory();
        for (int i = 0; i < _SIZE; i++) {
            array[i] = new Object();
        }
        Runtime.getRuntime().gc();
        long end = Runtime.getRuntime().totalMemory();
        long difference = ( start -  end ) / _SIZE;
        System.out.println( difference + " bytes used per object on
average" );
    }
}


Class Memory instantiates 500 Objects and calculates the average memory footprint. Note that I call gc() each time before calling totalMemory(). You cannot force garbage collection; however, it is my hope that the JVM does it so that we can have a cleaner totalMemory() call.

About the author

Tony Sintes is an independent consultant and founder of First Class Consulting, Inc., a consulting firm that specializes in the bridging of disparate enterprise systems and training. Outside of First Class Consulting, Tony is an active freelance writer as well as author of Sams Teach Yourself Object-Oriented Programming in 21 Days (Sams, 2001; ISBN: 0672321092).
  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources