how to remove an array without using anymore memory

edited September 2017 in Questions about Code

I have a problem... I have to create an array that is pretty large,but used only rarely so I decided to save it to a file since it is predefined (but changes every startup so I have to check every startup again). but for some reason it still uses an extrem amount of memory eventhough I created a local array in setup. So how do I "remove" the array properly ?

void setup(){ 
  String[] hi = new String[10];
  for (int jf = 0; jf < hi.length; jf++){ 
    hi[jf] = 5 + 5 - jf;
    // something that needs really much memory and is difficult to calculate (instead of that) 
  } 
  saveStrings("test.txt",hi);
}

void draw(){ 
  if (key == 'g'){
    String[] temp = loadStrings("test.txt"); 
    printArray(temp); 
  } 
}

so how do I properly unload the String ,because when I test my other program it uses memory up to 750k and it keeps staying that high, but when I remove the large array from the program's code it works fine using 30k memory.

Thanks in advance :D

Tagged:

Answers

  • edit post, highlight code and press ctrl-o to format.

    leave a blank line before and after.

    CHECK WHAT YOU'VE POSTED.

  • edited September 2017
    void setup(){ 
      String[] hi = new String[10];
      for (jf = 0; jf < hi.length; jf++){ 
        hi[jf] = 5 + 5 - jf;
        // something that needs really much memory and is difficult to calculate (instead of that) 
      } 
      saveStrings("test.txt",hi);
    }
    
    void draw(){ 
      if (key == "g"){
        String[] temp = loadStrings("test.txt"); 
        printArray(temp); 
      } 
    }
    
  • for some reason (i dont know why) it wont format easier to read sorry for that

  • edited September 2017

    @sLucas --

    If you paste this code into PDE and press "Run", it

    1. highlights a specific line in orange
    2. prints an error message in a big red bar
    3. prints several detailed error messages in the "Errors" tab at the bottom of the screen.

    What line is highlighted? What is the error? Why might it say that? Is there something that you should normally do that you have not done here?

  • edited September 2017

    excuse me

    void setup(){ 
      String[] hi = new String[10];
      for (int jf = 0; jf < hi.length; jf++){ 
        hi[jf] = str(5 + 5 - jf);
        // something that needs really much memory and is difficult to calculate (instead of that) 
      } 
      saveStrings("test.txt",hi);
    }
    
    void draw(){ 
      if (key == 'g'){
        String[] temp = loadStrings("test.txt"); 
        printArray(temp); 
      } 
    }
    

    this should work now but i need some help on the question i mentioned at the beginning

  • edited September 2017

    Maybe you guys dont get my point, because my english isnt that great since i m not native, so I'll try and explain it again

    the posted code is just simplified to show you an example of my problem the real code is complex but I made some search for problem research and can say that it will be solved if someone could tell me a way for removing the current array out of the ram because it is saved in setup and even thereafter eventhough it is just a local variable, so I was wondering how to remove an array fully out of memory because it is only temporary

    if you are interested in how I figured out that the still saved array is still saved and uses tons of memory well I tred running this code

    void setup() { 
      String[] hi = new String[9999999];
      for (int jf = 0; jf < hi.length; jf++) { 
        hi[jf] = str(1);
      }
    }
    
    void draw(){
      println("check out on taskmngr how much memory is being used by this sketch");
    }
    

    and I tried running this without the array.

    void setup() { 
    }
    
    void draw() {
      println("check out on taskmngr how much memory is being used by this sketch");
    }
    

    well as you can see example 1 uses tons of memory eventhough the array is temporary and not run twice and not needed anymore. So what I want to do is that the sketch runs as smoothly as the second does. what I already tried and didnt change anything at all is in terms of memory usage:

    void setup() { 
      String[] hi = new String[9999999];
      for (int jf = 0; jf < hi.length; jf++) { 
        hi[jf] = str(1);
      }
      hi = new String[0];
    }
    

    as well as:

    void setup() { 
      String[] hi = new String[9999999];
      for (int jf = 0; jf < hi.length; jf++) { 
        hi[jf] = str(1);
      }
      hi = null;
    }
    
  • Garbage Collection:

    http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

    in short, memory is not freed immediately.

    uses memory up to 750k

    750k is NOTHING these days.

  • edited September 2017

    ok just for all those who ran into the same problem an easy and 100% effective way of doing this is:

    -create your main sketch

    -create another sketch with the memory consuming calculations into another

    -add in setup() launch(sketchPath("YOUR.BAT")); ofcourse the batch file has to open the second sketch

    -in the second sketch you have to save the information as string into a file saveStrings("LOCATIONOFDDATA.txt");

    -then put close(); at the end of the second sketch();

    -back in the main sketch you have to write

    data[] = loadStrings("LOCATIONOFDDATA.txt");

    then you are done and have dodged this stupid garbage collector system

    I saved with this system 0.65gb ram = INTERGALACTIC PERFOMANCE BOOST

    have a nice day sLucas :D

Sign In or Register to comment.