background preloader

Memory

Facebook Twitter

Profiling

Key-value. Pointers. Java theory and practice: Urban performance legends, revisited. Pop quiz: Which language boasts faster raw allocation performance, the Java language, or C/C++? The answer may surprise you -- allocation in modern JVMs is far faster than the best performing malloc implementations. The common code path for new Object() in HotSpot 1.4.2 and later is approximately 10 machine instructions (data provided by Sun; see Resources), whereas the best performing malloc implementations in C require on average between 60 and 100 instructions per call (Detlefs, et. al.; see Resources). And allocation performance is not a trivial component of overall performance -- benchmarks show that many real-world C and C++ programs, such as Perl and Ghostscript, spend 20 to 30 percent of their total execution time in malloc and free -- far more than the allocation and garbage collection overhead of a healthy Java application (Zorn; see Resources).

Go ahead, make a mess Generational garbage collection Listing 1. Behavior of allocator in the presence of a copying collector Conclusion. Java Reference Objects. Java Reference Objects or How I Learned to Stop Worrying and Love OutOfMemoryError I started programming with Java in 2000, after fifteen years with C and C++. I thought myself fairly competent at C-style memory management, using coding practices such as pointer handoffs, and tools such as Purify. I couldn't remember the last time I had a memory leak. So it was some measure of disdain that I approached Java's automatic memory management … and quickly fell in love.

And then I met my first OutOfMemoryError. I can't remember what caused that first error, and I certainly didn't resolve it using reference objects. For a C++ programmer new to Java, the relationship between stack and heap can be hard to grasp. Integer foo = Integer(1); Java, unlike C++, stores all objects on the heap, and requires the new operator to create the object. Public static void foo(String bar) { Integer baz = new Integer(bar); } The diagram below shows the relationship between the heap and stack for this method. Thanks for the memory. The Java heap, where every Java object is allocated, is the area of memory you're most intimately connected with when writing Java applications. The JVM was designed to insulate us from the host machine's peculiarities, so it's natural to think about the heap when you think about memory. You've no doubt encountered a Java heap OutOfMemoryError — caused by an object leak or by not making the heap big enough to store all your data — and have probably learned a few tricks to debug these scenarios.

But as your Java applications handle more data and more concurrent load, you may start to experience OutOfMemoryErrors that can't be fixed using your normal bag of tricks — scenarios in which the errors are thrown even though the Java heap isn't full. When this happens, you need to understand what is going on inside your Java Runtime Environment (JRE). This article is one of two covering the same topic on different platforms. A recap of native memory Hardware limitations Table 1. Figure 1. Wazi. How to Fix Memory Leaks in Java Your pager hasn't been sleeping well. It periodically wakes you up in the middle of the night to tell you that your server is firing off "OutOfMemoryError" messages. Worse still, your significant other forcibly relocated you to the couch and told you not to return until your pager stops buzzing. Sound familiar? If so, you may have a case of memory leak induced insomnia, but fortunately we've got a cure for what ails you.

This tutorial will teach you everything you need to know to ease your suffering, including what memory leaks are, why they happen, and how to diagnose and fix 'em.In this article we'll focus on techniques that will enable you to address memory leaks with any commercial or free/open source memory profiler; we're not here to recommend one tool over another. After all, the most important thing is that you fix the problem and get some rest, not the tool you use to get it done. Before You Start Methodology What Are Memory Leaks? Meat & Potatoes. Java Objects Memory Structure. Update (December 18th, 2008): I've posted here an experimental library that implements Sizeof for Java. One thing about Java that has always bothered me, given my C/C++ roots, is the lack of a way to figure out how much memory is used by an object.

C++ features the sizeof operator, that lets you query the size of primitive types and also the size of objects of a given class. This operator in C and C++ is useful for pointer arithmetic, copying memory around, and IO, for example. Java doesn't have a corresponding operator. In reality, Java doesn't need one. Size of primitive types in Java is defined in the language specification, whereas in C and C++ it depends on the platform. But every Java developer at some point wondered how much memory is used by a Java object.

The first distinction to be made is between shallow size and deep size. Memory layout of classes that have no instance attributes In the Sun JVM, every object (except arrays) has a 2 words header. Here is an example: