aspose file tools*
The moose likes Performance and the fly likes Array of Nulls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Array of Nulls" Watch "Array of Nulls" New topic
Author

Array of Nulls

Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

This isn't a homework question but for clearity I think I'll phrase it like one since I'm teaching this year...

Let n be an abitrary positive integer value (assume at least 100)

Let A be the memory allocated by the following single command:
Object[] x = new Object[n];

Let B be the memory allocated by the following single command:
double[] y = new double[n];

What is the approximate value of A/B? Or, alternatively which is bigger and by what percentage?

For my situation, I have a large n^k dimensional matrix that uses a ton of space and I'm trying to determine if there would like be space saved using either implementation.
[ September 13, 2006: Message edited by: Scott Selikoff ]

My Blog: Down Home Country Coding with Scott Selikoff
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, it's implementation-dependent, but in general new Object[n] should take 4*n bytes (each reference is a 32-bit value), and new double[n] should take 8*n bytes (each double being a 64-bit value). So looking just at the arrays, the double[] takes twice as much space. However the Object[] will probably need some actual Objects too (Doubles perhaps?), and depending on how many different objects you have, there's a good chance that the Object[] version will end up taking more total memory. Unless the references are mostly null or mostly to shared instances.


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
And in the event you do have a lot of nulls in there, you might want to use some sort of sparse matrix implementation, for greater memory savings.
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

Actually I am working with sparse matrixes, so perhaps I should rethink the construction. I thought using nulls (and then replacing with Double) would save space and it sounds like it would, but there are better ways to implement sparse matrices that I'm thinking I will try.

Thanks for the information though (I suspected 4*n but wasn't sure). Interestingly, if I used float[n] I'm guessing this would be the same as Object[n] plus the space for each object.
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

As an aside, do you know of good sparse matrix implementions and/or one that used prefix matching techniques?
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

Problem solved.... using arrays with null elements instead of a fully initialized one saved insane amounts of space.

Part of the reason I was having trouble is because I'm creating a dynamic matrix on the fly using reflection so I had to modify the reflection classes used to construct the array (so don't construct nxnxn, construct nx3, and put [][] pointers in the columns initially set to null).
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Scott]: As an aside, do you know of good sparse matrix implementions and/or one that used prefix matching techniques?

Never used it myself, but I heard good things about Colt back when it came out, years ago. (Not about sparse matrices in particular, but the library in general.) Haven't heard much since - I haven't done much resembling real math in some time. No idea about prefix matching techniques in matrices, sorry. Though it sounds like you're in good shape now anyway...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array of Nulls