What matters from a memory perspective is to determine when the object becomes eligible for garbage collection.
In your case it makes no difference: as soon as you write wbuffer = new byte[n];
the previous byte array becomes unreachable and therefore eligible for GC.
Reusing the same array would improve memory footprint in which case you need to declare it before the loop.
And GC will run when necessary. Apart from very specific use cases, it is generally a bad idea to call System.gc();
- it can actually have a negative impact on performance.