Thursday, February 13, 2014

Detecting memory leaks:

Detecting memory leaks:
Use tools like JProbe, OptimizeIt etc to detect memory leaks. 
Use operating system process monitors like task manager on NT systems, ps, vmstat, iostat, netstat etc on UNIX systems.
Write your own utility class with the help of totalMemory() and freeMemory() methods in the Java Runtime class. Place these calls in your code strategically for pre and post memory recording where you suspect to be causing memory leaks. An even better approach than a utility class is using dynamic proxies or Aspect Oriented Programming (AOP) for pre and post memory recording where you have the control of activating memory measurement only when needed.
Minimising memory leaks:
In Java, typically memory leak occurs when an object of a longer lifecycle has a reference to objects of a short life cycle. This prevents the objects with short life cycle being garbage collected. The developer must remember to remove the references to the short-lived objects from the long-lived objects. Objects with the same life cycle do not cause any issues because the garbage collector is smart enough to deal with the circular references.
JVM Architecture: - JVM 1.4.1 version developed by Hotspot. 
  
Class loader subsystem does the following things: -
1. It will load .class file into memory (RAM)
2. It will verify byte code of that .class file.
3. It will allot the required memory for the program.
            This memory is divided into five parts; they are called runtime data areas.
1. Method area: - It stores class code, static variables, static blocks and method code.
2. Heap area: - Objects are created on Heap. JVM creates objects on Heap.
3. Java Stacks: - Java stacks are used to execute the java methods. Java stacks will have frames. On each frame a separate method is executed.
4. PC Registers: - These registers contain the address of that instruction to be executed by the processor.
5. Native method stacks: - It is a method is written in a language other than java. Native method stacks places when native methods are executed.
Note: - Native method interface links header files to a native method while it is being executing.
r  Execution engine contains interpreter & JIT compiler, which translates byte code into machine language.
How JIT compiles converts byte codes.
For Interpreter
 
Ex: -     print a; 2sec
            print b; 2sec
memory
 
JIT Compiler Processor
 
            Repeat the loop 10times;
            print a; 2 + 2 


r  Hotspot is the block code of given to JIT compiler for execution.
10. Multithread: - Thread means an execution or processing group of statements. Executing different parts of a program simultaneously at a time by using more than one thread is called Multithreading. This is an essential feature of to design server side programs.
11. Dynamic: - It means at runtime, we can develop programs in java, which dynamically change on Internet. (Ex: Applets).
r  In java entire memory will be dynamic memory.

No comments:

Post a Comment