March 2019
Intermediate to advanced
242 pages
6h 21m
English
The following sample code allocates an image array of size 999999 and loads images to it. Then, the code retrieves the image bytes and stores them to a byte array of size 999999.
On a heap size of 3,044 MB and region size of 1 MB, this code will force full G1 GC and eventually shut down the JVM with OutOfMemoryError:
import java.io.*; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; public class TriggerG1FullGC { final BufferedImage[] images = new BufferedImage[999999]; final byte[][] imgByte = new byte[999999][]; public TriggerG1FullGC() throws Exception { for (int i = 0; i < 999999; i++) { images[i] = ImageIO.read(new File("img.jpg")); } System.out.println("Images read"); for (int i = 0; i < 999999; i++) { ...Read now
Unlock full access