2

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?

12
  • 6
    We know that an int variable occupies 2 bytes of memory space in Java. I know it doesn't :) Commented Oct 8, 2015 at 14:38
  • Also, what is the original problem you're trying to solve here? (Not what you think the solution is, but the underlying computing problem.) Commented Oct 8, 2015 at 14:39
  • can u explain a bit clearly @biziclop ? :-) Commented Oct 8, 2015 at 14:41
  • 1
    Time vs. Space. There are collections that allow you to tune for the latter. It depends on what you want to do. Be wary of premature optimizations. Commented Oct 8, 2015 at 14:46
  • 1
    @KonakanchiSaketh Okay, that's different then. In general, you should size data fields sensibly: don't use longs for storing the day of month but at the same time, don't use a byte to 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). Commented Oct 8, 2015 at 15:00

5 Answers 5

2

Your data fits in a byte, so just use byte instead of int (which is 4 bytes).

Sign up to request clarification or add additional context in comments.

3 Comments

As a side comment, using int occupies 4000 bytes = 4KB. Is this really even an issue as it seems to be a negligible amount of memory.
AFAIK most JVMs use 32 bit for the byte type too.
@fabian Individually, yes. But multiple byte fields (or byte arrays) can be packed.
1

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

yes true brother. the final verdict says that, "we cant do without a compromise..." :-)
1
  1. An int in Java is 32 bit (4 bytes) long
  2. 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

1
  1. int data type is a 32-bit signed two's complement integer. And hence occupies 4 bytes of memory. For int:
    • 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.

  1. Byte data type is an 8-bit signed two's complement integer.

    • Minimum value is -128 (-2^7)
    • Maximum value is 127 (inclusive)(2^7 -1)
  2. Short data 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

However it's worth mentioning that linked lists also incur a per-element overhead of at least 1 pointer size, so if your elements are small, you're actually worse off.
The emphasis should be on element size though. A doubly linked list will cost you 8/16 bytes per element, so if your element is a 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.
This will only use about 1 KB (for 1000 bytes) if you use a 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.
Updated my answer. Thanks for your valuable insights biziclop & @Peter Lawrey
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.