Wonderful blogs from
http://java-performance.info/!!! List of articles
http://java-performance.com/
Java type memory usage
byte, boolean | 1 byte |
short, char | 2 bytes |
int, float | 4 bytes |
long, double | 8 bytes |
Byte, Boolean | 16 bytes |
Short, Character | 16 bytes |
Integer, Float | 16 bytes |
Long, Double | 24 bytes |
EnumSet , BitSet | 1 bit per value |
EnumMap | 4 bytes (for value, nothing for key) |
ArrayList | 4 bytes (but may be more if ArrayList capacity is seriously more than its size) |
LinkedList | 24 bytes (fixed) |
ArrayDeque | 4 to 8 bytes, 6 bytes on average |
JDK collection | Size | Possible Trove substitution | Size |
HashMap | 32 * SIZE + 4 * CAPACITY bytes | THashMap | 8 * CAPACITY bytes |
HashSet | 32 * SIZE + 4 * CAPACITY bytes | THashSet | 4 * CAPACITY bytes |
LinkedHashMap | 40 * SIZE + 4 * CAPACITY bytes | None | |
LinkedHashSet | 32 * SIZE + 4 * CAPACITY bytes | TLinkedHashSet | 8 * CAPACITY bytes |
TreeMap, TreeSet | 40 * SIZE bytes | None | |
PriorityQueue | 4 * CAPACITY bytes | None |
All Java objects start with 8 bytes containing service information like object class and its identity hash code (returned by System.identityHashCode method). Arrays have 4 more bytes (one int field) containing array length. It looks like all user-written (not JDK classes) have another reference to object Class. These fields are followed by all declared fields.
All objects are aligned by 8 bytes boundary. All primitive fields must be aligned by their size (for example, chars should be aligned by 2 bytes boundary).
Object reference (including any arrays) occupy 4 bytes.
What does it mean for us? In order to get most use of available memory, all object fields must occupy N*8+4 bytes (4, 12, 20, 28 and so on). In this case 100% memory will contain useful data.
Java Boxing Type Caching
Byte, Short, Long | Character | Integer | Float, Double |
From -128 to 127 | From 0 to 127 | From -128 to java.lang.Integer.IntegerCache.high or 127, whichever is bigger | No caching |
Java Performance Tips
Never use exceptions as return code replacement or for any likely to happen events (especially in not IO-bound methods!). Throwing an exception is too expensive - you may experience 100 times slowdown for simple methods.
Throwing an exception in Java is a very slow operation. Expect that throwing an exception costs you something between 100 and 1000 ticks in most cases.
Case Study
No comments:
Post a Comment