Technology
http://www.bratandblekning.com Top Blogs Automatic memory management ~ JAVA TUTORIAL

Automatic memory management

Java uses an automatic garbage collector to manage memory in the object lifecycle. The programmer decides when objects should be created, and the Java runtime is responsible for recovering the memory once objects are no longer in use. If object is not used, then unused memory becomes eligible to be freed automatically by the garbage collector.

 If methods for an object,that does not exist, are called, a "null pointer exception" is thrown.
One of the ideas behind Java's automatic memory management model is that programmers can be spared the burden of having to perform manual memory management. But In some languages, memory for the creation of objects is implicitly allocated on the stack & explicitly allocated and deallocated from the heap. 

If the program does not deallocate an object, a memory leak will occurs. If the program access or deallocate memory that has already been deallocated, the result is not defined and difficult tosay what would happen, and the program is likely to become crash. 

This can be overcome by the use of smart pointers, but this causes to add overhead and complexity. Its important to note, that garbage collection does not prevent "logical" memory leaks and also, those where the memory is still referenced but never used.
Garbage collection may happen at any time.It is guaranteed to be triggered if there is insufficient free memory on the heap to allocate a new object; this can cause a program to stall momentarily. In Java Explicit memory management is not possible.
Java does not support pointer ,where object addresses and unsigned integers (usually long integers) can be used interchangeably.

This enables the garbage collector to relocate referenced objects and ensures type safety and security.
The Values of primitive types are either stored directly in fields (for objects) or on the stack (for methods) rather than on the heap, as commonly true for objects (but see Escape analysis). That was an important decision by Java's designers for performance reasons. 
Because of this, Java was not considered to be a pure object-oriented programming language.But, as of Java 5.0, autoboxing allows programmers to proceed as if primitive types were instances of their wrapper class.

 By default, HotSpot uses the Concurrent Mark Sweep collector, which is also known as the CMS Garbage Collector. Well, there are also several other garbage collectors that can be used to manage the Heap.

No comments:

Post a Comment