Wednesday 29 July 2015

Java Source World: Java stack and heap

Java stack and heap | Java Source World

Java Source World A Java quick reference blog


Java Memory 



Java Memory is one of the most interesting concepts in JVM and its always fun to know how java memory model is designed. Java memory model (JMM) has quite a few components of which stack and heap are two of them.



  • JVM has some space allocated to heap and stack. JVM stack stores local variables, method pointers and function call whereas heap is used to store class level variables, java class objects, string pool.
  • JVM has a heap that is shared across all Java threads. Each java thread has its own stack called as stack frames, where it stores local variables and stack pointers .
  • 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
  • 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.
  • JVM heap is configurable using -Xmx and -Xms and stack size can be configured using -Xss
  • The lifetime of stack is the longetivity of thread, where as heap is present as long application is running. Garbage collections runs on heap.
  • Whenever an object is created, it's always stored in the Heap space and stack memory contains the reference to it.

Java Source World A Java quick reference blog

Read More »

Thursday 16 July 2015

Java Source World: Difference between HTTPSession and Stateful Session Bean

Difference between HTTPSession and Stateful Session Bean in EJB | Java Source World
                                                                   Java Source World         A Java quick reference blog

Difference between HTTPSession and Stateful Session Bean in EJB:



 From a logical point of view, a Servlet/JSP session is similar to an EJB session. Using a session, in fact, a client can connect to a server and maintain his state.

But, is important to understand, that the session is maintained in different ways and, in theory, for different scopes.



A session in a Servlet, is maintained by the Servlet Container through the HttpSession object, that is acquired through the request object. You cannot really instantiate a new HttpSession object, and it does not contains any business logic, but is more of a place where to store objects.


A session in EJB is maintained using the SessionBeans. You design beans that can contain business logic, and that can be used by the clients. You have two different session beans: Stateful and Stateless. The first one is somehow connected with a single client. It maintains the state for that client, can be used only by that client and when the client "dies" then the session bean is "lost".



A Stateless Session Bean does not maintain any state and there is no guarantee that the same client will use the same stateless bean, even for two calls one after the other. The lifecycle of a Stateless Session EJB is slightly different from the one of a Stateful Session EJB. Is EJB Containers responsability to take care of knowing exactly how to track each session and redirect the request from a client to the correct instance of a Session Bean. 


The way this is done is vendor dependant, and is part of the contract.




Also Read: 

Difference between stateful and stateless session



                                                                        Java Source World         A Java quick reference blog








Read More »