Consider this situation. I am having a thousand int variables in my program. We know that an int variable occupies 2 bytes of memory space in Java. So, the total amount of space occupied by my variables would be 2000 bytes. But, the problem is that, the value in every variable occupies only half of the space it has. So, the actual required space used would be 1000 bytes, which means that 1000 bytes goes waste. How can I address this problem? Is there a way to do address this problem in Java?
5 Answers
Your data fits in a byte, so just use byte instead of int (which is 4 bytes).
3 Comments
byte type too.Checking the space required by values every time they are stored would mean creating an additional useless overhead.
If you know which size your values will be, it is up to you to properly chose their type while developing. If you don't know how much space your value might occupy, you have to choose the largest possible one.
During runtime, if a value is an int for example, this means it can possibly be between -2^31 and (2^31)-1. If you know this value will never be this size, you can use smaller sized primitives such as byte or short.
We could check and allocate the exact amount of memory required for each element in memory but it would take more time. On the contrary, using a fixed amount of memory takes up more space evidently but is faster, this is how Java works. Unfortunately, we can't do without a compromise.
1 Comment
- An int in Java is 32 bit (4 bytes) long
- The value held in the int does not affect the int size in memory, e.g. if your int has the value 0x55
So, in order to occupy 2000 bytes in memory, you will need 500 ints, if you create an int array, there will be overhead of the array itself, so it will take up more memory.
Comments
intdata type is a 32-bit signed two's complement integer. And hence occupies 4 bytes of memory. Forint:- Minimum value is - 2,147,483,648.(-2^31)
- Maximum value is 2,147,483,647(inclusive).(2^31 -1)
If your values are not going to be so large then you can use either byte or short as per your requirement.
Bytedata type is an 8-bit signed two's complement integer.- Minimum value is -128 (-2^7)
- Maximum value is 127 (inclusive)(2^7 -1)
Shortdata type is a 16-bit signed two's complement integer.- Minimum value is -32,768 (-2^15)
- Maximum value is 32,767 (inclusive) (2^15 -1)
You could also use a linked list if you really are not sure on how many elements are going to be in a large list.
The principal benefit of a linked list over a conventional array is that the list elements can easily be inserted or removed without reallocation or reorganization of the entire structure because the data items need not be stored continuously in memory or on disk, while an array has to be declared in the source code, before compiling and running the program. Linked lists allow insertion and removal of nodes at any point in the list, and can do so with a constant number of operations if the link previous to the link being added or removed is maintained during list traversal.
In general use a linked list for int data type or heavier data types as they actually incur a per-element overhead of at least 1 pointer size, so if your elements are small, you're actually worse off.
4 Comments
byte, you'll waste 90-95% of the space. If however your element is an 8 kbyte buffer, the overhead is only 0.1-0.2%, regardless of the list size.byte[], If you use an ArrayList<Byte> this will use about 4 bytes per entry or 4 KB and if you use a LinkedList it will use about 24 bytes per element.See the Oracle docs:
int: By default, the int data type is a 32-bit signed two's complement integer
So it means that int occupies 4 bytes in memory.
And
byte: The byte data type is an 8-bit signed two's complement integer
So in your case you can change your datatype to byte instead of int.
We know that an int variable occupies 2 bytes of memory space in Java.I know it doesn't :)longs for storing the day of month but at the same time, don't use abyteto store temperatures because it is quite possible that something will be too hot or cold. Beyond that, there are variable-sized data structures but they come at a cost: they usually have a fixed header, which itself occupies space (and this makes them impractical for small amounts of data).