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?

RisingHerc
  • 694
  • 1
  • 9
  • 26
  • 6
    `We know that an int variable occupies 2 bytes of memory space in Java.` I know it doesn't :) – biziclop Oct 08 '15 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.) – biziclop Oct 08 '15 at 14:39
  • can u explain a bit clearly @biziclop ? :-) – RisingHerc Oct 08 '15 at 14:41
  • It sounds like you need to create your own aggregate data type that you index into. But we don't know what you want to do that an int array can't satisfy. But if you know you are collecting 1000 shorts or something, then you can aggregate them and handle the indexing yourself. –  Oct 08 '15 at 14:42
  • do we have a "stretchable" data type in Java? – RisingHerc Oct 08 '15 at 14:42
  • an `int` array has fixed amount of memory allocated, right? What about the extra space that is unused by the values stored? – RisingHerc Oct 08 '15 at 14:45
  • @KonakanchiSaketh There must be a reason why you think "1000 bytes going to waste" is a problem. Because in most cases it isn't, certainly not big enough a problem to start hand-rolling data types. But there are certain scenarios when this is necessary, or where another solution is even better. We need to know what your scenario is, who creates this data structure, what is it used for and why saving space is critical. – biziclop Oct 08 '15 at 14:46
  • 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. –  Oct 08 '15 at 14:46
  • I am really sorry. I am mistaken. `int` in Java occupies 4 bytes – RisingHerc Oct 08 '15 at 14:50
  • I have posted this question just to gain more knowledge in Java. Actually, there is no scenario or project in which I am working on, which led to this problem :-) – RisingHerc Oct 08 '15 at 14:52
  • my question is that, 1 kb of memory may be negligible amount of memory for us. but in large apps, when such kind of situations occur many times, wouldn't so much memory be wasted? – RisingHerc Oct 08 '15 at 14:55
  • 1
    @KonakanchiSaketh Okay, that's different then. In general, you should size data fields sensibly: don't use `long`s 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). – biziclop Oct 08 '15 at 15:00

5 Answers5

2

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

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
  • 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. – Jean Logeart Oct 08 '15 at 14:47
  • AFAIK most JVMs use 32 bit for the `byte` type too. – fabian Oct 08 '15 at 14:48
  • 1
    @fabian Individually, yes. But multiple byte fields (or byte arrays) can be [packed](http://stackoverflow.com/questions/229886/size-of-a-byte-in-memory-java). – biziclop Oct 08 '15 at 14:49
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.

MByD
  • 135,866
  • 28
  • 264
  • 277
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.

Riddhesh Sanghvi
  • 1,218
  • 1
  • 12
  • 22
  • 1
    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. – biziclop Oct 08 '15 at 15:01
  • 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. – biziclop Oct 08 '15 at 15:09
  • This will only use about 1 KB (for 1000 bytes) if you use a `byte[]`, If you use an `ArrayList` 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. – Peter Lawrey Oct 08 '15 at 15:16
  • Updated my answer. Thanks for your valuable insights biziclop & @Peter Lawrey – Riddhesh Sanghvi Oct 08 '15 at 15:19
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.

Paul Podgorsek
  • 2,416
  • 3
  • 19
  • 22
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.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331