Page 2 of 2 FirstFirst 12
Results 21 to 38 of 38
  1. #21
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    And it has been explained. If these "operations" are using resources (mainly IO type resources, but other system resources also apply) and the "operations" are not properly designed to handle a "short-circuit" and cleanup after themselves, you have a problem.

  2. #22
    charan reddy is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    oops,,,ok so i understood now....so if there is a dedicated thread which invoked a method xyz() and in xyz() you have utilized lot of memory and resources you have created are still live even after successfull execution of xyz() method and also thread successfully completes it execution....eventhough thread has completed and exited out succesfully(not killed) the open resources will be there with out eligible for garbage collection..thats what u say?

  3. #23
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    If they are not closed properly, yes. They may be "eligable" for GarbageCollection, from the point of view of any of your references to it, that doesn't mean it will necessarily be successful at it though, nor that the system does not have references to it, when they haven't been properly closed.

  4. #24
    charan reddy is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    well ok thanks..thats strange....even aftyer a thread finishes execution..i was under belief that all resources used by that thread instance should be eligible...huff :) but its not the case it seems as u are saying..

  5. #25
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Not if it has open resources. Those will cause problems. If they are closed properly, and the thread "exits" normally, no problem, otherwise ...

  6. #26
    charan reddy is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    well ok...pointed noted friend...welll let me get in touch with people who programmed jobs...will ask them to refine jobs to release all resources used by it..and then get back to u on whats the update....
    thanks for now,...we will the discussion after 3 days,,,giv me sometime...it was really good conversation with u frend

  7. #27
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Also, if these "operations" are in there own jarfiles (or their packages starting froma a specific directory), then you should create a URLClassloader to this file/directory and set that classloader as the context ClassLoader for the thread. Otherwise, any "static" resources will, of course, remain as those are tied to the loaded classes, and those loaded classes will only be "unloaded" when the ClassLoader that loaded them goes out of scope.
    Last edited by masijade; 10-03-2009 at 03:47 PM.

  8. #28
    charan reddy is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    the framework already dynmically instantiates the class using class.forname...and then executes the method it in ,,,,,so are u talking about that?

  9. #29
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    No, that loads the class in the current ClassLoader and those classes will remain loaded (although the Classes themselves are PermGen space and not Heap space), and all static items related to those classes will remain loaded and instantiated as long as that class is loaded. Do something to the effect of
    Java Code:
    public void run() {
      URLClassLoader ucl = new URLClassLoader(<url to classes>);
      currentThread.setContextClassLoader(urlc);
      urlc.loadClass(<class>); // instead of Class.forName();
      // continue
    }
    Then, as soon as the thread actually exits, that ClassLoader will go out of scope and all loaded classes and static items will also be released, which they won't be if you don't do this.

  10. #30
    charan reddy is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    Since i need to dynamically instantiate the classes deployed in framework on the fly , i am using class.forName().Its mandatory in my scenario. So is it ok if i add little tweak to above code provided by you?

    public class CreateInstance {

    public static IGeneric getObject(String classname,Thread t) throws ClassNotFoundException, InstantiationException, IllegalAccessException{

    Class cls = Class.forName(classname); // classname variable holds a string value like com.xyz.Helloworld
    ClassLoader loader = cls.getClassLoader();
    t.setContextClassLoader(loader);
    return (IGeneric)cls.newInstance(); // IGeneric is an interface
    }

    public void run(){
    IGeneric ig = CreateInstance.getObject(jobCodePath,Thread.curren tThread());
    jobErrorCode=ig.runJob(jobsession);
    .
    .
    ..
    ...
    }


    Is the tweaks done ok for you ?
    Last edited by charan reddy; 10-06-2009 at 08:22 PM.

  11. #31
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Then create the classloader first and use the forName that takes a classloader as the second parameter (loadclass does exactly the same thing as forname, by the way) and pass it that classloader, or the class, and all classes that subsequently get loaded, are still loaded in the current context, which completely defeats the purpose of creating the classloader.

  12. #32
    charan reddy is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Question

    hey jade...THANKS FOR UR TIMELY REPLY......is this below changes fine?


    -----------------------SKELETON CODE--------------------------


    public class CreateInstance {

    public IGeneric getObject(String classname,Thread t) throws ClassNotFoundException, InstantiationException, IllegalAccessException{

    Class cls = null;

    try{

    ClassLoader loader = this.getClass().getClassLoader();
    cls = Class.forName(classname,true,loader);
    t.setContextClassLoader(loader);
    }
    catch(Exception ex){}

    return (IGeneric)cls.newInstance();

    }

    }




    public class Controller implements Runnable {

    public void run(){
    IGeneric ig = new CreateInstance().getObject(jobCodePath,Thread.curr entThread());
    jobErrorCode=ig.runJob(jobsession);
    ig=null;
    .
    .
    ..
    ...
    }

    }

  13. #33
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Looks better. To be safe I would place the setcontextloader before the forname call, though.

  14. #34
    charan reddy is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    hey jade,

    I am again running in to heap space issues.. :(...! anyother approach?

  15. #35
    charan reddy is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Question

    this issue came even after the above suggestion made is done....!
    Can you let me know is there any other approach?
    Onething we noticed is...this happens only when that thread has huge amount data needs to be processed.....so s thread if on heavy load is taking 100MB of memory..its not releasing after thread completes sucessfully....but on low loads...i could see memory is released...

    so i think may be GC is not running when it is really required...

    any idea why??

    should we look in to Tuning GC..?

    Note:
    Also we allocated 1GB to the JVM when we spawned the java process...using below
    java -Xms512m -Xmx1024m

    Looking for a good solution from you for this...
    Last edited by charan reddy; 10-07-2009 at 03:12 PM.

  16. #36
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    How are you checking that "memory is released" or not. Hopefully you're using some kind profiler and not looking at system memory usage, as that has absolutely nothing to do with how much of the heap space is currently in use. It does have a little something to do with the maximum amount that was in use at some point, but even then it is not an accurate depiction.

    GC is running, in fact it must run before the JVM is allowed to throw a Heap Space exception (I seem to say this a lot recently). So, either you still have a reference to something, somewhere (a reference to thread itself is probably enough), or the thread has not exited.

    Use a profiler and/or debugger and find out what is really happening. At this point you are poking around in the dark and that is usually destined to fail.

  17. #37
    charan reddy is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Question

    I am checking memory using this Runtime.getRuntime().freeMemory() and writing to log file at certain check points.

    Also, the thread which invokes these Heavy Weight Jobs are exiting successfully and cleanly and i am not killing it. So considering the classloader snippet added should make sure that the after thread completes successfully , the resources allocated to the heavy weight job should be released,...but thats not happening....

    Also i am making sure that i explicitly making thread reference was made to null after thread completes successfully...So for next job invocation a new thread is created and made to null after the job completes successfully,,,so i was NOT making the thread to live after job completes.

    Also,.,During minimal load,,,,i could see memory stats...and its working fine....

    I complete agree if teh above issue happens if i am killing a thread abruptly but i am not doing it anymore,.,!

  18. #38
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    I can't tell you anything more than I have already have. Use a debugger, or profiler, and find out exactly what's happening.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Problem with Java Heap Space using Socket
    By mvillara in forum Networking
    Replies: 3
    Last Post: 08-21-2009, 04:45 AM
  2. Java heap space OutofMemoryError
    By everlast88az in forum Advanced Java
    Replies: 3
    Last Post: 05-18-2009, 08:12 AM
  3. Java Heap Space
    By sandeeprao.techno in forum Advanced Java
    Replies: 19
    Last Post: 10-30-2008, 11:27 AM
  4. Java heap space error
    By gezzel in forum New To Java
    Replies: 19
    Last Post: 09-25-2008, 12:07 AM
  5. Java heap space?
    By javanewbie in forum New To Java
    Replies: 1
    Last Post: 06-24-2008, 06:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •