Friday, November 13, 2009

The Garbage Collector Approaches

A Garbage Collector must do two things:-

1.Detect garbage objects
2.Deallocate the memory of garbage objects and make it available for the program.

There are four approaches that a garbage collector may adopt to detect the garbage objects.

A) Reference -counting Collectors :-Reference counting garbage collectors keep a count of the references for the each live object.When an object is created ,the reference count of each object is set to one.When you reference the object ,the reference count is incremented by one.Similarly when a reference to an object is eliminated ,the reference count is decremented by one.An object which has reference count zero is a garbage object when the object is garbage collected ,the references of the object that it refers to are decremented.Therefore garbage collection of the one may lead to the creation of other garbage objects.This method can be executed in small parts with the program ,and the program need not to be interrupted for a long time .However ,there is an overhead of incrementing and decrementing the counter everytime something happens on the references side.

B) Tracing Collectors :-In this technique ,a set of root is defined from where objects are traced .An object is reachable if there are objects that referenced and cannot ,therefore be accessed in the program.Objects that are reachable are marked .At the end of the trace ,all marked objects can be garbage collected.
This is also known as the mark and sweep algorithm .The mark phase marks all the referenced objects .The sweep phase garbage collects the memory of unreachable and unreferenced objects.

C) Compacting Collectors :-These collectors reduce the degree of memory fragmentation by moving the all unused and free space on one side during garbage collection.The free memory is then available as one huge chunk.All references need to be shifted ,objects are the updated to refer to the new memory locations.

D)Adaptive Collectors :-This algorithm makes the use of the fact that different garbage collectors algorithm works better in different situations .The adaptive algorithm monitors the situation and uses the garbage technique that best suits the situation .It may switch from one technique to the other according to the need.

Suggested Reading:-
Java Sandbox
Java Garbage Collectors
Java Architectutre Tradeoffs
Java class loader architecture

Read more...

Thursday, November 12, 2009

Java Garbage Collector

It is known that you can allocate memory to an object or an array using the ' new ' operator.You cannot however release the memory explicitly through your code.Garbage Collection is the process that automatically frees the memory of objects that are no longer in use.There is no specification of a technique for garbage collection and the implementation has been left to the vendors.

When a programmer stops referencing an object ,the object is not required any more and becomes garbage.The space that is used by the object can be released and used for another objects.In native languages like C and C++ you can explicitly release the memory allocated to objects and data members.Since the native languages are so memory vulnerable that if programmer forgot to release the unused memory then he may run into all sorts of trouble.

Java programming language was first of its kind which has the capability of automatic garbage collection.Garbage collection frees a programmer from the burden of freeing the memory time and time again.The garbage collector itself decides the objects that are of no use or no more referenced in the program and release the memory allocated to them.

Garbage collection is one of the coolest feature Java has but the so called garbage collector has a huge problem that it does memory fragmentation.That's right he is not wise enough to make the decision to make memory the contiguous block of free addresses.What Garbage collector does is it sweeps all the unreferenced objects and makes them free but it leads to memory fragmentation in which there are space available between the chunks of data or vice verse.This memory fragmentation thing to an extent slows down the things a bit due to extra switching overhead.This burden counts to nothing if the application is small but if the application is huge then there is every possibility that your application being slow(Read about Java architecture Trade offs).

NOTE:-You can not enforce Garbage collector to run whenever you want .Rather it will run whenever it wants to.So each time you make a call to Garbage collector (System.gc();) doesn't mean that you actually deallocated the memory from unused objects.It is like a Thread in execution when you are not certain about the output or steps to output.

Suggested Reading:-

JAVA Sandbox
JAVA Architecture
Memory Management in JAVA
JAVA Class loader Architecture
How JVM Handles Strings and Literals

Read more...

Saturday, November 7, 2009

The Java SandBox

The sandbox allows code to be downloaded from any source,but applies restriction on it upon execution.You can also say sandbox isolates particular code during execution to apply the restrictions.A Java sandbox is an area in memory outside which the Java program cannot make calls.This prevents Java programs from being able to call low level system functions that may cause data corruption or other damages.The Java sandbox is used by Java to discourage unsafe applets from accessing the resources.It applies several restrictions on the applets.

The Java sandbox has three components:-

  1. The Bytecode verifier
  2. The applet class loader
  3. The security Manager
