You can try combining these two sources.
MemoryRecoveryTest.java
Makes recovery attempts with OutOfMemoryError .
import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.JProgressBar; import javax.swing.JOptionPane; import javax.swing.JDialog; import javax.swing.Timer; import javax.swing.border.EmptyBorder; import java.util.ArrayList; public class MemoryRecoveryTest { public static void main(String[] args) {
IWantToBeBig.java
Runs the Process with the specified memory size.
import java.awt.EventQueue; import javax.swing.JOptionPane; import java.io.File; class IWantToBeBig { public static void main(String[] args) throws Exception { if (args.length==0) { ProcessBuilder pb = new ProcessBuilder( "java", "-jar", "-Xmx512m", "big.jar", "anArgument" ); pb.directory(new File(".")); Process process = pb.start(); process.waitFor(); System.out.println("Exit value: " + process.exitValue()); } else { Runnable r = new Runnable() { public void run() { JOptionPane.showMessageDialog( null, "Max Memory: " + Runtime.getRuntime().maxMemory() + " bytes."); } }; EventQueue.invokeLater(r); } } }
source share