This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Memory usage of byte and long variable. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Memory usage of byte and long variable." Watch "Memory usage of byte and long variable." New topic
Author

Memory usage of byte and long variable.

sriram sundararajan
Ranch Hand

Joined: Apr 08, 2003
Posts: 43
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
>
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18875
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rakesh Bagaria
Greenhorn

Joined: Sep 06, 2009
Posts: 14
could anybody explaine what is the question here??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39070
    
  23
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 . . .
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

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

Joined: Aug 25, 2008
Posts: 149



I believe in running code
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

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

Joined: Apr 08, 2003
Posts: 43
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

Yep.
 
GeeCON Prague 2014
 
subject: Memory usage of byte and long variable.