Monday, 21 September 2009

HttpURLConnection: java.net.ConnectException: Connection timed out

CodeGuru Forums > Java Programming > Java Programming > HttpURLConnection: java.net.ConnectException: Connection timed out:


PDA

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



          Using jconsole



          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



          VisualVM - tool for profiling Java applications

          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"

          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...