Tuesday, March 8, 2016

Heap Space Vs Permgen Space

Simply
  • Heap space: All live objects are allocated here.
  • Stack space: Stores references to the object for variable in method call or variable instantiation.
  • Perm space: Stores loaded classes information
For example:
Student std = new Student();
after executing the line above memory status will be like this.
  • Heap: stores "new Student()"
  • Stack: stores information about "std"
  • Perm Space: stores information about Student class
Stack also stores primitive literals

Heap Space:

java.lang.OutOfMemoryError:Java heap space

Java applications are only allowed to use a limited amount of memory. This limit is specified during application startup. To make things more complex, Java memory is separated into two different regions. These regions are called Heap space and Permgen (for Permanent Generation):
OutOfMemoryError: Java heap space
The size of those regions is set during the Java Virtual Machine (JVM) launch and can be customized by specifying JVM parameters -Xmx and -XX:MaxPermSize. If you do not explicitly set the sizes, platform-specific defaults will be used.
The java.lang.OutOfMemoryError: Java heap space error will be triggered when the application attempts to add more data into the heap space area, but there is not enough room for it.
Note that there might be plenty of physical memory available, but thejava.lang.OutOfMemoryError: Java heap space error is thrown whenever the JVM reaches the heap size limit.

What is causing it?

There most common reason for the java.lang.OutOfMemoryError: Java heap space error is simple – you try to fit an XXL application into an S-sized Java heap space. That is – the application just requires more Java heap space than available to it to operate normally. Other causes for this OutOfMemoryError message are more complex and are caused by a programming error:
  • Spikes in usage/data volume. The application was designed to handle a certain amount of users or a certain amount of data. When the number of users or the volume of data suddenly spikes and crosses that expected threshold, the operation which functioned normally before the spike ceases to operate and triggers the java.lang.OutOfMemoryError: Java heap space error.
  • Memory leaks. A particular type of programming error will lead your application to constantly consume more memory. Every time the leaking functionality of the application is used it leaves some objects behind into the Java heap space. Over time the leaked objects consume all of the available Java heap space and trigger the already familiar java.lang.OutOfMemoryError: Java heap space error.


Permgen space:

java.lang.OutOfMemoryError:Permgen space

Java applications are only allowed to use a limited amount of memory. The exact amount of memory your particular application can use is specified during application startup. To make things more complex, Java memory is separated into different regions which can be seen in the following figure:
java.lang.outofmemoryerror: Permgen space
The size of all those regions, including the permgen area, is set during the JVM launch. If you do not set the sizes yourself, platform-specific defaults will be used.
The java.lang.OutOfMemoryError: PermGen space message indicates that thePermanent Generation’s area in memory is exhausted.

What is causing it?

To understand the cause for the java.lang.OutOfMemoryError: PermGen space, we would need to understand what this specific memory area is used for.
For practical purposes, the permanent generation consists mostly of class declarations loaded and stored into PermGen. This includes the name and fields of the class, methods with the method bytecode, constant pool information, object arrays and type arrays associated with a class and Just In Time compiler optimizations.
From the above definition you can deduce that the PermGen size requirements depend both on the number of classes loaded as well as the size of such class declarations. Therefore we can say that the main cause for the java.lang.OutOfMemoryError: PermGen space is that either too many classes or too big classes are loaded to the permanent generation.

Resource Link:

  1. https://plumbr.eu/outofmemoryerror/java-heap-space
  2. https://plumbr.eu/outofmemoryerror/permgen-space
  3. http://stas-blogspot.blogspot.in/2011/07/most-complete-list-of-xx-options-for.html#UseConcMarkSweepGC
  4. https://www.javacodegeeks.com/2013/12/decoding-java-lang-outofmemoryerror-permgen-space.html
  5. http://www.javavillage.in/reasons-outofmemory-permespace.php
  6. https://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-VM/html/memleaks.html
  7. http://www.dynatrace.com/en/javabook/other-java-memory-issues.html

Monday, March 7, 2016

how the hashmap stores the objects internally?

HashMap stores the values through put(key,value) and gets the values thorugh get(key). The process follows the concept of Hashing.
When we say put(key,value) - Internally hashCode() for the key is calculated and being taken as the input for hashfunction() to find the bucket location for storing.
In case of Collision - while calculating the hashcode() there may be a possibility the key is different but the hashcode() is same, at that time after finding out the bucket location the storing is done in the linked list. Please Note - While storing as the Map.Entry both the key-value is stored.
When Retrieve of the value through key is done then if during the collision where the key's hashcode() may be same then the value is retrived though equals() function to find out the desired key's value.
All credit goes to Anand Builders

Why does Hashtable not allow null key?

To successfully store and retrieve objects from a hashtable, the objects used 
as keys must implement the hashCode method and the equals method.
In a nutshell, since null isn't an object, you can't call .equals() or .hashCode() on it, so the Hashtable can't compute a hash to use it as a key.
HashMap is newer, and has more advanced capabilities, which are basically just an improvement on the Hashtable functionality. As such, when HashMap was created, it was specifically designed to handle null values as keys and handles them as a special case.
Specifically, the use of null as a key is handled like this when issuing a .get(key):
(key==null ? k==null : key.equals(k))
All Credit goes to cdeszaq
Original Link: Why does Hashtable not take null key?

CV Study

Sunday, March 6, 2016

Moja

Coffee shop management system in java
If Cashier, items, Customers, inventory are major classes of java program what will be the relation they have for each other. In short, how to make Object oriented model of coffee shop management system (like Composition, inheritance etc) please help me

http://stackoverflow.com/questions/35827433/postgresql-change-spelled-numbers-to-numbers