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:
- Get the total amount of memory available to the VM:
long totalMem = java.lang.Runtime.freeMemory();
- Explicitly set the object in question to null:
Object myBigObject = null;
- Call the garbage collector
- Subtract:
System.out.println("You just got rid of " + totalMem - java.lang.Runtime.freeMemory());
A: What do you think of this idea?
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.
Learn more about this topic
- "Question of the Week No. 107," researched by Ramchander Varadarajan (Java Developer Connection, September 2000)
http://developer.java.sun.com/developer/qow/archive/107/index.html - Jeff Friesen discusses the details of garbage collection and
gc()in "Trash Talk, Part 1" (JavaWorld, December 2000)
http://www.javaworld.com/javaworld/jw-12-2001/jw-1207-java101.html - You'll find numerous JVM-related articles in the Java Virtual Machine section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-jvm-index.shtml - Want more? See the Java Q&A Index for the full Q&A catalog
http://www.javaworld.com/columns/jw-qna-index.shtml - For over 100 insightful Java tips from some of the best minds in the business, visit JavaWorld's Java Tips Index
http://www.javaworld.com/columns/jw-tips-index.shtml - Learn the basics of client-side Java in our Java Beginner discussion. Core topics include the Java language, the Java Virtual Machine, APIs, and development tools
http://forums.idg.net/webx?50@@.ee6b804 - Sign up for JavaWorld's free Applied Java newsletter
http://www.javaworld.com/subscribe - You'll find a wealth of IT-related articles from our sister publications at IDG.net


![JavaWorld > Android Studio for Beginners [movile java package]](https://images.idgesg.net/images/article/2019/02/jw_android_studio_for_beginners_3x2_1200x800_pkg_idg_google_darkovujic_gettyimages-100788152-small.3x2.jpg)
![JavaWorld > Persistence [series] > data blocks / data center / database / server traffic routing](https://images.idgesg.net/images/article/2019/04/jw_java_persistence_series_3x2_2400x1600_3_data_center_database_digital_information_blocks_server_traffic_routing_by_ramcreativ_gettyimages-100792562-small.3x2.jpg)