Disposing of objects
You can integrate the Java garbage collector with your .NET objects using Javonet. When you create an .NET object and store it in a Java variable, it will be handled in .NET process as long as your variable lives in the Java memory.
When Java collects the NObject object, this event is passed to .NET, and the corresponding .NET object is disposed and collected. If the .NET object implements the IDisposable interface, then the appropriate disposal procedure is followed.
You can also force object disposal by calling the "Dispose" method on the "NObject" object. Javonet exposes the dedicated "Dispose" method for closing all objects and releasing .NET memory. This method should be called only at the end of your Java application.
Assuming we have a custom JAR library with the following class inside:
public class TestClass {
public TestClass() {
}
public static int MyStaticField;
public int MyInstanceField;
public static String SayHello(String name) {
return "Hello " + name;
}
public static int MethodExpectingPrimitiveInt(int arg) {
return arg * 2;
}
public static int MethodExpectingClassInteger(Integer arg) {
return arg * 2;
}
public int MultiplyByTwo(Integer arg) {
return arg * 2;
}
public <T> T MyGenericMethod(T arg1)
{
return arg1;
}
}
To see how destructor fro .NET works:
Was this article helpful?