Presentation is loading. Please wait.

Presentation is loading. Please wait.

Memory Management in Java Mr. Gerb Computer Science 4.

Similar presentations


Presentation on theme: "Memory Management in Java Mr. Gerb Computer Science 4."— Presentation transcript:

1 Memory Management in Java Mr. Gerb Computer Science 4

2 Overview What’s special about Java? Memory management in Java

3 The Java Compiler Earlier Languages (Fortran, C,C++,Pascal) – Source code compiled into object code – Object code linked into executable code – Computer runs executable code Java – Source code compiled into bytecode – Bytecode run by Java Virtual Machine

4 Why Bytecode is Better Bytecode created on one computer will run on any Java virtual machine – Not true of C/C++ object code More secure – Executable code can do anything in a computer – Java virtual machine keeps bytecode from doing anything malicious … But nothing is faster than executable code

5 Memory Management In Java, 3 types of memory: TypeComes fromHow? StaticFixed place in memory Declare as static AutomaticStackLocal variables DynamicHeapAllocated via new

6 Examples Static static int foo; Automatic int bar; Dynamic baz=new myClass(arg)

7 Composite Types More than one data item – Classes – Arrays In Java, all extended types are dynamic – But references to them can be static, dynamic or automatic Primitive types can be static (declared static), automatic (as local variables) or dynamic (part of a composite type)

8 Declaring Extended Type in Java myClass foo; – No memory has been allocated – foo is a special pointer called null – It is an error to reference a data member or method of foo foo = new myClass(); – An instance of myClass is now allocated from the heap. Foo points to it. – Can pass arguments to the constructor.

9 Deallocating Memory in Java No need to delete Java automatically returns all allocated memory to the heap when your program is done with it. Called garbage collection Greatest invention since the egg slicer


Download ppt "Memory Management in Java Mr. Gerb Computer Science 4."

Similar presentations


Ads by Google