Shop OBEX P1 Docs P2 Docs Learn Events
Memory leak in Integer.toString() — Parallax Forums

Memory leak in Integer.toString()

nrk752nrk752 Posts: 8
edited 2006-05-22 13:36 in General Discussion
I have noticed that whenever I use Integer.toString() the available memory decreases by·just under 100 bytes.· After a minute of using it repeatedly my program will crash with an out of memory error.· I have had this problem in multiple programs, and using debug messages to report the free memory makes it quite clear that the memory is disappearing right when Integer.toString() is used.· Anyone know of a fix for this?· Can I somehow release the memory that is being eaten up?

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-19 18:14
    The Integer.toString() calls String.valueOf() that calls new String().
    So everytime a new String is created. Since the javelin lacks garbage collection
    you run out of memory if you call Integer.toString frequently.
    The same applies to using + to concatenate strings.

    The best option is to use the Format class.
    Use the method bprintf() or sprintf() to assemble a string in memory, eg
    char[noparse]/noparse numstring = new char[noparse][[/noparse]7]; //allow for -xxxxx plus closing 0
    Format.sprintf(numstring,"%d",someIntegerValue);
    Or use printf() to print directly to the JIDE message window:
    Format.printf("%d",someIntegerValue);

    The Format class is here:
    http://groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/util/text/

    You also find a manual there. Format also allows you to print binary and hex values,
    and parse strings to convert numbers (bscanf).

    regards peter
    ·
  • nrk752nrk752 Posts: 8
    edited 2006-05-22 13:36
    Thank you, I was able to fix my code using the Format class.
Sign In or Register to comment.