Wednesday, 16 June 2010
About Java Run Out Perm. Space
What is the memory leak? The dynamically allocated memory has become unreachable.
1) C/C++ is prone to cause memory leak, because it has no auto garbage collection mechanism.
2) JAVA: unbounded memory use, but
There are two types of Memory in JVM: Heap and Permanent.
What Heap for? dynamatically generating instances.
What Permanent for? store the templates, i.e. Classes, but not dynamically used.
So run out the memory of this part, mostly because it has been used out of boundary of the space, at least I think so. So increasing permananent memory from the default of 64mb to 150 mb.
Add java option: -XX:MaxPermSize=150m
Stack memory:
In addition: java vm also manage a stack for the handling local var. and function calls; each thread has its own stack, seen as a private memory. If there is no memory left for storing local var., then java will throw a stack overflow error.
Heap memory:
Java VM maintain a memory space for a common usage for all threads, where all objects are created there. no matter where these class having been instantiated. When heap runs out, then java throws java.lang.OutOfMemoryError.
So there are 3 different kind of memory usage in java. heap perm. and stack.
Monday, 21 September 2009
HttpURLConnection: java.net.ConnectException: Connection timed out
CodeGuru Forums > Java Programming > Java Programming > HttpURLConnection: java.net.ConnectException: Connection timed out:
Click to See Complete Forum and Search --> : HttpURLConnection: java.net.ConnectException: Connection timed out:
vadirajak1
January 11th, 2005, 03:44 AM
Hi,
I am trying to read from a URL and display the contents in an Applet. I am using URL,HttpURLConnection classes. I am consistently getting Connection time out Exception. I am unable to connect. Could any one help me out with this. I have tried some TimeOut handler classes available on the net but still I am getting the same Exception. Please provide your comments if you have come across similar problems before.
Here is the code Iam using
url = new URL((URL)null, str, new HttpTimeoutHandler(15000)); // timeout value in milliseconds
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.connect();
nputStream is = httpConnection.getInputStream();
Thanks & Regards,
Vadi
cma
January 11th, 2005, 07:57 AM
Can you connect to the server directly, say, using your browser?
cjard
January 11th, 2005, 08:53 AM
connection timeouts occur when:
the IP address for the requested server is successfully found
connection establishment packets are dispatched to the IP address
the destination address deliberately ignores or does not receive them
similar to connection timeout is Connection Refused, but in this case the destination system is actually sending packets back saying "go away, there is no service running on the port you are trying to connect to"
your packets are just being lost.. either the system or some intermediate system is firewalled. follow CMA's advice.. if you cant connect using telnet, a browser or some other app, its a network problem, not a java one
vadirajak1
January 12th, 2005, 01:11 AM
I am able to connect to the url through browser and able to see the data that is there. But when trying to connect through the code, I am getting the connection time out exception. So this must not be a Network/Firewall related issue. Also, the same code is working fine when we tried testing it from the US, but it is giving the timeout Exception when tried from India. Is this a simple time out issue? If so, even some of the time out handlers available on the net are not solving the issue? Can anyone suggest a way out please.
Thanks & Regards,
Vadi
cjard
January 12th, 2005, 07:52 AM
check your browser proxy settings
try using telnet to connect to the same server that youre using in code and the browser:
telnet www.google.com 80
codeguru.com
Copyright 2009 WebMediaBrands Inc., All Rights Reserved.
Friday, 4 September 2009
Java Memory Insufficent
The JVM heap space is where all Java objects are stored, as well as memory used by the garbage collector. Sun recommends increasing this value for server applications,
Large server applications often experience two problems with these
defaults. One is slow startup, because the initial heap is small and
must be resized over many major collections. A more pressing problem
is that the default maximum heap size is unreasonably small for most
server applications.
If an application gobbles up memory until it throws an OutOfMemory exception, or seems to be spending an excessive amount of time doing garbage collection, a heap dump can help track down the source of the problem.
Three type of Insufficent Memory:
1. Heap memory error: when an application creates a new object but the heap does not have sufficient space and cannot be expanded further, and OutOfMemoryError will be thrown with the following error message:
java.lang.OutofMemoryError: Java heap space
2. Non-heap memory error: The permanent generation is a non-heap memory area in the HotSpot VM implementation that stores per-class structure as well as interned strings. When the permanent generation is full, the application will fail to load a class or to allocate an interned string, and an OutOfMemoryError will be thrown with the following error message:
java.lang.OutOfMemoryError: PermGen space
3. Native memory error: The Java Native Interface(JNI) code or the native library of an applicaiton and the JVM implementation allocate memory from the native heap. An OutOfMemoryError will be thrown when an allocation in the native heap fails. For instance, the following error message indicates insufficient swap space, which could be caused by a configuration issue in the operating system or by another process in the system that is consuming much of memory.
java.lang.OutOfMemoryError: request <size> bytes for <reason>. Out of swap space?
Reasons for Insufficent Memory
Momory Leaks: An applicatoin keeps a reference to an object that it no longer needs. Garbage collector cannot free this part of memory.
Finalizers: excessive use of finalizers.
DeadLocks:
Looping Threads:
High Lock Contention:
How to remotely monitoring JVM
1) Opening a monitoring port on the remote Tomcat server, we added following java VM options at the startup script,
Export JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.port=19993 -Dcom.sun.management.jmxremote.authenticate=
false -Dcom.sun.management.jmxremote.ssl=false"
This requires firewall opening between the local pc and server on port specified 19993.
2) There are two good tools for monitoring JVM, i.e. JConsole and VirtualVM. run these two appliation and login the remote server and the specified port, then you may monitor the VM state.
JConsole is shipped with JDK after version 5. You may easily find it from JAVA_HOME/bin/jconsole.exe
VisualVM is a third party software, which can be download from its official home.
How to Reset Heap Memory Size
Sometime the insuficent memory is just caused by the insufficent heap memory, so it need to reset the memory size.The Java Virtual Machine takes two command line arguments which set the initial and maximum heap sizes:
-Xms and -Xmx. For example if you want to give your Java program needs a 128Mb initial and 256Mb maximum heap size you could launch it as follows:
JAVA_OPTS="-server -Xms128M -Xmx256M"
Set HeapDumpOnOutOfMemoryError Option
set JAVA_OPTS=%JAVA_OPTS% -server -Xms512m -Xmx800m -XX:PermSize=64M -XX:MaxPermSize=128m -Djava.awt.headless=true -XX:+HeapDumpOnOutOfMemoryError -XX:+HeapDumpOnCtrlBreak
A heap dump file will be generated in /bin folder, when out of memory happens.
The -XX:+HeapDumpOnOutOfMemoryError command-line option was introduced in Java SE release
5.0 update 7. This option tells the HotSpot VM to generate a heap dump when the first thread throws a
java.lang.OutOfMemoryError because the Java heap or the permanent generation is full. There is
no overhead in running with this option, and so it can be useful for production systems where
OutOfMemoryError takes a long time to surface.
The heap dump is in HPROF binary format, and so it can be anaylzed by any tool that can import this
format, for example the Heap Analysis Tool (HAT).
By default the heap dump is created in a file called java_pid<pid>.hprof in the working directory
of the VM, where <pid> is the process ID. You can specify an alternative file name or directory with
the -XX:HeapDumpPath= option. For example, -XX:HeapDumpPath=/disk2/dumps will cause the
heap dump to be generated in the /disk2/dumps directory.
How to monitor JVM
I sent several banches of data to the debitorregisterweb, and it seems that there are some heap memory that cannot be released afterwards, the used the heap space increased step by step, where is the gc operation,
How to fix memory leak
http://java.dzone.com/news/how-fix-memory-leaks-java
How profile Java application in VirtualVM
Wednesday, 2 September 2009
Setup Jconsole Remotely Monitoring Memory Leak
JVM Arguments
J2SE 1.5.0_06
# Memory configuration
JAVA_OPTS="-server"
JAVA_OPTS="$JAVA_OPTS -Xms1280m"
JAVA_OPTS="$JAVA_OPTS -Xmx1280m"
JAVA_OPTS="$JAVA_OPTS -XX:NewSize=640m"
JAVA_OPTS="$JAVA_OPTS -XX:MaxNewSize=640m"
JAVA_OPTS="$JAVA_OPTS -XX:SurvivorRatio=5"
JAVA_OPTS="$JAVA_OPTS -XX:TargetSurvivorRatio=90"
JAVA_OPTS="$JAVA_OPTS -XX:MaxTenuringThreshold=12"
JAVA_OPTS="$JAVA_OPTS -XX:+UseConcMarkSweepGC"
JAVA_OPTS="$JAVA_OPTS -XX:+CMSIncrementalMode"
JAVA_OPTS="$JAVA_OPTS -XX:+CMSIncrementalPacing"
JAVA_OPTS="$JAVA_OPTS -XX:+CMSParallelRemarkEnabled"
JAVA_OPTS="$JAVA_OPTS -XX:+UseParNewGC"
JAVA_OPTS="$JAVA_OPTS -XX:PermSize=64m"
JAVA_OPTS="$JAVA_OPTS -XX:MaxPermSize=64m"
JAVA_OPTS="$JAVA_OPTS -XX:+UseTLAB"
# Enable class unloading (needed with ConcMarkSweepGC
JAVA_OPTS="$JAVA_OPTS -XX:+CMSClassUnloadingEnabled"
JAVA_OPTS="$JAVA_OPTS -XX:+CMSPermGenSweepingEnabled"
# debug and memory tweaks to avoid Hotspot Compiler Failure
JAVA_OPTS="$JAVA_OPTS -XX:+PrintCompilation"
JAVA_OPTS="$JAVA_OPTS -XX:CodeCacheMinimumFreeSpace=2M"
JAVA_OPTS="$JAVA_OPTS -XX:ReservedCodeCacheSize=64M"
JAVA_OPTS="$JAVA_OPTS -XX:CompileCommandFile=/my/portal/bin/hotspot_compiler"
# Enable JMX Remote Monitoring
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote"
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.port=9000"
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.ssl=false"
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.password.file=/my/portal/bin/jmxremote.password"
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.access.file=/my/portal/bin/jmxremote.access"
# turn on some debug for GC only print the distribution when doing tuning
JAVA_OPTS="$JAVA_OPTS -verbose:gc"
JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCTimeStamps"
JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCDetails"
#JAVA_OPTS="$JAVA_OPTS -XX:+PrintTenuringDistribution"
JAVA_OPTS="$JAVA_OPTS -Xloggc:/my/portal/logs/portal/gc.log"
# Enable remote debugging port
JAVA_OPTS="$JAVA_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"
Tuesday, 14 July 2009
Servlet Filter
Redirecting a request
Another use for filters is to restrict access to resources - if a filter doesn't call FilterChain.doFilter(), the resource won't be loaded. Simply returning would send an empty document to the browser, so it's better to redirect or forward the request to a different resource. As with a servlet, there are two ways to do this. HttpServletReponse.sendRedirect() actually sends a response to the browser giving it a URL to fetch instead of the original one, which may be on a different server. The other option is to use a RequestDispatcher to load a different resource on the server instead of the resource which would have been loaded, transparently to the browser.
Unfortunately the Tomcat project doesn't provide any useful examples for this, so I'm reduced to writing my own code. SecureFilter checks the current session to see whether there is a User object; if not, it forwards the request to a login form. While hardly a robust component, you can see how a more useful system could be implemented.
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
boolean authorized = false;
if (request instanceof HttpServletRequest) {
HttpSession session = ((HttpServletRequest)request).getSession(false);
if (session != null) {
User user = (User) session.getAttribute("user");
if (user != null)
authorized = true;
}
}
if (authorized) {
chain.doFilter(request, response);
return;
} else if (filterConfig != null) {
String login_page = filterConfig.getInitParameter("login_page");
if (login_page != null && !"".equals(login_page)) {
filterConfig.getServletContext().getRequestDispatcher(login_page).
forward(request, response);
return;
}
}
throw new ServletException
("Unauthorized access, unable to forward to login page");
}
Threading issues
Wednesday, 18 March 2009
Difference of Path convention in Unix and Win/Dos
\" (the famous "backslash") to represent the "root" directory, and as a separator between directory levels, UNIX uses / (Shall we call it "forwardslash"?). This character is found on the same key as the questionmark ("?") on most keyboards.
Thursday, 9 October 2008
Database: Functional Dependencies and Normalization
- Design each relation schema so that it is easy to explain its meaning. Intuitively, each schema should correspond to one entity or one relationship type in the real life.
- Design relation schema so that no insertion, deletion, or modification anomalies are present.
Can Jackson Deserialize Java Time ZonedDateTime
Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...
-
The strictfp keyword is used to force the precision of floating point calculations (float or double) in Java conform to IEEE’s 754 standard...
-
In construction. Spring test annotations: @SpringBooTest @DataJpaTest @TestPropertySource @ActiveProfiles @Sql @SpringBootTest It is used f...
-
Testing Controllers @Runwith(SrpingRunner.class) Unit test @Runwith(SrpingRunner.class) @WebMvcTest(SpecificController.class) Integration te...