Where would main be inlined to? And why? It’s only called once. Also, vals pointer is still on the stack so it is live so no chance for it to be collected.
>From my run… 9421512 [GC (System.gc()) 9200K->1314K(251392K), 0.0025157 secs] [Full GC (System.gc()) 1314K->1160K(251392K), 0.0080973 secs] 1188704 405215328 [GC (System.gc()) 395718K->392017K(642048K), 0.0040519 secs] [Full GC (System.gc()) 392017K->391536K(642048K), 0.0641594 secs] 402275264 [GC (System.gc()) 394157K->391568K(642048K), 0.0048252 secs] [Full GC (System.gc()) 391568K->391501K(642048K), 0.1082497 secs] 400897776 [GC (System.gc()) 392812K->391533K(642048K), 0.0053044 secs] [Full GC (System.gc()) 391533K->391501K(642048K), 0.0086243 secs] 400897776 [GC (System.gc()) 392812K->391533K(642048K), 0.0040812 secs] [Full GC (System.gc()) 391533K->391501K(642048K), 0.0091088 secs] 400897776 [GC (System.gc()) 392812K->391533K(642048K), 0.0037178 secs] [Full GC (System.gc()) 391533K->391501K(642048K), 0.0067113 secs] 400897776 [GC (System.gc()) 392812K->391533K(642048K), 0.0036010 secs] [Full GC (System.gc()) 391533K->391501K(642048K), 0.0076227 secs] Looks pretty much expected from my POV. Kind regards, Kirk > On Jan 25, 2018, at 3:44 AM, 'Carl Mastrangelo' via mechanical-sympathy > <[email protected]> wrote: > > Consider the following code: > > public class Test { > static volatile Integer discard; > public static void main(String [] args) throws InterruptedException { > printMemory(); > System.gc(); > printMemory(); > int iterations = 1000; > int[] vals = new int[100_000_000]; > while (args.length == 0) { > printMemory(); > System.gc(); > Thread.sleep(200); > discard = iterations++; > } > } > > private static void printMemory() { > System.out.println(Runtime.getRuntime().totalMemory() - > Runtime.getRuntime().freeMemory()); > } > } > > I am surprised to see the memory used by this code starts at about 200MB, > goes up to 600MB, but never seems to go back down. The large int array > accounts for the memory usage jump, but it never seems to be garbage > collected. Why? The variable is never read after it is allocated. It > cannot be reordered by the compiler to be after the while loop, because I can > see the memory jump. I am intentionally allocating memory in the loop by > boxing the integer. Enabling +PrintCompilation and PrintInlining never shows > main() being inlined; perhaps it is not being compiled? > > -- > You received this message because you are subscribed to the Google Groups > "mechanical-sympathy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected] > <mailto:[email protected]>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. -- You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
