1

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.

newlogic
  • 807
  • 8
  • 25
  • In pure Java, you can't. – Sotirios Delimanolis Oct 18 '13 at 15:35
  • You need to use native code. – SLaks Oct 18 '13 at 15:36
  • 3
    I'm not sure your plan of using Java to violate every principle of java will go well. – Richard Tingle Oct 18 '13 at 15:37
  • Are these GC pause times causing you difficulties? – Richard Tingle Oct 18 '13 at 15:38
  • I'm interested in low latency developement so yes they are. – newlogic Oct 18 '13 at 15:39
  • if you're interested in low latency development then you don't use Java. Just sayin'. – Mateusz Dymczyk Oct 18 '13 at 15:40
  • And these off heap objects? Are they forever, remaining in RAM until the end of the program – Richard Tingle Oct 18 '13 at 15:40
  • Why not use C/C++ instead? You're trying to shoot a bird with a water pistol... –  Oct 18 '13 at 15:40
  • As much as I love java I think I agree, java is not an approriate tool for whatever you're trying to do – Richard Tingle Oct 18 '13 at 15:41
  • Richard Tingle, I do intend to free the memory. – newlogic Oct 18 '13 at 15:42
  • The argument for not using C/C++ is too remain within a single development environment, if its possible to achieve this sort of thing in Java and its not incredibly difficult then why not do it in Java as most of the code will be in Java. – newlogic Oct 18 '13 at 15:44
  • 1
    @user1037729: single dev unit? you mean portability? Java is portable because of the JVM. So you're using Java for the JVM yet you are trying to bypass the JVM. See where I'm going with this? ;-) – Mateusz Dymczyk Oct 18 '13 at 15:46
  • @user1037729 I think that it **will** be incredibly difficult and as Jon Skeet says it will be very system dependant and horrible – Richard Tingle Oct 18 '13 at 15:47
  • 1
    You are currently trying to dig a hole with a screwdriver. Now a screwdriver is an excellent piece of equipment, but you'll be digging a long time – Richard Tingle Oct 18 '13 at 15:48
  • Ok fair enough that it would be more idiomatic to do this in C/C++, its interesting none the less to see if this sort of this is possible in Java, the closest way I can think of achieving this is to create a wrapper FunctionPointer Java object which contains a single method, this would be serialized and stored in DirectByteBuffer, and the location of this in the buffer would effectively be the function pointer. – newlogic Oct 18 '13 at 16:12

1 Answers1

2

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I've just edited the question to provide some background on why I want to do this, do you know a way of achieving this? – newlogic Oct 18 '13 at 15:37
  • 1
    @user1037729: I've responded to your edit - fundamentally, I think it's a bad idea. You'd have to do it with JNI, and you should expect a huge amount of pain. – Jon Skeet Oct 18 '13 at 15:40
  • Java JNI is awesome. For instance, you can assign into final variables. I use that to store pointers safely in native code in long fields. – Enerccio Oct 18 '13 at 15:41
  • 2
    @Enerccio Awesome, terrifying, potato potartow – Richard Tingle Oct 18 '13 at 15:42