Shop OBEX P1 Docs P2 Docs Learn Events
Garbage Collecting — Parallax Forums

Garbage Collecting

atekippeatekippe Posts: 15
edited 2005-05-06 15:24 in General Discussion
Hello,

I know that the javelin has no means of garbage collecting but does anyone know where I can find information on some solutions to avoid this problem?· No matter what we do in our codes, it errors out·or runs very poorly because of data accumulation. Any help would be appreciated.

atekippe
ISU

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-04-13 18:53
    Do not use the new keyword for local variables, only use new for global (static) variables

    and for global class variables. Single char,byte,short and int are always released once

    a method returns. Also try to avoid using String operations (especially adding).

    Or use a heap to reclaim space once a variable is no longer needed.

    http://groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/peripheral/memory/



    regards peter
  • atekippeatekippe Posts: 15
    edited 2005-05-05 20:33
    Hey Peter,

    First thank you for your help earlier.· I've been away from the office for a while and wasn't able to get back on the forums to thank you.· Could you elaborate a little more about how·values·are·released when returning from a method?· I've tried a few little tests to check this and I do not see it being released.· What I'm doing is calling a method, setting an integer and character value, printing those·characters to the screen, then returning to the main program and printing them again.· From what I understand the second time it prints it should be different because the value was released but I'm getting the same thing. Perhaps I'm just missing something simple here.· Thanks for all of your help

    atekippe
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-05-05 22:51
    Hi,

    Globals are declared outside a method and retain their values, whereas locals are declared

    inside the method.

    example:

    public class test {

    · static int p = 1; //global variable, retains its value

    · static void sample() {

    ··· int p = 2; //local variable, lost if scope ends

    · }

    · static void main() {

    ··· sample(); //call sample() that sets the local p to value 2

    ··· System.out.println(p); //will print 1, the value of the global p

    · }

    }

    More general, a variable is only known inside the block it is declared.

    Example:

    for (int p=0; p<10; p++) {

    · //p is only known inside the for loop

    }

    remember, only scalar variables (char, short, byte and int) are automatically released when

    exiting a block.

    regards peter
  • atekippeatekippe Posts: 15
    edited 2005-05-06 15:24
    Thank you very much peter. That cleared it up.

    atekippe
Sign In or Register to comment.