The Bytecode Verifier
The first level of Java security is the Java Bytecode verifier.The Bytecode is verified before it is allowed to run on the users machine.It is checked to authenticate its creation by the Java compiler ,and its access is restricted accordingly.The Bytecode verifier makes sure that the format of the bytecode fragment is correct.A built-in theorem is applied on each fragment to make sure that the bytecode does not violate access restrictions or try to access objects using incorrect information.The bytecode is checked in two phases.The first phase takes place when the bytecode is loaded.The verifier checks for the structure of the .class file.Phase two occurs when the bytecode is executed .The verifier checks the validity of the classes,the variables and the methods used in the program.This is done because Java programs are dynamically linked.

The Applet class loader
The second level of security defense id the Java Applet class loader.All Java objects belong to classes and the applet class loader determines how and when an applet is allowed to add classes to a running Java environment.It also makes sure that important parts of the Java Runtime Environment(JRE) are not replaced by any applet code.In a Java environment ,there can be many active class loaders and at the same time ,each class loader can create its own runtime environment .The applet class loader loads all the applets and their references.

The Security Manager
The third level ,and a very important part of the Java sandbox ,is the security manager .The security Manager defines the boundaries of the sandbox.The Java API refers to the security manager before it allows any access to the resource.It restricts the way in which the applet uses visible interfaces.While loading classes,the class loader always compiles with the security manager's decision .Built -in classes are given preference over classes loaded over the net.The sandbox can be used to run untrusted code on the users machine.

NOTE:-A Java protected domain is an extension of the Java sandbox in a file system.Java protected domains enable the use of permission or use a default setting for providing access to function calls outside the sandbox.

Suggested Reading:-

Java Architecture
Java Architecture trade offs
Java Architecture Features
Java class loader architecture
Memory management in Java

Read more...

The Java Architecture TradeOffs

Java is a common programming language and the basic purpose of Java is to develop network programs.Now it is also used to develop system software's and programs too.The Java architecture has trade off between speed and efficiency.In case of Java the efficiency beats the speed the execution and rightly so because there are issues which are needed to be addressed in order to become a sound programming language.

Java has some incredible features which are not in any programming language.But these features comes at some price.The features offered by Java makes Java programs slow in execution.Most of you must not be aware of the fact that Java programs are slower than C++ programs.There are some reasons which states that Java programs are slow in comparison to C++ or C.

1.Java ByteCode is slow in interpreting than executing the machine code of C++.

2.Checks on array bounds are made on each and every array.

3.All the variables are checked for type at RunTime.

4.Java programs are dynamically linked and therefore,the programs has to wait for bytecode to get downloaded.

5.Java checks for null values for all the objects at runtime.

6.The garbage collector is not very efficient in sweeping the unused space.

7.There are also no concept of pointers involved in Java.

8.The extra security checks makes Java more slow in executing(interpreting) the programs(ex:Java Sandbox).

With so many security and value based checks the Java is indeed slow but it provides more security which is not provided by any other language.

Suggested Reading:-

Java Architecture
Java Architecture Features
Java Class loader Architecture
Memory Management in Java
How to Execute System Programs

Read more...

Tuesday, November 3, 2009

The main Thread - Creation and Handling

Every Java program when started,one thread begins running immediately.This is called the main Thread.The reason it is called main Thread is that it starts execution when our program begins.There are two basic characteristics of main Thread are:-

1.It is the Thread from which other threads are generated.
2.It finishes last to perform various tasks which are related to resources.

Main Thread works simply like a method ,from which new calls to Threads are generated and Threads are called in the call stack of main Thread.All called or created Threads must finish before than main Thread.It works simply as a method call.The main Thread finishes when all other Threads are returned.



NOTE:-Every Thread has its own seperate call stack to store its own variable and place mathod calls.The main method calls executes other Threads just in the same fashion as the main() method does.

Remember:-Each and every Java program when starts a main Thread.Keep it in your memory.

Main Thread is automatically created by the JVM when the program is started but the fact is you can control your main Thread too just like any other Thread.

You can craete instance of main Thread as follows:-

? you can use the currentThread() method which is public and static.
i.e.
public static Thread currentThread()
This method as the name suggests returns the reference to the Thread in which it is invoked.

Example:-

class Threadex{
public static void main(String a[]){
Thread t = Thread.currentThread();
System.out.println("Current Thread is "+t);
t.setName("Main Thread");
System.out.println("Current Thread is "+t);
}
}

in the output you will notice that the name of Thread is chnaged to Main Thread.
setName() method is used to set the name of current Thread.The other same method is getName() which returns the name of the Thread.

Suggetsed Reading:-

Ways of creating Threads
Java Thread Basics
Thread life Cycle

Read more...

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP