Results 1 to 3 of 3
Thread: Efficiently allocating memory
- 10-31-2010, 01:04 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 12
- Rep Power
- 0
Efficiently allocating memory
Hi All,
I have a question about the code snippet below:
In this code, it creates a two dimensional array in every each loop of "for", and therefore that array allocates a memory space. I realized when I was debugging this code, that array allocates a new memory space in every increment in the loop even though previously created array is no longer needed. I thought Java's garbage collector will write over the previously created array's space instead of let the next array continue on upcoming memory space. If I have a limited memory in my computer, I believe that it will make a problem. Am I right? ThanksJava Code:for (int i = 0; i < POPULATION_SIZE; i++) { int[][] gene = new int[data.getNumberOfExams()][data.slotNumber]; generateChromosome(gene); chromosomePopulation.add(gene); }
- 11-01-2010, 11:41 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
The problem has nothing to do with the creation of that array, since you presumably want to keep POPULATION_SIZE arrays that are stored in the chromosomePopulation List(?). If you were so constrained on memory that you can't hold that number of arrays then there's nothing you can do with this bit of code to fix that.
- 11-01-2010, 11:52 AM #3
This depends on what generateChromosone and chromosonePopulation.add (gene) does.
If they store the array, then as tolls says theres nothing you can do.
If they extract bits of the array and then dont use it anymore (or store any references to it) then you dont need to do anything - the garbage collector is smart enough to know to get rid of it.
In any case you dont really need to do anything with the "int [][]gene" variable itself - the garbage collector will get rid of it (because it becomes out of scope at the end of the for loop)
Similar Threads
-
Copying large files efficiently
By bayan in forum New To JavaReplies: 2Last Post: 10-27-2010, 05:01 PM -
repainting more efficiently
By imorio in forum AWT / SwingReplies: 2Last Post: 08-24-2010, 04:24 AM -
question about painting efficiently
By gib65 in forum AWT / SwingReplies: 16Last Post: 07-04-2010, 08:18 PM -
How can I do this more efficiently ?
By chucklesotoole in forum New To JavaReplies: 5Last Post: 06-04-2010, 02:28 PM -
Non Allocating String Buffer
By chrisdb89 in forum New To JavaReplies: 5Last Post: 10-25-2008, 06:57 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks