Weak, soft and Phantom references

I have got some confusions about reference types in java. These are:-
1. How we create weak and soft references! Are they predefined classes and we extend them, or they are interfaces and we implement them, or they are the concepts we implement by following any particular pattern.
or the references which are gone out of scope are considered as weak references.
2. can we change the type of reference dynamically! as JVM does it.
for eg:-
While the data objects are actually in use (or they are in scope) they are immune because they are strongly referenced by the display windows, once you move to look at something else (or they go out of scope) they become eligable for removal.
3. There is one set of problem I am facing, could that be resolved by weak or soft references.
"I set variables at session level(if they are needed at more than one page."
"At the last page I remove it from session."
"If for example there are 4 page sequence, and user quits after three steps, then the garbage variables are left in the session."
"if I make them as weak reference(or soft reference) then would it be a solution. or will it increase any potential risk of loosing the variables in between the process."
"or is there any way out for my problem using ref package, or it is used only for special purposes, not for a senario like this."
"or I am bound to use request level instead of session level, in these type of problems(which I am presently doing to solve this problem, but in this case I have to to set same attribute 3 times.)"
please comment.
4. What happens when we create an instance of reference queue.
-->does it give us a reference to the main queue that is used by garbage collector. when destroying objects out of scope.
that is garbage collector removes the reference of from the object and makes it point to queue element.
--> or when object goes out of scope, at that time only the reference is removed from the object and is made to point to queue element.
--> and when we do obj=null; at that time also obj reference is made to point to queue element.
please comment. I am confused about how java handles references.
5. does reference queue takes all the junked references.
if yes!
then for eg. we are using a background thread and doing some work when instance of class A is cleared, and doing some other work when instance of class B is cleared up by garbage collector.
queue.remove will return the junked reference, now how will we be determining which object is junked, instance of class A or class B. As reference of object is junked we can't use instanceOf operator.
please comment.
Thanks.

I find the documentation a bit opaque myself.
You create, say, a Weak reference by creating an object of class WeakReference, or a subclass of same. The object referenced (the referent) is a separate object which you pass in the WeakReference's constructor.
As long as the referent exist then calling get on the WeakReference object will return it. Once you call get and put the resulting reference in an object you have a hard reference and you know that the referent won't disappear until you let go of it.
If the referent has been disposed of then get returns null.
Sometimes you want to do extra cleaning up when a reference is cleared. Then you need to subclass the Reference class you are using to add indentifying information about the object that just disappeared. Obviously this can't be a reference to the object itself, that would be a "Hard" reference and would prevent the object being cleared.
You then create a ReferenceQueue, which is an ordinary object you own. The Reference objects have a reference to this queue. What happens is the the GC, when it clears one of these Reference objects, adds the Reference to the queue it references. You run a background thread which takes these cleared references from the queue and, using the extra information you added when you subclassed them, does whatever cleaning up you need to do. A typical example is that you have a Map, with the Reference objects as values. Each reference is extended to store the key, so that the cleanup will remove cleared references from the map.
No, it's not suitable for session objects, because you can't depend on the object still being arround. You can only rely on session timeout. It's a basic problem with web services that you never really know if the user has gone away and abandoned you.

Similar Messages

  • How strong ,soft ,weak ,phantom references are used in garbage collection

    Hi
    to all here.I have doubt that how garbage collection is deciding to cleaning up heap , and what are the roles of strong , soft , weak and phantom reference in garbage collection, i went throgh sun's java docs but i couldn't get any clear idea about those , please can anyone explain me with nice examples for which i will be really thankful.

    See:
    http://java.sun.com/developer/technicalArticles/ALT/RefObj/

  • IterateOverReachableObjects and soft/weak/phantom references

    Hi,
    what are soft/weak/phantom references for the JVM TI call IterateOverReachableObjects? Are those reference-objects
    1. ... "normal" objects having references and beeing traversed by this function
    (OR)
    2. ... interpreted as references with hiding the object nature
    (OR)
    3. ... ignored as they are ignored by the GC?
    Thanks,
    Robert

    The reference_kind with JVMTI_REFERENCE_FIELD, the referrer_tag will be the value of the tagged Reference instance, and the referrer_index will be the index of the "referent" field. It is this field that is treated in a special way by the GC.

  • Weak/Soft/Phantom/Circular references

    Hi,
    Could you please explain the below with example?
    Weak/Soft/Phantom/Circular references in java and when It should be used in what situations? What are the pros and cons?
    Please clarify.
    Thanks.

    What part of the Javadoc didn't you understand?
    BTW circular references do not belong with the others. A doubly linked list is an example of a set of circular references: there is a chain of references that leads back to the starting point. This is also known as a cyclic graph.

  • Soft, weak & phantom reference

    Hi all :)
    A short while ago, I began to study "java.lang.ref" package. It's not so easy to me. Also I've gotten many relative questions & answers from this forum, here is a problem confused me for so long time :( Please help me out, thanks!
    import java.lang.ref.*;
    import java.util.Hashtable;
    public class TestMe {
        private Hashtable ht = new Hashtable();
        private ReferenceQueue rq = new ReferenceQueue();
        public TestMe() { }
        private void clearQueue() {
            SoftReference r;
            while ((r = (SoftReference)rq.poll()) != null) {
                Object obj = r.get();
                if (obj != null) {
                    //* Here is the problem: why this always doesn't work?
                    // using weak or phantom reference is same like soft reference here :(
                    System.out.println("=> " + obj);
                ht.remove(r);
        public void put(Object key, Object obj) {
            clearQueue();
            ht.put(new SoftReference(key, rq), obj);
        public static void main(String[] args) {
            TestMe tm = new TestMe();
            int i=0;
            while(true) {
                tm.put(Integer.toString(i++), new String("wow"));

    The reference object is put into the reference queue after all SoftReferences to the reference object have been cleared. Since you're trying to get the reference pointed to by the SoftReference after it's been queued, it is null.
    If you want to store some information to use after the reference object has been queued, you need to subclass the SoftReference class and store the information in a field.

  • Video becomes soft and watermark blurry when exporting to iphone

    Hi guys, having a bit of an issue. I have some video i saved as apple either pro res 422 HQ or apple pro res 4x4. When i try going from that, to a .mp4 h.264 format (either in quicktime or compressor using the apple devices preset, at 700 kb) the video gets soft, and the watermark gets choppy when ever something is moving behind it. I've noticed going straight from source material things look fine. I know going from source is always going to be better, but i didn't think the difference was going to be so catastrophic. Anyone have some tips?

    New feature in QuickTime is Export to Web and it is still in "baby" step mode.
    Nice to have QuickTime writing the page code and creating the reference movie but a little more understanding by the page authors is required.
    Take a look at the data rate differences between the "cellular" and "iPhone" settings. 900kbit/sec versus 56kbit/second rate.
    Same dimensions (I suppose because I've done no tests) so the 56 kbps so that version would download very fast and look very ugly.
    If you don't need it (and we all probably don't) just remove the check mark before choosing Export. But, since it will be such a tiny file, no real harm using it (unless you get comments that your video looked very ugly when viewed "cellularly".

  • When do VI and queue references become invalid?

    Hi all,
    I have a fairly complicated problem, so please bear with me.
    1)  I have a reentrant SubVI (let's call it VI "Assign") that has an input cluster of (VI ref, queue ref) (let's call the cluster type "Refs").  If the VI ref is invalid (first execution), the VI ref and queue ref are assigned values and are passed on as an output cluster of same type.  (The VI does other things too, but for simplicity only this is important.)
    2)  The VI that calls VI "Assign" (let's call it VI "Store") is not reentrant and has a local variable of type "Refs" that is wired to the input and output of VI "Assign".  This VI effectively stores the references.  The references returned by VI "Assign" are always valid right after the call, but after the problem condition described below, they are invalid at the start of all calls before they are passed to VI "Assign".
    3)  VI "Store" is called by multiple non-reentrant VIs, so the local variables of VI "Assign" retain their values (Has been tested and verified to retain their values).  The VI causing the problem in this case is a template VI of which multiple copies are launched (let's call it VI "Template").
    The problem is that the moment an instance of VI "Template" is closed, the queue reference becomes invalid, although the actual variant value of the reference remains the same.  The VI ref can become invalid or not, depending on small changes, but is always reproducible.  I assume there must be some similarity between VI and queue refs.  After spending some time researching, the Labview help states for the Open VI Ref component "If you do not close this reference, it closes automatically after the top-level VI associated with this function executes."  In this case I assumed it means that the moment the reentrant VI "Assign" finishes, the references will get cleared ??  So I made a non-reentrant VI (let's call it VI "NR Assign") that only assigns values to the references and VI "Assign" now calls this VI (It effectively does what I described VI "Assign" does).  I keep this VI alive by using it in a VI which never terminates and since it never terminates, the references should never become invalid.  Anyone still following?  This didn't solve the problem though.  If I reproduce the same scenario using only one instance of the template VI, it works just fine.  Furthermore, the VI and queue references are never closed, to aid analysis of the problem.  Can anyone shine some light on what happens when a template VI terminates?  Could this be the problem?
    Unfortunately I cannot include the code.
    Thank you whoever is able to make sense of this.
    Christie

    Christie wrote:
    Hi all,
    I have a fairly complicated problem, so please bear with me.
    1)  I have a reentrant SubVI (let's call it VI "Assign") that has an input cluster of (VI ref, queue ref) (let's call the cluster type "Refs").  If the VI ref is invalid (first execution), the VI ref and queue ref are assigned values and are passed on as an output cluster of same type.  (The VI does other things too, but for simplicity only this is important.)
    2)  The VI that calls VI "Assign" (let's call it VI "Store") is not reentrant and has a local variable of type "Refs" that is wired to the input and output of VI "Assign".  This VI effectively stores the references.  The references returned by VI "Assign" are always valid right after the call, but after the problem condition described below, they are invalid at the start of all calls before they are passed to VI "Assign".
    3)  VI "Store" is called by multiple non-reentrant VIs, so the local variables of VI "Assign" retain their values (Has been tested and verified to retain their values).  The VI causing the problem in this case is a template VI of which multiple copies are launched (let's call it VI "Template").
    The problem is that the moment an instance of VI "Template" is closed, the queue reference becomes invalid, although the actual variant value of the reference remains the same.  The VI ref can become invalid or not, depending on small changes, but is always reproducible.  I assume there must be some similarity between VI and queue refs.  After spending some time researching, the Labview help states for the Open VI Ref component "If you do not close this reference, it closes automatically after the top-level VI associated with this function executes."  In this case I assumed it means that the moment the reentrant VI "Assign" finishes, the references will get cleared ??  So I made a non-reentrant VI (let's call it VI "NR Assign") that only assigns values to the references and VI "Assign" now calls this VI (It effectively does what I described VI "Assign" does).  I keep this VI alive by using it in a VI which never terminates and since it never terminates, the references should never become invalid.  Anyone still following?  This didn't solve the problem though.  If I reproduce the same scenario using only one instance of the template VI, it works just fine.  Furthermore, the VI and queue references are never closed, to aid analysis of the problem.  Can anyone shine some light on what happens when a template VI terminates?  Could this be the problem?
    Unfortunately I cannot include the code.
    Thank you whoever is able to make sense of this.
    Christie
    All LabVIEW refnums do get deallocated automatically when the top-level VI in whose hierarchy the refnum was created goes idle (stops executing). You will have to make sure that the creation of a refnum is done inside a VI hierarchy that stays running for the entire time you want to use that refnum.
    Rolf Kalbermatter
    Message Edited by rolfk on 06-27-2007 11:52 AM
    Rolf Kalbermatter
    CIT Engineering Netherlands
    a division of Test & Measurement Solutions

  • I am trying to update to i-tunes 11.3 but when I download I get a message that itunes didn't install properly, and a reference to error 127. Any ideas?

    I am trying to update to i-tunes 11.3 but when I download I get a message that itunes didn't install properly, and a reference to error 127. Any ideas?

    For general advice see Troubleshooting issues with iTunes for Windows updates.
    The steps in the second box are a guide to removing everything related to iTunes and then rebuilding it which is often a good starting point unless the symptoms indicate a more specific approach. Review the other boxes and the list of support documents further down page in case one of them applies.
    Your library should be unaffected by these steps but there is backup and recovery advice elsewhere in the user tip.
    tt2

  • IAccessible on Windows 8.1 and uiautomation reference leaks causing crashes in our applications.

    We have started crashing on Windows 8.1 and I have traced the problem to uiautomation.dll and combase.dll. The root cause of the problem is reference counting. It appears that on Windows 8.1 uiautomation is using keyboard hooks and other means to constantly
    query for interfaces leading to reference leaks on UI objects. We use MFC and have code that has relied on reference couting to to lead to calls to DestroyWindow. However on Windows 8.1, in cases where we expect, and historically have had, the window get destroyed,
    we have windows now hanging around due to elevated reference counts. I can set breaks in the MFC oleunk.cpp and monitor the reference count going up in CCmdTarget::ExternalAddRef for say, a command bar (menu) and after constantly clicking the menu, the reference
    count just keeps increasing and increasing. I can quite quickly cause the reference count to zoom into the hundreds. We started getting reports of crashes on Windows 8.1 from customers and at first I didn't duplicate the issue. Then my Windows 8.1 box rebooted
    on me (that's another issue as it did so on its own even though I said to defer the restart and I lost data thanks to that nasty Win 8 feature) and after updates installed, I could easily duplicate the crash.
     The version now installed for uiautomation is "7.2.9600.17415 (winblue_r4.141028-1500)" and "6.3.9600.17415 (winblue_r4.141028-1500)" as seen from the Visual Studio debugger's modules window. I do see some release calls coming from
    a function named "AccessibleObjectFromWindowTimeOut" and wonder what the "TimeOut" implies. That is because the easiest way for a user to crash our app on Windows 8.1 is to use GoToMeeting on Windows 8.1 and invite a participant to take
    control of the application. I can also crash on Windows 8.1 simply by setting various break points and trace points to print messages as I track what is going on. That is, the crashes appear to be dependent on some sort of race condition and the debugger interferes
    with the proper functioning of whatever is occurring inside uiautomation and combase. I have never seen the debugger "interfere" with a process in this way. I can run the same app and perform the same tasks on Windows 7 and these calls that cause
    the ref count to constantly increase simply are not made (and combase.dll doesn't get injected into our process on Windows 7). I think Microsoft needs to look into what they are doing on Windows 8 and start releasing interfaces they are leaking when obtaining
    the IAccessible interface from windows that implement it.
    R.D. Holland

    A dump file doesn't show the cause of the crash - an ever increasing reference count. I changed an implementation of OnGetObject (MFC API) and and didn't return the IAccessible interface and the reference count remained the same on Windows 8.1 as it does
    on Windows 7. I did that after tracing the ever increasing reference count to calls to get IAccessible coming from those MS DLLs. Eventually I made sure we destroy the window when we think it should be destroyed instead of relying on the MFC reference counting
    mechanism. We have testing code that relies on UI automation so just refusing to give out IAccessible isn't an option as I don't know the context of the request.
    R.D. Holland

  • TOC and Cross reference bookmarks

    Hi. I recently finished work on a book that had front matter containing a brief TOC and a detailed TOC. The book also contained individual TOCs at the beginning of each chapter.
    I updated all numbering across the book files before generating the TOCs in the front matter, then generated the brief and detailed TOCs using my TOC Styles. However, I assigned cross references for the individual chapter TOCs. May not have been the best choice.
    Because there was special styling for some of the front matter TOC entries that wasn't easily specified using just the TOC Styles, I cut and pasted groups of entries from the generated TOC on the pasteboard into a new frame on the page. I checked in Story Editor to ensure that the invisible markers for the TOC entries had been copied into the new frame.
    When I exported the files to PDF, the TOC entries no longer pointed to the correct pages. They simply pointed to the first page of the PDF when clicked. Furthermore, no bookmarks showed up the TOC entries in the Bookmarks panel, even though I had "Create PDF Bookmarks" checked in my TOC Styles.
    I have three questions, then:
    --Is there any way to reclaim the behavior of the TOC bookmarks, and their display in the Bookmarks list of the PDF, if the entries are copied and pasted into a new frame in InDesign, without doing this manually in Acrobat Pro?
    --Is there any way to get cross references to display as bookmarks in a PDF, assuming that I needed to make individual PDFs of each chapter, without doing this manually in Acrobat Pro?
    --I'm betting that all files should be exported from InDesign as a single PDF in order for all TOC and cross reference links to work properly, without manual intervention in Acrobat Pro? If so, and manual intervention is required, is this accomplished by cataloging/indexing in Acrobat Pro?
    I need to be able to pass this information along to a publisher who is now requiring all of their compositors to submit bookmarked PDFs with their printer files. I'm pretty sure I know the answer to this one, but I would like verification from the pros here. Thanks!

    Hi. I recently finished work on a book that had front matter containing a brief TOC and a detailed TOC. The book also contained individual TOCs at the beginning of each chapter.
    I updated all numbering across the book files before generating the TOCs in the front matter, then generated the brief and detailed TOCs using my TOC Styles. However, I assigned cross references for the individual chapter TOCs. May not have been the best choice.
    Because there was special styling for some of the front matter TOC entries that wasn't easily specified using just the TOC Styles, I cut and pasted groups of entries from the generated TOC on the pasteboard into a new frame on the page. I checked in Story Editor to ensure that the invisible markers for the TOC entries had been copied into the new frame.
    When I exported the files to PDF, the TOC entries no longer pointed to the correct pages. They simply pointed to the first page of the PDF when clicked. Furthermore, no bookmarks showed up the TOC entries in the Bookmarks panel, even though I had "Create PDF Bookmarks" checked in my TOC Styles.
    I have three questions, then:
    --Is there any way to reclaim the behavior of the TOC bookmarks, and their display in the Bookmarks list of the PDF, if the entries are copied and pasted into a new frame in InDesign, without doing this manually in Acrobat Pro?
    --Is there any way to get cross references to display as bookmarks in a PDF, assuming that I needed to make individual PDFs of each chapter, without doing this manually in Acrobat Pro?
    --I'm betting that all files should be exported from InDesign as a single PDF in order for all TOC and cross reference links to work properly, without manual intervention in Acrobat Pro? If so, and manual intervention is required, is this accomplished by cataloging/indexing in Acrobat Pro?
    I need to be able to pass this information along to a publisher who is now requiring all of their compositors to submit bookmarked PDFs with their printer files. I'm pretty sure I know the answer to this one, but I would like verification from the pros here. Thanks!

  • How to get vendor name and invoice reference details in asset register

    Hi Experts,
    My client requirement is he want to see Vendor name and invoice reference filed in asset register.
    Is there any standard reports relating this requirement.
    Kindly Provide valuable information relating to this issue.
    Regards
    Madhan

    Hi Madhan,
    Let me know the t.code which you are getting asset data.
    Thanks

  • Looking for a script to connect bodytext to footnote and cross-references

    Hi,
    Need to do the following:
    I was provided with separate (tagged-text) files: bodytext, (foot)notes and cross-references. Now I am looking for a solution (script?) that can do the following:
    to restore the connection between the body-text, the footnotes and te cross-references after placing these in a document in Indesign.
    The cross-references should appear (in anchored text-frames) in the margin and the notes in a separate frame at the bottom of the page. If possible in the same way as footnotes in Indesign appear, but in a separate frame to be able to do some more formatting than native footnotes would accept.
    I have tested the plugin: Side Heads from InTools, and tis works well with Indesign files tthat contain footnotes. But this doesn't work with separately placed footnotes from a tagged-text-file, as they re not knotted together.
    I can't do any scripting myself.
    I would appreciate any help, and I am looking forward to your response...
    Regards,
    kkingg

    The first thing to do is:
    regexp "match regexp = ([0-9]+)" $_cli_result match count
    if $count eq 0
    exit 0
    end
    The second is a bit more challenging.  I think this will work:
    cli command "show call active voice br"
    foreach line $_cli_result "\n"
    regexp "^([0-9a-zA-Z]+) : " $line match callid
    if $_regexp_result eq 1
      continue
    end
    regexp "^dur 1d" $line
    if $_regexp_result eq 1
      cli command "show call active voice br | section $callid"
      syslog msg "$_cli_result"
    end
    end

  • Is it possible to download a PDF of "ActionScript 3.0 Language and Components Reference"?

    I have found the "ActionScript 3.0 Language and Components Reference" online fine but my Internet is sometimes slow and I would like to take the reference with me even when I am not connected to the Internet. Is there a way to download a PDF version of this reference and if so, how? Help would be very much appreciated. Thanks

    The second item in the listing, ActionScript 3.0 Reference for the Adobe Flash Platform (ZIP, 66 MB) is a standalone copy of the files that you are looking for.
    Here's an Adobe Community file on how to implement the local help: http://forums.adobe.com/thread/1000134

  • Internal and external reference voltage for DAC

    Hi
    For example: If we are using +10V as the internal reference voltage, the range of the DAC would be -10 to +10V. But if the signal goes only from -5 to +5 V, the resolution of DAC is not getting maximized. To maximize the Resolution, an external refernce voltage of +5V can be given. Now the range of the DAQ will be -5V to +5V, same as that of the signal..
    What is internal and external reference voltage for DAC ?????
    Somil Gautam
    Think Weird

    Hi,
    you're better post this question on the "multifunctional DAQ" section, quoting the P/N of your device, as well as your programming language

  • How to identify the component is an assebly and Phantom component

    Hi ,
    I am working on BOM. If You see the transaction cs03 (go with any bom  ) inside we can see the componenets......
    These componenet having the check boxes for Assembly and Phantom componenet. If the check box is cheked we will consider as that component is Assembly Or Phantom....
    My question is how to identy this at table level..i don't see any field related to these check boxess in SRPO , STKO and MAST.
    Please do need full.
    Regards,
    Raja.K

    Hi Raja,
    If you can handle ABAP, please use FM 'CS_BOM_EXPL_MAT_V2' to explode your Bom
    Write something like this :
    *     Tables  "call CS_BOM_EXPL_MAT_V2 "
    DATA: BEGIN OF stb OCCURS 0.
            INCLUDE STRUCTURE stpox.
    DATA: END OF stb.
      CALL FUNCTION 'CS_BOM_EXPL_MAT_V2'
        EXPORTING
          auskz                 = ' '
          capid                 = 'PP01'
          datuv                 = sy-datum
          emeng                 = '1.000'
          stlan                 = '1'
          stlal                 = mast-stlal
          mtnrv                 = matnr
          mehrs                 = 'X'
          mmory                 = '1'
          stpst                 = '0'
          werks                 = werks
        IMPORTING
          topmat                = selpool
          dstst                 = dstst_flg
        TABLES
          stb                   = stb
          matcat                = matcat
        EXCEPTIONS
          material_not_found    = 4
          no_plant_data         = 8
          no_bom_found          = 12
          no_suitable_bom_found = 16.
      SORT stb BY idnrk.
    The field which will indicate you wether your component is either an Assembly or a Phantom is stb-dumps ( EQ space , for assembly ).
    Hope this helps,
    Erwan

Maybe you are looking for