Observability Done Right: Best Practices and Anti-Patterns for Effective System Monitoring

Image
  WHAT Observability is a concept that refers to the ability to gain insights into the behavior and performance of complex systems. In the context of software engineering, observability involves the collection, analysis, and visualization of data from software applications, infrastructure, and other components of a system. In the animal kingdom, observability plays a critical role in survival, allowing animals to monitor their surroundings, detect threats, and find food. Dolphins use echolocation to observe their surroundings. They emit high-frequency sounds that bounce off objects, allowing them to create a 3D map of their environment. Thanks for reading Knowledge Cafe! Subscribe for free to receive new posts and support my work. Subscribed WHY In today's era, architectures are becoming increasingly large, complex, and fast-paced due to the faster development and deployment of software by distributed teams with the help of DevOps, continuous delivery, and agile development methodo...

Difference between Stack vs Heap in Java


What Is Stack?


Each Java virtual machine thread has a private Java virtual machine stack, created at the same time as the thread. A Java virtual machine stack stores frames. It holds local variables and partial results, and plays a part in method invocation and return. Because the Java virtual machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. The memory for a Java virtual machine stack does not need to be contiguous.

What Is Heap?


The Java virtual machine has a heap that is shared among all Java virtual machine threads. The heap is the runtime data area from which memory for all class instances and arrays is allocated.

The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector); objects are never explicitly deallocated. The Java virtual machine assumes no particular type of automatic storage management system, and the storage management technique may be chosen according to the implementor’s system requirements. The heap may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger heap becomes unnecessary. The memory for the heap does not need to be contiguous.

As we know objects are created inside heap memory and Garbage collection is a process which removes dead objects from Java Heap space and returns memory back to Heap in Java. For the sake of Garbage collection Heap is divided into three main regions named as New Generation, Old or Tenured Generation and Perm space. New Generation of Java Heap is part of Java Heap memory where newly created object are stored, During the course of application many objects created and died but those remain live they got moved to Old or Tenured Generation by Java Garbage collector thread on full garbage collection. Perm space of Java Heap is where JVM stores Meta data about classes and methods, String pool and Class level details. You can see How Garbage collection works in Java for more information on Heap in Java and Garbage collection.

The heap mainly store objects create using or class level variables.

Difference between Stack vs Heap in Java


1) Main difference between heap and stack is that stack memory is used to store local variables and function call, while heap memory is used to store objects in Java. No matter, where object is created in code e.g. as member variable, local variable or class variable,  they are always created inside heap space in Java.


2) Each Thread in Java has there own stack which can be specified using -Xss JVM parameter, similarly you can also specify heap size of Java program using JVM option -Xms and -Xmx where -Xms is starting size of heap and -Xmx is maximum size of java heap.


3) If there is no memory left in stack for storing function call or local variable, JVM will throw java.lang.StackOverFlowError, while if there is no more heap space for creating object, JVM will throw java.lang.OutOfMemoryError: Java Heap Space.


4) If you are using Recursion, on which method calls itself, You can quickly fill up stack memory. Another difference between stack and heap is that size of stack memory is lot lesser than size of  heap memory in Java.


5) Variables stored in stacks are only visible to the owner Thread, while objects created in heap are visible to all thread. In other words stack memory is kind of private memory of Java Threads, while heap memory is shared among all threads.

Pseudo Code to create an object on the stack:


void somefunction() {
 /* create an object "m" of class Member 
 this will be put on the stack since the 
 "new" keyword is not used, and we are 
 creating the object inside a function
 */

  int a = 10;

} 
//the variable "a" is destroyed once the function ends

 Pseudo Code to create an object on the heap:


void somefunction( ){
 /* create an object "m" of class Member this will be put on the heap since the "new" keyword is used, and we are creating 
 the object inside a function */

  Member m = new Member( ) ;

  /* the object "m" must be deleted otherwise a memory leak occurs */

  m = null;
 }

Word of Advise for Developer to Avoid Memory related errors:

OutOfMemoryException :

(1) Avoid creating new objects for smaller operations: Use primitive types for calculation instead of wrapper classes. For strings you can create String s = new String("myString")-> this will create new string object instead you can utilize string pool where same string object is returned : String s = "myString";

(2) For large size projects: where database returns big result set for select query: we can use batch select instead of big result set having all rows.

(3) Programmaticaly call System.gc() --> regular interval

(4) Use Sonar/Jprofiler to check memory leaks

StackOverflaw:


(1) Avoid cicular calling of methods (recursive call) which has big set of variables defined.

(2) Instead of creating custom class having many properties, we can have different object with less number of properties

(3) Follow SOLID (Single responsibility, Open-closed, Liskov substitution, Interface segregation and Dependency inversion) principle of class design ( http://en.wikipedia.org/wiki/SOLID_(object-oriented_design))


There can be many more suggestions based on application needs but above are common guidelines.



Popular posts from this blog

Chain of responsibility using Spring @Autowired List

Iterate Through a HashMap

Under the Hood: Understanding the Gossip Protocol in Apache Cassandra