0

I have the following C# code:

  int num = Convert.ToInt32(this.Memory.ReadInt(
        this.Memory.BaseAddress() + 0x80f0cc, new int[] { 12, 0x6c5c }));

This reads the data from the memory and gives me the integer value.

Is that somehow possible to achieve in Java? I've tried searching online but couldn't get anything.

reto
  • 9,995
  • 5
  • 53
  • 52
Makky
  • 17,117
  • 17
  • 63
  • 86

4 Answers4

1

No, Java is specifically designed to disallow that... but...

It's not officially supported (and how/whether or not it works likely varies between JVM implementations), but the class sun.misc.Unsafe allows one to do stuff that you're normally not supposed to be able to do. See this article.

Excerpt:

@Test
public void testCopy() throws Exception {
    long address = unsafe.allocateMemory(4L);
    unsafe.putInt(address, 100);
    long otherAddress = unsafe.allocateMemory(4L);
    unsafe.copyMemory(address, otherAddress, 4L);
    assertEquals(100, unsafe.getInt(otherAddress));
}

The unsafe.getInt(otherAddress) call may be the closest thing you can achieve in pure Java.

There was a survey in 2014 about whether or not Unsafe should gain official support, but I'm not sure what (if anything) came of that.

BambooleanLogic
  • 7,530
  • 3
  • 30
  • 56
  • If you allocate a memory via the unsafe.allocateMemory, don't forget to call `unsafe.freeMemory` when you finish your work with that memory. Otherwise you will create a memory leak. – Marian Spanik Feb 02 '15 at 14:22
0

No. It is not possible to access native memory with pure Java code. You can use JNA or JNI to link to native libraries, and they can access arbitrary memory but Java itself runs in a virtual machine and does not allow you to access memory explicitly inside or outside of the virtual machine.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You could try going over to Project Kenai and having a look at the API and their examples:

Project Kenai

I also found an example for someone else who was trying to do something similar. Hope it helps.

Example

Daniel Kelley
  • 7,579
  • 6
  • 42
  • 50
Gaz
  • 328
  • 3
  • 15
0

Makky, you can look at the old reply about memory management in java. He describes why you can not do this.

Community
  • 1
  • 1