• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Memory usage of byte and long variable.

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scenario : 1

Object[] arr = new Object[100];
for (int n=0; n<max; n++) {
arr[n] = new long[10*1024*128];
}

Scenario : 2

Object[] arr = new Object[100];
for (int n=0; n><max; n++) {
arr[n] = new byte[10*1024*128];
}

Scenario 1 crashes the JVM but scenario 2 working fine. Why it is so?

regards
sri
>
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Actual memory usage is implementation dependent -- but it is safe to say that, a byte takes a byte, and a long takes 8 bytes.

Henry
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could anybody explaine what is the question here??
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use the Code button. I presume the >< is a misprint; I have seen it before.
You are using 100 * 10 * 128 * 1024 * 8 (= 1.048576e9) bytes at least if you use longs and 1.31072e8 bytes for bytes, ie just under 1GB for one and just under 128MB for the other. Now you can work out how much memory you have at your disposal . . .
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rakesh Bagaria wrote:could anybody explaine what is the question here??


Exactly what was asked--why does one work, and the other throws an OOME.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's assume arrays have no overhead.

Each byte[]/long[] has 10*1024*128 = 1,310,720 different array values. In scenario 2 each element takes 1 byte, therefore the total is 1,310,720 bytes or 1.25MB. In scenario 1 each element takes not 1 but 8 bytes, therefore the total is 8 * 1,310,720 = 10,485,760 bytes or 10MB.

Now alone this shouldn't cause an OutOfMemoryError. However, you are creating up to 100 (no idea what value max has) of these arrays. For byte[] that is 125MB. For long[] that is 1000MB, nearly 1GB.
 
Ranch Hand
Posts: 152
VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:
Now alone this shouldn't cause an OutOfMemoryError. However, you are creating up to 100 (no idea what value max has) ...



max <= 100 or array out of bounds exception

and it nicely fails w/ 2GB heap max

Why?
 
Matt Cartwright
Ranch Hand
Posts: 152
VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I believe in running code
 
Matt Cartwright
Ranch Hand
Posts: 152
VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just got rid of the IDE (Eclipse), gave it an extra 80m for overhead
and it works on my machine

$ java -Xms1104m -Xmx1104m Scenarios

$ java -version
java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
Java HotSpot(TM) 64-Bit Server VM (build 14.3-b01, mixed mode)

M
 
sriram sundararajan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys,
Sorry for replying it late.
So it is all because of long, since it occupies 8 times more memory than a byte.

 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep.
 
reply
    Bookmark Topic Watch Topic
  • New Topic