Results 1 to 3 of 3
  1. #1
    reis3k is offline Member
    Join Date
    Aug 2009
    Posts
    12
    Rep Power
    0

    Default Efficiently allocating memory

    Hi All,

    I have a question about the code snippet below:

    Java Code:
            for (int i = 0; i < POPULATION_SIZE; i++) {
    
                int[][] gene = new int[data.getNumberOfExams()][data.slotNumber];
                generateChromosome(gene);
                chromosomePopulation.add(gene);
               
            }
    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? Thanks

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    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.

  3. #3
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    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

  1. Copying large files efficiently
    By bayan in forum New To Java
    Replies: 2
    Last Post: 10-27-2010, 05:01 PM
  2. repainting more efficiently
    By imorio in forum AWT / Swing
    Replies: 2
    Last Post: 08-24-2010, 04:24 AM
  3. question about painting efficiently
    By gib65 in forum AWT / Swing
    Replies: 16
    Last Post: 07-04-2010, 08:18 PM
  4. How can I do this more efficiently ?
    By chucklesotoole in forum New To Java
    Replies: 5
    Last Post: 06-04-2010, 02:28 PM
  5. Non Allocating String Buffer
    By chrisdb89 in forum New To Java
    Replies: 5
    Last Post: 10-25-2008, 06:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •