One garbage collection question

suppose i have allocated 2 GB space for heap. And my program is maximum going to take 50 MB in the heap. Will it make sure that the garbage collector will not start since i am using much less space than allocated.

JoachimSauer wrote:
shashi_rajak wrote:
What is the requirement here?Some one ask me this question in an interview for meryll lynch (BofA).I see. That's actually a pretty nice interview question (depending on how the answer is interpreted of course), because it shows if you know about the behavior of the GC in practice and not just how it should work in theory. The other question is if that knowledge is actually necessary for anything ;-)I'd say it can be useful. If you know the above general GC behavior, know that there exist parameters to tune it, and are able to do the research to learn the details of what those parameters and their effects are, you now have the tools to influence the runtime behavior of your app to meet, or at least approach, your requirements. Obviously there's always a lot if indeterminacy in GC, but depending on the nature of the app and the tuning particulars, there's a good chance you can make it "better" than if the JVM were just left to its defaults.

Similar Messages

  • [scjp exam] garbage collection questions

    can someone please post me some garbage collection questions + correct answers, because i've taken the SCJP test again and i didn't passed it again. A co-worker of mine also didn't passed either.
    (We both got 0% on Garbage Collection every time) And i really thought i had it correct the last time.
    So please can someone mail/post me some garbage collection questions? thanks!

    The garbage collector collects unreachable objects.
    You have no control over when the objects are
    collected, but there is a guarantee that all
    unreachable objects will be collected before an
    OutOfMemoryError is thorwn.Exactly my thoughts...
    That's it. It really is just that simple. I scored 94%
    on the Programmer's Exam, with 100% in the garbage
    collection section.Hmm i got 54% last exam i took. I really begin to doubt about myself, because i find the questions on the exam so tricky.. Last book was Exam Cram (Java), and i found this a very nice and good book to study for the exam. Garbage collection is just one question, so it's either 0% or 100%. So far it hasn't gone anything higher then 0% with me.
    Here are some questions:
    1. When is the Object first available for garbage
    collection (when does it become unreachable)?
    Object o = new Object();
    Object p = o;
    o = null;
    p = null;p = null; -> available for gc.
    2. When is the Object first available for garbage
    collection?
    Object o = new Object();
    Vector v = new Vector();
    v.add(o);
    o = null;
    v = null;v = null; -> available for gc.
    3. Can the Vectors be garbage collected at the end of
    this code?
    Vector v = new Vector();
    Vector w = new Vector();
    v.add(w);
    w.add(v);
    v = null;
    w = null;yes
    Now i have a question:
    public int foo {
    Integer result = new Integer(10);
    result = null;
    return result;
    when is result available for gc?

  • JNI / Garbage Collection question

    I have a C++ library I am calling into from our application. The question I have is I want the library to return a jintarray or a jstringarray (not sure which yet). If they create that array in the function and then return it to me as the return type, will the Garbage collector automatically free the memory for that array once I am done using it, or do I need to manually free it some how since the array was created in the c code?
    Thanks,
    Jeffrey Haskovec

    Java objects whether they are create in java or JNI are all managed by the VM.

  • Garbage collection question

    I'm developing a cash card app which will need to store transactions on the card. Basically it may make several cash transactions with terminals that are not connected to the internet (and thus can't sync) and once in a while the user will interface with a terminal that is connected to the internet, and will be able to sync the account.
    My plan is define a Transaction object, which will store the account number of the other person's account, the transaction amount, possibly the time of the transaction, and possibly a transaction id. A new Transaction object will be created on the card every time there's a transaction, and when the user syncs, all Transaction objects will be deleted from the card.
    However, it has come to my attention that there is no garbage collector in Java Cards, or at least most Java Cards. In a typical desktop Java app, one would dynamically create Transaction objects as needed and let the garbage collector delete the old ones. Can something like this be implemented on a Java Card? Is the answer to allocate a buffer of say, 20 Transaction objects in the constructor and just keep reusing them? That would make for some ugly code.
    On a side note, if I wanted to record the time of every transaction, is there something like System.getCurrentTimeMillis() in Java Card, or would I have to send the time from the host app?

    Sarevok wrote:
    I'm developing a cash card app which will need to store transactions on the card. Basically it may make several cash transactions with terminals that are not connected to the internet (and thus can't sync) and once in a while the user will interface with a terminal that is connected to the internet, and will be able to sync the account.
    My plan is define a Transaction object, which will store the account number of the other person's account, the transaction amount, possibly the time of the transaction, and possibly a transaction id. A new Transaction object will be created on the card every time there's a transaction, and when the user syncs, all Transaction objects will be deleted from the card.
    However, it has come to my attention that there is no garbage collector in Java Cards, or at least most Java Cards. In a typical desktop Java app, one would dynamically create Transaction objects as needed and let the garbage collector delete the old ones. Can something like this be implemented on a Java Card? Is the answer to allocate a buffer of say, 20 Transaction objects in the constructor and just keep reusing them? That would make for some ugly code.You will need to use a buffer of objects. It is not perfect but it is efficient. The code for this isn't too bad. You just need a pointer to the current buffer location.
    On a side note, if I wanted to record the time of every transaction, is there something like System.getCurrentTimeMillis() in Java Card, or would I have to send the time from the host app?There is no clock on a Java Card based smart card so you will have to rely on the host application for the current time.

  • Garbage collection Java Virtual Machine : Hewlett-Packard Hotspot release 1.3.1.01

    "Hi,
    I try and understand the mechanism of garbage collection of the Java Virtual Machine : Hewlett-Packard Hotspot release 1.3.1.01.
    There is description of this mechanism in the pdf file : "memory management and garbage collection" available at the paragraph "Java performance tuning tutorial" at the page :
    http://h21007.www2.hp.com/dspp/tech/tech_TechDocumentDetailPage_IDX/1,1701,1607,00.html
    Regarding my question :
    Below is an extract of the log file of garbage collections. This extract has 2 consecutive garbage collections.
    (each begins with "<GC:").
    <GC: 1 387875.630047 554 1258496 1 161087488 0 161087488 20119552 0 20119552
    334758064 238778016 335544320
    46294096 46294096 46399488 5.319209 >
    <GC: 5 387926.615209 555 1258496 1 161087488 0 161087488 0 0 20119552
    240036512 242217264 335544320
    46317184 46317184 46399488 5.206192 >
    There are 2 "full garbage collections", one of reason "1" and one of reason "5".
    For the first one "Old generation After " =238778016
    For the second "Old generation After " =238778016
    Thus, "Old generation Before garbage collection" of the second is higher than "Old generation After garbage collection". Why?
    I expected all objects to be allocated in the "Eden" space. And therefore I did not expect to s

    I agree but my current Hp support is not very good on JVM issues.
    Rob Woollen <[email protected]> wrote:
    You'd probably be better off asking this question to HP.
    -- Rob
    Martial wrote:
    The object of this mail is the Hewlett-Packard 1.3.1.01 Hotspot JavaVirtual Machine
    release and its garbage collection mechanism.
    I am interested in the "-Xverbosegc" option for garbage collectionmonitoring.
    I have been through the online document :
    http://www.hp.com/products1/unix/java/infolibrary/prog_guide/java1_3/hotspot.html#-Xverbosegc
    I would like to find out more about the garbage collection mechanismand need
    further information to understand the result of the log file generatedwith the
    "-Xverbosegc"
    For example here is an extract of a garbage collection log file generatedwith
    Hewlett-Packard Hotspot Java Virtual Machine. Release 1.3.1.01.
    These are 2 consecutive rows of the files :
    <GC: 5 385565.750251 543 48 1 161087488 0 161087488 0 0 20119552 264184480255179792
    335544320 46118384 46118384 46137344 5.514721 >
    <GC: 1 385876.530728 544 1258496 1 161087488 0 161087488 20119552 020119552 334969696
    255530640 335544320 46121664 46106304 46137344 6.768760 >
    We have 2 full garbage collections, one of Reason 5 and the next oneof Reason
    1.
    What happened between these 2 garbage collections as we got : "Oldgeneration
    After" of row 2 is higher than "Old generation Before" of row 1? Iexpected Objects
    to be initially allocated in eden and so we could not get "old generation2modified
    between the end of one garbage collection and before the next one.
    Could you please clarify this issue and/or give more information aboutgarbage
    collection mechanisms with the Hewlett-Packard Hotspot Java VirtualMachine. Release
    1.3.1.01.

  • Swing Transferhandler prevents garbage collection of one Component

    While searching for memory leaks in my app I found out that an object wasn't garbage collected because of a static reference from javax.swing.TransferHandler.
    Once an app has called TransferHandler.exportAsDrag, TransferHandler keeps alive one static reference to a SwingDragGestureRecognizer.
        public void exportAsDrag(JComponent comp, InputEvent e, int action) {
            int srcActions = getSourceActions(comp);
            // only mouse events supported for drag operations
         if (!(e instanceof MouseEvent)
                    // only support known actions
                    || !(action == COPY || action == MOVE || action == LINK)
                    // only support valid source actions
                    || (srcActions & action) == 0) {
                action = NONE;
            if (action != NONE && !GraphicsEnvironment.isHeadless()) {
             if (recognizer == null) {
              recognizer = new SwingDragGestureRecognizer(new DragHandler());
                recognizer.gestured(comp, (MouseEvent)e, srcActions, action);
         } else {
                exportDone(comp, null, NONE);
    ....As you can see there is a call to recognizer.gestured(comp, (MouseEvent)e, srcActions, action). This object saves a reference to the component comp.
    This means that the last Component for which the exportAsDrag method was called (and the gestured call was reached); and all objects refererred to by the Component can not be garbage collected.
    I think in a normal application this will be not much of a problem because it's only a reference to a single Component; but still I think this is not very nice.
    Should I report this as a bug somewhere?

    Something similar seems to happens to JInternalFrame.lastFocusOwner
    Oh well, I guess I shouldn't worry too much about single references; I don't think they immediately cause memory leaks..

  • A question about JNI references, persistence, and garbage collection

    I am writing a library to allow our Java developers to access C functions in our product. I want people on the Java side to be able to instantiate instances of a new object (on the C side), say "Map", and I want those objects to persist until they are destroyed on the Java side. I am thinking of creating a wrapper for the native methods, which stores an identifier for the instance of an object. When the C object is accessed, the identifier is passed to the C code which looks up the correct object in a table. I understand that if I use the NewGlobalReference call in JNI I can get objects to persist.
    When it is time for garbage collection I plan to override finalize() to call the C code and tell it to remove the Global Reference.
    Here's what I have in mind
    public class JMap() {
         private static int id;
         static {
              System.loadLibrary("libMap");
              /*Call C method which instantiates object and creates a GlobalReference
              which is stored in table. Returns ID for reference*/
              id = _init();
         public void setSize(int x, int y) {
              _setSize(id, x, y);
         public void finalize() {
              _finalize(id);
              super.finalize();
         private native int _init();
         /*calls DeleteGlobalReference for the reference matching this id*/
         private native void _finalize(int id);
         private native void _setSize(int id, int x, int y);
    }Is this the right thing to do? Have I understood the way JNI works with regard to GlobalReferences?
    Any comments, tips or pointers would be appreciated :)

    This probably should have been posted in the Native Methods sub-forum, I have reposted there:
    http://forum.java.sun.com/thread.jspa?threadID=657667
    Mods please delete this post :)

  • High cpu usage for garbage collection (uptime vs total gc time)

    Hi Team,
    We have a very high cpu usage issue in the production.
    When we restart the server, the cpu idle time would be around 95% and it comes down as days goes by. Today idle cpu is 30% and it is just 6th day after the server restart.
    Environemnt details:
    Jrockit version:
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_05-b04)
    BEA WebLogic JRockit(TM) 1.4.2_05 JVM R24.4.0-1 (build ari-38120-20041118-1131-linux-ia32, Native Threads, GC strategy: parallel)
    Gc Algorithm: JRockit Garbage Collection System currently running strategy: Single generational, parallel mark, parallel sweep
    Number Of Processors: 4
    Max Heap Size: 1073741824
    Total Garbage Collection Time: 21:43:56.5
    Uptime: 114:33:4.1
    Total Garbage Collection Count: 420872
    Total Number Of Threads: 198
    Number Of Daemon Threads: 191
    Can you guys please tell me what would be problem in the server which causing the high cpu usage?
    One more thing I would like to know is that why the total number of threads is 198 when we specified the Executor pool size as 25? I agree that weblogic would create some threads for its maintenance but around 160 threads!!! something is wrong I guess.
    Santhosh.
    [email protected]

    Hi,
    I'm having a similar problem, but haven't been able to resolve it yet. Troubleshooting is made even harder by the fact that this is only happening on our production server, and I've been unable to reproduce it in the lab.
    I'll post whatever findings I have and hopefully we'll be able to find a solution with the help of BEA engineers.
    In my case, I have a stand-alone Tomcat server that runs fine for about 1-2 days, and then the JVM suddenly starts using more CPU, and as a result, the server load shoots up (normal CPU utilization is ~5% but eventually goes up to ~95%; load goes from 0.1 to 4+).
    What I have found so far is that this corresponds to increased GC activity.
    Let me list my environment specs before I proceed, though:
    CPU: Dual Xeon 3.06GHz
    RAM: 2GB
    OS: RHEL4.4 (2.6.9-42.0.2.ELsmp)
    JVM build 1.5.0_03-b07 (BEA JRockit(R) (build dra-45238-20050523-2008-linux-ia32, R25.2.0-28))
    Tomcat version 5.5.12
    JAVA_OPTS="-Xms768m -Xmx768m -XXtlasize16k -XXlargeobjectlimit16k -Xverbose:memory,cpuinfo -Xverboselog:/var/log/tomcat5/jvm.log -Xverbosetimestamp"
    Here are excerpts from my verbose log (I'm getting some HT warning, not sure if that's a problem):
    [Fri Oct 20 15:54:18 2006][22855][cpuinfo] Detected SMP with 2 CPUs that support HT.
    [Fri Oct 20 15:54:18 2006][22855][cpuinfo] Trying to determine if HT is enabled.
    [Fri Oct 20 15:54:18 2006][22855][cpuinfo] Trying to read from /dev/cpu/0/cpuid
    [Fri Oct 20 15:54:18 2006][22855][cpuinfo] Warning: Failed to read from /dev/cpu/0/cpuid
    [Fri Oct 20 15:54:18 2006][22855][cpuinfo] Trying to read from /dev/cpu/1/cpuid
    [Fri Oct 20 15:54:18 2006][22855][cpuinfo] Warning: Failed to read from /dev/cpu/1/cpuid
    [Fri Oct 20 15:54:18 2006][22855][cpuinfo] HT is: supported by the CPU, not enabled by the OS, enabled in JRockit.
    [Fri Oct 20 15:54:18 2006][22855][cpuinfo] Warning: HT enabled even though OS does not seem to support it.
    [Fri Oct 20 15:54:55 2006][22855][memory ] GC strategy: System optimized over throughput (initial strategy singleparpar)
    [Fri Oct 20 15:54:55 2006][22855][memory ] heap size: 786432K, maximal heap size: 786432K
    [Fri Oct 20 16:07:30 2006][22855][memory ] Changing GC strategy to generational, parallel mark and parallel sweep
    [Fri Oct 20 16:07:30 2006][22855][memory ] 791.642-791.874: GC 786432K->266892K (786432K), 232.000 ms
    [Fri Oct 20 16:08:02 2006][22855][memory ] 824.122: nursery GC 291998K->274164K (786432K), 175.873 ms
    [Fri Oct 20 16:09:51 2006][22855][memory ] 932.526: nursery GC 299321K->281775K (786432K), 110.879 ms
    [Fri Oct 20 16:10:24 2006][22855][memory ] 965.844: nursery GC 308151K->292222K (786432K), 174.609 ms
    [Fri Oct 20 16:11:54 2006][22855][memory ] 1056.368: nursery GC 314718K->300068K (786432K), 66.032 ms
    [Sat Oct 21 23:21:09 2006][22855][memory ] 113210.427: nursery GC 734274K->676137K (786432K), 188.985 ms
    [Sat Oct 21 23:30:41 2006][22855][memory ] 113783.140: nursery GC 766601K->708592K (786432K), 96.007 ms
    [Sat Oct 21 23:36:15 2006][22855][memory ] 114116.332-114116.576: GC 756832K->86835K (786432K), 243.333 ms
    [Sat Oct 21 23:48:20 2006][22855][memory ] 114841.653: nursery GC 182299K->122396K (786432K), 175.252 ms
    [Sat Oct 21 23:48:52 2006][22855][memory ] 114873.851: nursery GC 195060K->130483K (786432K), 142.122 ms
    [Sun Oct 22 00:01:31 2006][22855][memory ] 115632.706: nursery GC 224096K->166618K (786432K), 327.264 ms
    [Sun Oct 22 00:16:37 2006][22855][memory ] 116539.368: nursery GC 246564K->186328K (786432K), 173.888 ms
    [Sun Oct 22 00:26:21 2006][22855][memory ] 117122.577: nursery GC 279056K->221543K (786432K), 170.367 ms
    [Sun Oct 22 00:26:21 2006][22855][memory ] 117123.041: nursery GC 290439K->225833K (786432K), 69.170 ms
    [Sun Oct 22 00:29:10 2006][22855][memory ] 117291.795: nursery GC 298947K->238083K (786432K), 207.200 ms
    [Sun Oct 22 00:39:05 2006][22855][memory ] 117886.478: nursery GC 326956K->263441K (786432K), 87.009 ms
    [Sun Oct 22 00:55:22 2006][22855][memory ] 118863.947: nursery GC 357229K->298971K (786432K), 246.643 ms
    [Sun Oct 22 01:08:17 2006][22855][memory ] 119638.750: nursery GC 381744K->322332K (786432K), 147.996 ms
    [Sun Oct 22 01:11:22 2006][22855][memory ] 119824.249: nursery GC 398678K->336478K (786432K), 93.046 ms
    [Sun Oct 22 01:21:35 2006][22855][memory ] 120436.740: nursery GC 409150K->345186K (786432K), 81.304 ms
    [Sun Oct 22 01:21:38 2006][22855][memory ] 120439.582: nursery GC 409986K->345832K (786432K), 153.534 ms
    [Sun Oct 22 01:21:42 2006][22855][memory ] 120443.544: nursery GC 410632K->346473K (786432K), 121.371 ms
    [Sun Oct 22 01:21:44 2006][22855][memory ] 120445.508: nursery GC 411273K->347591K (786432K), 60.688 ms
    [Sun Oct 22 01:21:44 2006][22855][memory ] 120445.623: nursery GC 412391K->347785K (786432K), 68.935 ms
    [Sun Oct 22 01:21:45 2006][22855][memory ] 120446.576: nursery GC 412585K->348897K (786432K), 152.333 ms
    [Sun Oct 22 01:21:45 2006][22855][memory ] 120446.783: nursery GC 413697K->349080K (786432K), 70.456 ms
    [Sun Oct 22 01:34:16 2006][22855][memory ] 121197.612: nursery GC 437378K->383392K (786432K), 165.771 ms
    [Sun Oct 22 01:37:37 2006][22855][memory ] 121398.496: nursery GC 469709K->409076K (786432K), 78.257 ms
    [Sun Oct 22 01:37:37 2006][22855][memory ] 121398.730: nursery GC 502490K->437713K (786432K), 65.747 ms
    [Sun Oct 22 01:44:03 2006][22855][memory ] 121785.259: nursery GC 536605K->478156K (786432K), 132.293 ms
    [Sun Oct 22 01:44:04 2006][22855][memory ] 121785.603: nursery GC 568408K->503635K (786432K), 71.751 ms
    [Sun Oct 22 01:50:39 2006][22855][memory ] 122180.985: nursery GC 591332K->530811K (786432K), 131.831 ms
    [Sun Oct 22 02:13:52 2006][22855][memory ] 123573.719: nursery GC 655566K->595257K (786432K), 117.311 ms
    [Sun Oct 22 02:36:04 2006][22855][memory ] 124905.507: nursery GC 688896K->632129K (786432K), 346.990 ms
    [Sun Oct 22 02:50:24 2006][22855][memory ] 125765.715-125765.904: GC 786032K->143954K (786432K), 189.000 ms
    [Sun Oct 22 02:50:26 2006][22855][memory ] 125767.535-125767.761: GC 723232K->70948K (786432K), 225.000 ms
    vvvvv
    [Sun Oct 22 02:50:27 2006][22855][memory ] 125768.751-125768.817: GC 712032K->71390K (786432K), 64.919 ms
    [Sun Oct 22 02:50:28 2006][22855][memory ] 125769.516-125769.698: GC 711632K->61175K (786432K), 182.000 ms
    [Sun Oct 22 02:50:29 2006][22855][memory ] 125770.753-125770.880: GC 709632K->81558K (786432K), 126.000 ms
    [Sun Oct 22 02:50:30 2006][22855][memory ] 125771.699-125771.878: GC 708432K->61368K (786432K), 179.000 ms
    So, I'm running with the default GC strategy which lets the GC pick the most suitable approach (single space or generational). It seems to switch to generational almost immediately and runs well - most GC runs are in the nursery, and only once in a while it goes through the older space.
    Now, if you look at [Sun Oct 22 02:50:27 2006], that's when everything changes. GC starts running every second (later on it's running 3 times a second) doing huge sweeps. It never goes through the nursery again, although the strategy is still generational.
    It's all downhill from this point on, and it's a matter of hours (maybe a day) before we restart the server.
    I guess my only question is: What would cause such GC behavior?
    I would appreciate your ideas/comments!
    Thanks,
    Tenyo

  • SystemManager and Garbage Collection

    Hi everyone, I have a question regarding the SystemManager and Garbage Collection. I have and application that loads in its assets via a swc created in Flash. In that swc I have different MovieClips that act as the different screens of my application each one being tied to its own custom class. For example one MovieClip is a login screen another is the main menu etc. These screens contain components, text fields and animations. The problem that I am having is when I move from one screen to the other the garbage collector is not cleaning up everything. There are still references to the MovieClips that have animations for example. So even though I remove the screen via removeChild and set the variable reference to that object to null it is not releasing the MovieClips. When I pull it up in the profiler it shows me that SystemManager is holding references to them. Also if I debug the applicaion and look inside the instance of the MovieClip I can see that the private property "listeners" has values, but I am not adding listeners. It appears that the SystemManager is adding listeners. Does anyone know how I can clear these listeners or force the SystemManager to release these items. Any ideas or help would be greatly appreciated. I am fairly new to dealing with memory management and garbage collection in Flex. I am using Flash CS4 to create the swc and Flash Builder 4 Beta with the 3.4 framework and Flash Player 10 to create the app. If you need me to clarify any of this please let me know. Again any help or ideas on where to go from here would be great!

    This chain says that the focusManager is referencing UserMenu.  Is there a default button or focusable objects in UserMenu and references from UserMenu to the objects in question?
    BTW, the CS4 fl.managers.FocusManager and any fl.. classes are incompatible with Flex, so make sure you're not using them in your MovieClips.
    Alex Harui
    Flex SDK Developer
    Adobe Systems Inc.
    Blog: http://blogs.adobe.com/aharui

  • When will wls try to garbage collect ?

    The docs for the option "Low memory GC threshold" say that this is the "Threshold
    level at which WLS will try to garbage collect once the granularity report has
    been met".
    My question is: When is this condition met ? If at least once there has been a
    granularity report in the log ? Or if the server has been set to Warning state
    The problem Im trying to address is that my "Low memory GC threshold" is set to
    20% and my server has reached only 1% free memory and garbage collection did not
    run, I had to force it via console.
    Thanks in advance, Giselle

    I am wonder when the WLS activate or passivate session and how can I          control it?
              > From documents, sessionWillPassivate in HttpSessionActivationListener will
              be
              > executed when a session is about to passivate on one server (ie. WLS
              instance)
              > and the sessionDidActivate method when the same session has been activated
              on
              > a second server.
              >
              > But I have monitor the behavior for a long time by setting
              PersistentStoreType
              > to file, memory, replicated. sessionDidActivate and sessionWillPassivate
              will
              > never executed. I expected it will passivate and activate when session is
              replicated
              > between servers or persist in files. Any ideas?
              What version of WL? the interface you describe was new in Servlet 2.3, so it
              may not be implemented until 6.1 or even 7.x.
              Peace,
              Cameron Purdy
              Tangosol, Inc.
              http://www.tangosol.com/coherence.jsp
              Tangosol Coherence: Clustered Replicated Cache for Weblogic
              "Karen Law" <[email protected]> wrote in message
              news:3ec86188$[email protected]..
              >
              

  • Nested object reference after garbage collection

    I have a question about how GC works.
    Assume I have a Class A which has a reference to class B inside it. I create a object of class B and assign to member variable of class A.
    Now when obja is no longer needed, I set it null. And when GC runs, the object will be removed from memory. But should I also need to explicitly set objb to null. If i don't set it, will it also get garbage collected when obja is being removed from memory?
    public class ClassA {
    private ClassB objB = new ClassB();
    private static void main(String args[]) {
    ClassA obja = new ClassA();
    obja = null;
    }

    801625 wrote:
    But should I also need to explicitly set objb to null.No.
    If i don't set it, will it also get garbage collected when obja is being removed from memory?If there are no other references outside of classA to the object referenced by objB (in your case an instance of ClassB) then it will be garbage collected.
    Note - the only time one needs to set a reference to null is if one wants the object it references to be eligible for GC before the reference goes out of scope. There is a slight complication to this - depending on the JVM implementation, if a reference is defined in a block internal to a method then even though it goes out of scope when execution goes out of the block anything it references may not be eligible for GC until the method exits.

  • Help needed!! Novice with Garbage Collection problems.

    Hi Guys,
    Really hoping somebody can help me here. I am a relative novice when it comes to all things Java but i am slowly trying to learn. I have come across an issue which i have identified but i am just not sure what to do about it.
    Ok, in a nut shell the issue seems to be revolving around the frequency of garbage collection. From the default-err.log file i am seeing (on average) an Allocation Failure occur every 2 secs. Here is a sample from the log with verbose:gc active:
    <AF[4986]: Allocation Failure. need 208480 bytes, 78 ms since last AF>
    <AF[4986]: managing allocation failure, action=2 (559165976/1342176248)>
    <GC: Mon Oct 11 11:51:12 2004
    <GC(4986): freed 4101528 bytes in 1559 ms, 41% free (563267504/1342176248)>
    <GC(4986): mark: 1301 ms, sweep: 258 ms, compact: 0 ms>
    <GC(4986): refs: soft 0 (age >= 32), weak 0, final 0, phantom 0>
    <AF[4986]: completed in 1563 ms>
    <AF[4987]: Allocation Failure. need 208536 bytes, 78 ms since last AF>
    <AF[4987]: managing allocation failure, action=2 (559138336/1342176248)>
    <GC: Mon Oct 11 11:51:14 2004
    <GC(4987): freed 4105128 bytes in 1563 ms, 41% free (563243464/1342176248)>
    <GC(4987): mark: 1293 ms, sweep: 270 ms, compact: 0 ms>
    <GC(4987): refs: soft 0 (age >= 32), weak 0, final 0, phantom 0>
    <AF[4987]: completed in 1563 ms>
    As you can see, allocation failures are occuring all the time, and with 2secs between events, and each GC taking around 1.5secs, i am having massive problems with the response of the server. It seems that the javaw.exe process is just pegged at 100% CPU the whole time and then it will eventually grind to a halt, and the users will get terrible response times.
    OK, the questions are:
    - Even to me (a novice) the above extract from the log doesnt look good. Am i right?
    - what would be causing this? (i know - how long is a peice of string but i am hoping somebody can point me in the right direction so i can look some more)
    - what can i do about it? Is there any parameters i can put into the java args to help me out?
    Currently i am running -Xms of 128meg and an -Xmx of 1024m with no other settings. There are at the moment about 200users logged onto this server concurrently, and it seems to die a couple of hours into them all being logged on. I then have to kick everybody out and reboot to get it in a working state again.
    I am in some serious need of help from some gurus!! any help would be invaluable, thanks heaps guys.
    Tim

    Hi Again guys, thanks for all your replies.
    I have been working my butt of on this issue and i just cant seem to get anywhere... probably due to my complete lack of knowledge on this whole GC thing! :)
    One thing that i have noticed that seems to be very consistant is the fact that as soon as i get an "action=2" in my default-err.log from an allocation failure, thats when the system goes nuts and tends to not recover.
    At all other times it is an action=1, and the system seems to be running OK, but as soon as i get action=2, the time between GC events drop from seconds to miliseconds, and the bytes required just skyrockets. It keeps on this upward spiral till i just have to reboot the box.
    From all my reading, i have found that an action=2 means "2 - The Garbage Collector has tried to allocate out of the wilderness, and failed."
    This is the only item in the logs that i see is directly related to the server performance.
    Can anybody please explain to me (in laymans terms) what the action=2 means, what may cause it and what i should be looking at changing to fix it?
    The extract from the log files (in my first post in this thread) are still valid, as are the min/max memory settings.
    Any help at all would be invaluable.
    Thanks very much.
    Tim

  • Forcing �Mark Sweep� Garbage Collection?

    I have a fairly complex Java server-based application which is experiencing some memory management problems. As the application runs, it will continue to grow over time. If 5 or 6 instances of the application are running concurrently, eventually the server performance is significantly degraded and the application will crash due to a lack of system memory or system swap space.
    When monitoring the application with JConsole, I have noticed that a large number of �Scavenge� garbage collections normally occur. These do not consume much CPU time, but they also do not seem to free much memory. When I manually click the �Perform GC� button, however, that seems to trigger a �MarkSweep� type of garbage collection. This will normally reclaim a huge amount of memory (although it takes a relatively large amount of CPU to perform.)
    Is there a way to cause this �MarkSweep� to occur more frequently? I have read that explicitly calling system.gc() in my application is discouraged, and may not even cause any garbage collection to occur. (Although it appears that JConsole is somehow forcing it to occur.) I have also seen it suggested that I could tweak some of the JVM parameters for heap size and ratios, but this approach seems difficult and problematic for my application.
    I realize that explicitly forcing MarkSweep collections may cause noticeable pauses in my application. Although that behavior is not ideal, it is tolerable � especially if I can control when the pauses occur. Any suggestions or insight would be appreciated.
    Thanks,
    Mike H.

    Seeing the date of this question my answer will not be in use by the guy who asked but for some one else could be.
    I had similar problem - I wanted to release unused memory from the JVM to the OS.
    After some research i found that calling several consequent times System.gc() helps. I do the calls with some pause between them (1-2 seconds). This makes the app to be slower at this particular moment but releases hundreds of Megabytes in my case.
    I tested with this approach and found that it works only when Serial GC is running, but fortunately this is the GC in most of cases that JVM uses.

  • Long pauses during ParNew garbage collection Please Help !

    Hi,
    We are running a server application on an large machine (~120 CPU, ~380 GB Memory).
    After running 1 or 2 hours we suddenly get exorbitant application pause times during garbage collection and a massive cpu usage from the java vm
    We are running on Java 6 (64Bit) with 6GB Heap.
    Concurrent garbage collection is turned on using the parameters:
    -XX:+UseConcMarkSweepGC
    -XX:+CMSParallelRemarkEnabled
    -XX:CMSInitiatingOccupancyFraction=80
    -XX:+DisableExplicitGC
    We turned on verbose garbage collection and are getting the following output:
    1. Normal operation:
    Application time: 217.4656792 seconds
    3180.905: [GC 3180.906: [ParNew
    Desired survivor size 20119552 bytes, new threshold 4 (max 4)
    - age   1:    2843824 bytes,    2843824 total
    - age   2:    2577128 bytes,    5420952 total
    - age   3:    5742024 bytes,   11162976 total
    - age   4:     625672 bytes,   11788648 total
    : 329531K->15764K(353920K), 0.1484379 secs] 2435799K->2122105K(3392144K), 0.1492386 secs]
    Total time for which application threads were stopped: 0.1886810 seconds
    2. The Problem:
    Application time: 2.8858445 seconds
    5008.433: [GC 5008.434: [ParNew
    Desired survivor size 20119552 bytes, new threshold 2 (max 4)
    - age   1:   15837712 bytes,   15837712 total
    - age   2:   12284416 bytes,   28122128 total
    : 348338K->39296K(353920K), 138.5317715 secs] 2487779K->2192551K(3392144K), 138.5327383 secs]
    Total time for which application threads were stopped: 138.5778558 seconds
    Application time: 2.9764564 seconds
    5149.957: [GC 5149.957: [ParNew
    Desired survivor size 20119552 bytes, new threshold 2 (max 4)
    - age   1:    9483176 bytes,    9483176 total
    - age   2:   14499344 bytes,   23982520 total
    : 353920K->39296K(353920K), 231.5110574 secs] 2507175K->2204546K(3392144K), 231.5121011 secs]
    Total time for which application threads were stopped: 231.5257754 seconds
    Application time: 2.7932907 seconds
    5384.277: [GC 5384.278: [ParNew
    Desired survivor size 20119552 bytes, new threshold 4 (max 4)
    - age   1:   10756376 bytes,   10756376 total
    - age   2:    9135888 bytes,   19892264 total
    : 353920K->28449K(353920K), 256.2065591 secs] 2519170K->2207651K(3392144K), 256.2076388 secs]
    Total time for which application threads were stopped: 256.2221463 seconds
    I can't find any significant differences in the log between fast and long running garbage collections.
    I urgently need help in solving this problem !
    What can I do ?

    After the exciting reply I did get on my question, we did some more investigations on the problem and it seems that we finally found the solution to our problem.
    The number of garbage collection threads used by the virtual machine defaults to the number of cpus of the machine.
    This is ok for small machines or machines where the main load is produced by the java application itself.
    In our environment the main load is not produced by the java application but oracle database processes.
    When java tries to do it's garbage collection using 120 threads (# CPU) on the machine which is already overloaded by non java processes, the thread synchronization seems to produce an exorbitant overhead.
    My theory is that spin locking is used on memory access, causing threads to spin while waiting for other blocking threads not getting cpu because of the heavy load on the system.
    The solution is now to limit the number of garbage collection threads.
    We did that on the first try by setting -XX:ParallelGCThreads=8
    For over one day with heavy load no long GC pauses were experienced any more.

  • Garbage Collector Questions

    Howdy all,
    I've got a few questions about flash's garbage collector.  I'm building a  game and have all elements in a container sprite called _gameContainer.
    _gameConatiners contains other sprites such as _hudContainer which  contains more containers such as _gameoverContainer, _menuContainer,  etc.  So there's a whole hierarchy of containers that are all contained  in _gameContainer which is added directly to the document class (not the  stage btw).
    So the whole thing should basically be Stage -> Document Class -> _gameContainer -> etc.
    When the player loses or wants to restart the game, I'm removing all  event listeners (which are defined as weak, but still rather make sure  there's no loose ends), stopping all music/sounds, removing  _gameContainer from the stage, and then setting _gameContainer to null.   From there I am re-calling my init function which creates everything  (_gameContainer, and everything inside of it, as well as creating all the  standard eventlisteners).
    Am I doing everything upside down?  Is this *a* proper way of restarting  a game?  Mind you I dont say "the" proper way since I'm sure there's a  hundred different ways to do this.
    Also, on a separate note... If I have something such as an enemy, I keep all  enemy logic contained in the class linked to the movieclip on the  stage.  Who should be calling add/removechild?  Should I be using a  factory method that takes care of all this, or should I have the engine  create the enemy, and then have the enemy remove itself from stage?   Currently I'm using a mix of both, but generally I'll have a function in  the engine/caller add it to stage, then have the class have an  ADDED_TO_STAGE event listener.  When it comes time to remove the class, I  have it call it's own removeself function such as: (_container is a  reference to it's container as mentioned above such as _hudContainer)
    protected function removeSelf():void
        if(_container.contains(this)) {
            _container.removeChild(this);
        _container.parent.parent.dispatchEvent(new Event(Engine.START_GAME));
    Thanks!

    Wonderful question Travisism.
    The garbage collection is strange.  First I'm curious why you lump displayObjects into _gameConatiners.  Does _gameConatiners get added to the stage at any point?  Why not just add the proper hud at the point its required?
    Anyhow.  By removing listeners ,removeChild(_gameConatiners), and setting _gameConatiners=null does not mean these classes are killed.  null only marks the memory locations as having no more references to them.  When this occurs these objects may be removed from memory.  Why do I say may, that is because it takes a specific amount of memory utilized to trigger the garbage collection.
    Now just merely setting _gameConatiners=null may not ever kill your objects off.  You would be required to profile this and make sure they are dieing properly.  From the sounds of it you have a lot of inner children.
    There is reason to believe that in some cases, when an object is removed from the main stacks that its children will be removed as well.  Though, if the inner workings are so large, often the objects within referencing each other effects the way the garbace collection is stating they are still in use.  Thus keeping these objects alive.
    Its always best to kill off every reference being used.  You can do this by ensuring each object in your movieClip class declared a kill method, and continue trickeling down each object.  This ensures each object will be properly marked.
    As far as your movieClips a factory method is only for the creation of objects, never for the removal.
    You're best bet is to have an object that holds all objects on the stage in a collection.  When you destroy the game. Itterate through this collection and remove them from the stage there.
    This would fit into your engine concept.  There is no reason to use a displayObject for that since its just an object.  Better Yet use a Vector.<DisplayObject> to optimize this.
    Back to your question is this *a* proper way to reset.
    Yeah and No.  The asnwer is based on the memory usage your application eats, and the amount of time to rebuild all objects.  Ideally you should Pool any resources that can be reused versus having to rebuild.
    For example.  If you have a screen that says Game Over.  why would you have to rebuild it on a reset?  There is no information that was changed.   Each clip instantiation takes memory allocated and time.
    Unfortunately without seeing what you have its difficult to say.  But init methods are good and it can be wise to rebuild objects to being again, but as i said it depends.
    Lastly, your line:
    _container.parent.parent.dispatchEvent(new Event(Engine.START_GAME));
    Should not have parent.parent references within.  If you are creating a new Event, at least fall back on eventBubbling to allow the event to travel to the parent.parent.
    You should never target parents like that in a class.  Best practices is to pass in parent.parent as a reference to the class.
    What if you were to add one more layer of parents to the _container.  Then you would have to continue modifying parent.parent.parent.  At least if you bubble the event you dont have to be concerend at all about who listens to it.

Maybe you are looking for

  • Can't get back to project This Project is...... PLEASE HELP!

    "This project is unreadable or maybe too new for this version of Final Cut!!" It gets to 47 % load then flashes this message I only got FCE last week and haven't overloaded it. Any solutions?

  • Whenever I open a new window of firefox a new tab opens with the new window. How do I get it to stop doing that?

    Whenever I open a new window of Firefox, the new window opens at the hope page (like I want it to), but then a tab opens up with the new window. I want Firefox to stop doing this. When I open a new window I just want the window to open. I don't know

  • Arabic Problem in Indesign CS5 ME ???

    Hi, I am planning to publish a book about coins written in arabic. That's why I have to use latin and arabic letters in the same page. I just installed trial version of Indesign (7) CS5-ME. I creat a text box with "ME Type Tool"  and choose either Ad

  • Xbox call of duty 6 problems

    hello forums of linksys. i'm a noob at all of this, so please dont hate. I am using a wireless- g broadband router model WRT54G2. My ISP is Verizon. I dont have trouble at all playing live matches or chatting with my friends, all of that works great,

  • How to save a BufferredImage with RGB color encoded JPEG file.

    Hi, I am new to Java Imaging API programming. When I try to write a BufferredImage using following code - BufferedImage output = new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB); // Save File outputFile = new File("image.jpeg"); ImageIO