R
R
Refri2015-10-12 06:18:35
Java
Refri, 2015-10-12 06:18:35

How can you deliberately drop the JVM?

How can you drop the JVM, I heard that they might ask this at an interview, and I myself became interested.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vlad20012, 2015-10-12
@eldqs

Well, well, all your examples are just unhandled exceptions. Usually by "drop" they mean more fun things. Let's take Unsafe and read a byte at address zero.

import sun.misc.Unsafe;

public class CrashTheJVM
{
    private static final Unsafe UNSAFE = createUnsafe();

    private static Unsafe createUnsafe()
    {
        try
        {
            Field uf = Unsafe.class.getDeclaredField("theUnsafe");
            uf.setAccessible(true);
            return (Unsafe) uf.get(null);
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }
  
    public static void main(String[] args)
    {
        UNSAFE.getByte(0);
    }
}

Segfolt ! 100% drop. Well, if the SecurityManager forbids doing this or the JVM implementation does not support Unsafe, we will still get an unhandled exception.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question