Jump to content

Memory allocation: Difference between revisions

Added some Java...maybe someone else could add more
m (omit from <- omit)
(Added some Java...maybe someone else could add more)
Line 112:
( addr ) free throw
</lang>
=={{header|Java}}==
You don't get much control over memory in Java, but here's what you can do:
<lang java>//All of these objects will be deallocated automatically once the program leaves
//their scope and there are no more pointers to the objects
Object foo = new Object(); //Allocate an Object and a reference to it
int[] fooArray = new int[size]; //Allocate all spaces in an array and a reference to it
int x = 0; //Allocate an integer and set its value to 0
</lang>
There is no real destructor in Java as there is in C++, but there is the <tt>finalize</tt> method. From the Java 6 JavaDocs:
 
''The general contract of finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.''
<lang java>public class Blah{
//...other methods/data members...
protected void finalize() throws Throwable{
//Finalization code here
}
//...other methods/data members...
}</lang>
==Oberon-2==
'''TODO:'''
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.