Hi,

In a previous post http://www.javablog.fr/java-jvm-memory-parameters-outofmemoryerror-and-memory-checker.html,
we spoke about solution in order to avoid the famous OutOfMemoryError: Java heap space or OutOfMemoryError: PermGen space errors
and JVM parameters (-XX:PermSize, -XX:MaxPermSize, -Xms, -Xmx).
 
In this post, I would like to expose a simple method to calculate the memory footprint of JAVA objects in a cache in order to set the appropriate values of JVM parameters.
 
As a reminder:

  • PermGen is the memory dedicated to the permanent objects in the VM (Class names, internalized strings, objects that will never get garbage-collected). So the -XX:PermSize=128M parameter corresponds to the memory reserved at the application server starting time. The -XX:MaxPermSize parameter corresponds to the maximum memory usable by the server before swaping.
  • -Xms1024m corresponds to the memory reserved at the application server starting time for the non permanent (volatile) objects in the VM.
  • -Xmx4096m corresponds to the maximum memory usable by the server for the non permanent (volatile) objects in the VM, if this limit is reached, the server persists the data on disk via swaping.

…so, at the application server starting time, the memory reserved will be the total of -XX:PermSize and -Xms parameters.
 
 

The following calculation is based on the postulate that the text size of a JAVA object is larger than the actual size of object in memory.
So, the memory footprint of JAVA objects in cache is :
max number of elements X size of textual ONE element(in bytes) = _____ bytes.
 
Example:
Cache 1:

  • max number of elements = 10000
  • example of content = “090xxxxxxxxxxx32=ok” => 20 bytes
  • TOTAL : 20 bytes x 10000 = 200 000 bytes or 0.2Mo

 
Cache 2:

  • max number of elements = 10000
  • example of content = “XxxxxxxX=1xxxxxx1,2xxxxxx2…” => 1 element could be associated to 11 elements max => (1 + 11) x 9 bytes = 108/110 bytes
  • TOTAL : 110bytes x 10000 = 1 100 000 bytes soit 1.1Mo

 
Cache 3:

  • max number of elements = 10000
  • example of content = “Document [version=null,
    myRepeat1=7xxxxxx9, myfieldSe1=1, myfield1Lbl=Original,
    […]
    archivDate=java.util.GregorianCalendar[time=4484628120000,areFieldsSet=true,
    areAllFieldsSet=true,lenient=true,
    zone=sun.util.calendar.ZoneInfo[id=”Europe/Paris”,offset=3600000,
    dstSavings=3600000,useDaylight=true,transitions=184,
    lastRule=java.util.SimpleTimeZoneid=Europe/Paris,offset=3600000,
    dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,
    startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,
    endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,
    endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,
    YEAR=2112,MONTH=1,WEEK_OF_YEAR=6,WEEK_OF_MONTH=2,DAY_OF_MONTH=11,
    DAY_OF_YEAR=42,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=11,
    HOUR_OF_DAY=11,MINUTE=2,SECOND=0,MILLISECOND=0,ZONE_OFFSET=3600000,
    DST_OFFSET=0], myLabel1234=HUO1, is_reprise=false, mergingCount=1,
    mergingParentDocumentIds=[Ljava.lang.String;@1262d8c]” => 8000 bytes
  • TOTAL : 8000 bytes x 10000 = 80 000 000 bytes or 80Mo

 
…so the memory footprint of JAVA objects in all caches will be maximum 81.3 Mo
 
…and the JVM parameters could be:

  • DEV with RAM=2048MB : -Xmx1024m -Xms512m -XX:PermSize=128M -XX:MaxPermSize=256M
  • TEST with 4096MB : -Xmx2048m -Xms1024m -XX:PermSize=128M -XX:MaxPermSize=256M
  • PROD with 6144MB : -Xmx4096m -Xms1024m -XX:PermSize=128M -XX:MaxPermSize=256M

That’s all!!!

Huseyin OZVEREN