We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Recrating arrays and garbage collection
Page Index Toggle Pages: 1
Recrating arrays and garbage collection (Read 364 times)
Recrating arrays and garbage collection
Mar 13th, 2008, 3:20pm
 
Hi,

I've built a structure where I initialise the string array with size 1 and expand it later on with append.

But at some point I need to reinitialize the string array as fresh, that means without any data and with initial length which is 1.

At the beginning of my class decleration, the array is initialised as:

String[] somestring = new String[1];

When I want to reinitialize the variable(as it will grow in length and I have conditions that looks for its length), I will just say:

somestring = new String[1];

is this good programming practise? When I say "new String[1]" to the same variable, will the previous be handled by the garbage collector? I mean the resource for it will be freed right?

Or is there any better way to reinitialize an array with arbitrary length to length = 1 with no contents in it?

Thanks
Re: Recrating arrays and garbage collection
Reply #1 - Mar 13th, 2008, 3:29pm
 
Yes, "somestring=new String[1];" will cause the old entries in your array to be marked for deletion by the GC when it next runs.
Page Index Toggle Pages: 1