Java Memory explained (SUN JVM)

Here’s a list of resources I had noted down. Some of these explain how the heap/garbage collection works and some have details on how to configure everything. IBM How does garbage collection work? Detailed description of garbage collection Generational and concurrent garbage collection Sun Turbo-charging Java HotSpot Virtual Machine, v1.4.x to Improve the Performance and … Read more

Determining location of JVM executable during runtime

You could always just use os.name to check if the user is running Windows or not. That’ll work on OS X, Linux and Windows at String jvm_location; if (System.getProperty(“os.name”).startsWith(“Win”)) { jvm_location = System.getProperties().getProperty(“java.home”) + File.separator + “bin” + File.separator + “java.exe”; } else { jvm_location = System.getProperties().getProperty(“java.home”) + File.separator + “bin” + File.separator + “java”; … Read more

Android Studio Build gradle OutOfMemoryError

One of the methods below should work for you: METHOD 1 : Open gradle.properties file from your project tree add this line at the memory allocation line org.gradle.jvmargs=-XX\:MaxHeapSize\=256m -Xmx256m or org.gradle.jvmargs=-XX\:MaxHeapSize\=512m -Xmx512m or org.gradle.jvmargs=-XX\:MaxHeapSize\=1024m -Xmx1024m Depending on the Memory of your computer. Then Invalidate Caches/Restart project from the File > Invalidate Caches/Restart To clean up … Read more

How to emit and execute Java bytecode at runtime?

Here is a working “hello world” made with ObjectWeb ASM (a library which I recommend): package hello; import java.lang.reflect.Method; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Label; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; public class HelloWorldASM implements Opcodes { public static byte[] compile(String name) { ClassWriter cw = new ClassWriter(0); MethodVisitor mv; cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, “hello/HelloWorld”, null, “java/lang/Object”, null); cw.visitSource(“HelloWorld.java”, … Read more

What are the circumstances under which a finally {} block will NOT execute?

If you call System.exit() the program exits immediately without finally being called. A JVM Crash e.g. Segmentation Fault, will also prevent finally being called. i.e. the JVM stops immediately at this point and produces a crash report. An infinite loop would also prevent a finally being called. The finally block is always called when a … Read more

How can I disable IPv6 stack use for IPv4 IPs on JRE?

I wanted to use this for some program I hadn’t control for running that Java app so ended with this _JAVA_OPTIONS=-Djava.net.preferIPv4Stack=true environment variable. (read about _JAVA_OPTIONS here) If you are using Windows, just run this command on Windows cmd: setx _JAVA_OPTIONS -Djava.net.preferIPv4Stack=true Thanks to Jason Nichols for reminding this JVM argument 🙂