This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have a question about multi-dimensional arrays... In Java, an array of more than one dimension is merely an array referencing more arrays... or a list of arrays. And in Java, arrays are instantiated as objects, not mere data structures. So the statement:
is really instantiating 5 objects? If so, that is really interesting... and it gives me a question about how Java's garbage collection works. Normally, after I reference an object for the last time in a program, it is available for garbage collection. I played around with this by making a stupid program:
So I was using the finalize() to show me which # was being destroyed when. It's pretty random (!!), and all I know about garbage collection so far is what I learned from this program. So I am wondering, if each array is an object, and each multidimensional array is a list of arrays, after I reference somearr[x] for the last time, is somearr[x] still eating up memory, or can it, individually as a member of that set of array objects, be trashed by the garbage collector? Or does the garbage collector kill somearr as a group all at once, always? If it can trash the members of a multidimensional array individually, is there some variant on my primitive code that can show me when they are getting trashed? Thanks again.
Don't sweat the petty stuff, just pet the sweaty stuff.
Hi Ender, It seems to me that you are fighting a losing battle. The JVM doesn't specifically state whether garbage collection is run at all during program executions, just that it will run in low memory situations. If your program is the only application running in the JVM then the garbage collector might not even run at all or, as it seems in your case, sometimes you get lucky and it does (seems random though!). The JVM explicitly states that the program can NOT directly invoke garbage collection it can only hint with System.gc() so that won't help your cause either. Another thing to keep in mind is that garbage collection is vendor specific. That means that the JVM doesn't specify how it will be done, just that it should be executed in certain situations. Don't spend to much time dwelling on it unless you are going to learn how it works on any system where you might want to run a java program. The idea of garbage collection in java was that the programmer shouldn't have to get bogged down in the details ... just feel secure in knowing that something is helping our applications/applets free up some memory. Regards, Manfred.
Well, garbage collection really depends on how the programmer of the JVM you are using programmed it... other people on here probably know more about what goes on in the background than me... but one thing that Java does specify is that an object becomes eligible for garbage collection only when there are no active references to it... so normally a multidimensional array structure as you mentioned would pretty much all be garbage collected at the same time, unless you went in and specifically changed the reference to something else... then the old value and reference would be garbage collected.
I think I'll illustrate that last concept...
-Nate [This message has been edited by Nathan Pruett (edited June 05, 2001).]
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Of course Nathan is correct, but let's be muddy it up some. When you say somearr[x] , you are saying that there is a variable named somearr that holds one reference to one object. That object has a set of 34 data fields in it. Each of those data fields in turn holds one reference to one object that has 11 data fields in it. Each of those 11 data fields has a reference to one object which has x data fields in it, etc. Through this whole thing there is only ONE variable involved. 1st - a note. This is NOT a multi-dimensional array. There is a technical difference between an array of arrays and a multi-dimensional array, and some languages support BOTH. There is a nuance of difference between an array of arrays like java uses that is expressed (ex.) = new String; // note that this is 2 arrays each holding 5 And a true multi-dimensional array which would be expressed in some other language; = new String(2,5); //this is one array that holds 10 in 2X5 format That being said, I have to confess to mis-using this terminology on a regular basis myself, so I guess that I should start watching it . 2nd back to the original question. When you set somearr = null, the WHOLE shebang is trash, because it is ALL unreachable. That part is easy. If you set any of the data fields which are the physical makeup of an array (as shown by Nathan) to null, the data field that used to point to an object now points to nothing, so the OBJECT that used to be referenced is now AVAILABLE for the gc. Whether or not it gets cleaned up in a timely manner, sooner or later it WILL get cleanup up, at worst when the JVM goes down it will clean up on the way out.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Originally posted by Ender Everett: > I have a question about multi-dimensional arrays... > So the statement: > int list = new int > is really instantiating 5 objects? Just wanted to point out that the above gives you 1 three-element array. Each of those 3 elements is a four-element array. That gives you four arrays/objects, not five. Did you know that you can also make asymmetrical arrays? Junilu
Cindy, Nathan, Manfred: OK, I'm pretty clear on that now. You answered my questions exactly. I guess I didn't phrase it well enough at first, though (I'll learn). Junilu: Thanks for pointing that out. I was thinking in pig-Latin. int list = new int; gives me four objects, and int list = new int; gives me five... right? Hmmm... interesting. Is there any programming standard regarding the order of listing? no, I'm sure that's not the right term: "listing". I mean the order of the numbers in the s. As in, it is standard to place them least to greatest or greatest to least? I can imagine a negative impact on tightly operating systems (which probably wouldn't ever use JVM anyway...but this is hypothetical) where instantiating too many (or maybe the reverse, since the gc would have less to play with?) objects could be a problem, and reversing the order that you list the field sizes could affect this. So is there any standard? Even some old hangover from C or something? Sorry to keep bugging you with questions. And to Manfred, I know I have no real control over garbage collection and it is a somewhat worthless question, but I am intensely curious how all of this really works... if for no other reason than to have a clearer mental concept of what my programs are really making the computer do.
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