Do memory footprint yourself u will understand
http://java-performance.info/overview-of-memory-saving-techniques-java/
Each object instance holds a pointer to it's class object, this is exactly the extra memory for each wrapper object. I guess, it's system dependent, and it would be 8 bytes on 64 bit machine. It means that each "Integer" object, for example, takes 4b+8b=12b of memory in heap while of primitive "integer" that takes 4b on stack or in heap (this difference is mostly irrelevant in java in terms of performance). For booleans, if they are represented as 1b what is usually the case, it's 1b+8b=9b that is 9 times more memory. However, for booleans usually there's no need to create new instance, just use Boolean.TRUE or Boolean.FALSE. Same happens for some set of other primitive common values (like 0-256): if they can be determined at compile time compiler uses statically defined value. Autoboxing works the same way, if I remember correctly. As you see using objects has significant impact on memory it takes. But in most cases it doesn't really matter much. Just check heap dump of your application and calculate how much would grow memory footprint if you were using wrapper classes. I bet not much. Unless you're developing for embedded device you just shouldn't care too much. Even for most cases in android development. However, you should consider this extra memory footprint if you develop something that is very restrictive in memory or very number manipulation extensive.
Обсуждают сегодня