newlogic
newlogic

Reputation: 827

How would I invoke a Java method using a long memory address?

Lets say I had a memory address which was a long in Java, If I know that memory address was a function pointer, how could I invoke the function at this address?

The reason I am interested in this is for dealing with off-heap objects. I am to create objects in direct byte buffers which will not be subject to GC. This will allow me to negate GC pause times as the GC will never run if I don't create any objects on the heap.

Upvotes: 1

Views: 189

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502016

You wouldn't, within pure Java. It's just about the opposite of a lot of what Java is about. You could do so with JNI if you really wanted to. Ideally, you'd change your design so that you didn't need to do this though - it's a pretty odd requirement in most situations.

Now that you've edited the post and it seems you're basically wanting to do better than the VM's garbage collector, I would strongly suggest that you avoid this. It's likely to take a huge amount of effort and lead to a very brittle system which requires different binaries for every environment. You'd have a hard time using the off-heap values as real objects anyway, as at that point the JVM may well make various assumptions about the data. If you only care about primitive values, you could always have a large byte array to act as "raw" storage with appropriate wrapper code to convert between data in that array and the primitive values... all without JNI.

Upvotes: 2

Related Questions