Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Memory allocation

Reply
Thread Tools

Memory allocation

 
 
Momo
Guest
Posts: n/a
 
      06-05-2007
Hi,

Something was puzzling me about when I will run out of memory. I have
a simple program, that does nothing but take size as the input, and
then allocates a boolean array with x=new boolean[size]. I throw an
OutOfMemoryError when size is greater than about 6*10^7.

If I then change the program to allocate a boolean array with y=new
boolean[size][4], I get the Error when size is greater than about
3*10^6. I would have thought that double-scripting the array to be
length 4, I would make the maximum value of size roughly 1/4th as
large. But instead it changes by a factor of 20. Why such a big
change?

This is not an actual problem for me, since I just increased the size
of the java heap space. But I'd like to understand where the factor of
20 comes from.

Momo

 
Reply With Quote
 
 
 
 
Kai Schwebke
Guest
Posts: n/a
 
      06-05-2007
Momo wrote:
> large. But instead it changes by a factor of 20. Why such a big
> change?


boolean[n] consumes (most likely) c + n bytes.

boolean[n][4] is one array of n references to n arrays of 4 bytes.

Arrays are instances of class Array and consume (most likely)
from 12 to 16 bytes{1}. The reference may need additional 4 bytes.
All of this n times, leading to a quite high overhead.

You may consider using a bitset and a calculated index
(e.g. index = 4*a + b) to reduce the required heap space
to c + n/8 bytes.


Kai

{1} see http://martin.nobilitas.com/java/sizeof.html
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      06-05-2007
Kai Schwebke wrote:
> Momo wrote:
>> large. But instead it changes by a factor of 20. Why such a big
>> change?

>
> boolean[n] consumes (most likely) c + n bytes.
>
> boolean[n][4] is one array of n references to n arrays of 4 bytes.
>
> Arrays are instances of class Array and consume (most likely)
> from 12 to 16 bytes{1}. The reference may need additional 4 bytes.
> All of this n times, leading to a quite high overhead.
>
> You may consider using a bitset and a calculated index
> (e.g. index = 4*a + b) to reduce the required heap space
> to c + n/8 bytes.


Transposing would be simpler, and get most of the memory reduction.
boolean[4][n] is one array of four references to four arrays of n booleans.

Patricia
 
Reply With Quote
 
Momo
Guest
Posts: n/a
 
      06-05-2007
On Jun 5, 1:37 pm, Kai Schwebke <(E-Mail Removed)> wrote:
> Momo wrote:
> > large. But instead it changes by a factor of 20. Why such a big
> > change?

>
> boolean[n] consumes (most likely) c + n bytes.
>
> boolean[n][4] is one array of n references to n arrays of 4 bytes.
>
> Arrays are instances of class Array and consume (most likely)
> from 12 to 16 bytes{1}. The reference may need additional 4 bytes.
> All of this n times, leading to a quite high overhead.


Thank you!

> You may consider using a bitset and a calculated index
> (e.g. index = 4*a + b) to reduce the required heap space
> to c + n/8 bytes.


I changed it to four Bitsets a few days ago, so it turns out that I
fixed it, I just didn't know why I fixed it.

Momo


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
memory allocation and freeing memory Rodrigo Dominguez C Programming 11 06-14-2005 11:54 PM
Dynamic memory allocation and memory leak... s.subbarayan C Programming 10 03-22-2005 02:48 PM



Advertisments