Created
May 1, 2014 22:51
-
-
Save skehlet/a1d12bf81f8617a9593e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ckage com.vkg; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class ClassGenerator { | |
private static final int BUFFER = 1024; | |
private List<Class<?>> classList = new ArrayList<>(); | |
public static void main(String[] args) { | |
ClassGenerator classGenerator = new ClassGenerator(); | |
// Load just some class with class loaders until perm gen space fills. | |
while(true) { | |
classGenerator.classLoader(); | |
} | |
} | |
private void classLoader() { | |
ClassLoader classLoader = new ClassLoader() { | |
public Class<?> loadClass(String classNameWithPackage) throws ClassNotFoundException { | |
if(!classNameWithPackage.contains("DummyClass")) { | |
return super.loadClass(classNameWithPackage); | |
} | |
String className = classNameWithPackage.replace('.', '/') + ".class"; | |
byte[] classData = null; | |
InputStream inputStream = null; | |
try { | |
inputStream = getResourceAsStream(className); | |
byte[] buffer = new byte[BUFFER]; | |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | |
int bytesRead = -1; | |
while ((bytesRead = inputStream.read(buffer, 0, BUFFER)) != -1) { | |
outputStream.write(buffer, 0, bytesRead); | |
} | |
classData = outputStream.toByteArray(); | |
} | |
catch (IOException e) { | |
e.printStackTrace(); | |
} | |
finally { | |
if (inputStream != null) { | |
try { | |
inputStream.close(); | |
} catch (Exception e) {} | |
} | |
} | |
Class<?> c = defineClass(classNameWithPackage, classData, 0, classData.length); | |
resolveClass(c); | |
System.out.println("Steve I am loading another copy of Dummy class. Soon Permgen will fill."); | |
classList.add(c); | |
return c; | |
} | |
}; | |
try { | |
Class.forName("com.vkg.DummyClass", true, classLoader); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment