What is additional memory cost of Tuple[Int, Int] ie. (1, 2) over two Ints without Tuple ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
JVM overheads tend to be 16 to 24 bytes per object (32-bit and 64-bit respectively, though compressed pointers can make the latter smaller).
Tuple2is specialized onInt, which means it stores the values in fields, so you have 8 bytes for two ints as compared to 8+16=24 or 8+24=32 for (1,2). If you use a similar non-specialized collection (or use Tuple2 for something that it is not specialized on, likeChar), then you need pointers to objects, and you may need the objects depending on whether they can be pre-allocated (arbitrary integers, no; arbitrary bytes, yes; arbitrary chars, maybe). If yes, then you just need the pointer and it’s 8+16=24 or 16+24=40 bytes; if no, you need three objects, so it’s 16+8+2*(16+4) = 64 and 24+16+2*(24+4) = 96 respectively.Bottom line: objects use a lot more memory than primitive types, usually 3-4x, but sometimes over 10x. If you are short on memory, pack as much as you can into arrays. For example:
Bad for memory usage:
Good for memory usage:
If you’re really tight on memory, you can then write iterators and other wrappers that let you index things as if they were an array of tuples instead of a tuple of arrays. It’s a bit of a pain, but if you’re really short on memory it can be worth it.