...
Code Block | ||
---|---|---|
| ||
class MemoryLeak { public static void main(String[] args) throws IOException { ObjectOutputStream out = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream("ser.dat"))); for (int i = 10; i < 1024; i++) { byte[] arr = new byte[100 * 1024]; Arrays.fill(arr, (byte) i); out.writeObject(arr); } out.close(); } } |
...
Code Block | ||
---|---|---|
| ||
class NoMemoryLeak { public static void main(String[] args) throws IOException { ObjectOutputStream out = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream("ser.dat"))); for (int i = 10; i < 1024; i++) { byte[] arr = new byte[100 * 1024]; Arrays.fill(arr, (byte) i); out.writeObject(arr); out.reset(); // reset the stream } out.close(); } } |
...