3
(메모리가 JVM에 의해 제어되는 한) 메모리에서 값을 읽는 방법이 있다고 들었습니다. 그러나 예를 들어 주소 8E5203
에서 바이트를 가져 오는 방법은 무엇입니까? getBytes(long)
이라는 메서드가 있습니다. 이것을 사용할 수 있습니까?sun.misc.Unsafe : 주소에서 바이트를 가져 오는 방법
고맙습니다. 피트
(메모리가 JVM에 의해 제어되는 한) 메모리에서 값을 읽는 방법이 있다고 들었습니다. 그러나 예를 들어 주소 8E5203
에서 바이트를 가져 오는 방법은 무엇입니까? getBytes(long)
이라는 메서드가 있습니다. 이것을 사용할 수 있습니까?sun.misc.Unsafe : 주소에서 바이트를 가져 오는 방법
고맙습니다. 피트
메모리 위치에 직접 액세스 할 수 없습니다! JVM이 관리해야합니다. 보안 예외 또는 EXCEPTION_ACCESS_VIOLATION
이 발생합니다. 이로 인해 JVM 자체가 손상 될 수 있습니다. 그러나 코드에서 메모리를 할당하면 바이트에 액세스 할 수 있습니다.
public static void main(String[] args) {
Unsafe unsafe = null;
try {
Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
unsafe = (sun.misc.Unsafe) field.get(null);
} catch (Exception e) {
throw new AssertionError(e);
}
byte size = 1;//allocate 1 byte
long allocateMemory = unsafe.allocateMemory(size);
//write the bytes
unsafe.putByte(allocateMemory, "a".getBytes()[0]);
byte readValue = unsafe.getByte(allocateMemory);
System.out.println("value : " + new String(new byte[]{ readValue}));
}