This week's book giveaway is in the Design forum. We're giving away four copies of Building Microservices and have Sam Newman on-line! See this thread for details.

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 ]

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

posted

0

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.

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.

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

posted

0

[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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com