A Safe use of WeakHashMap ?

Hi all,
I'd like to have a clarification about WeakHashMap. It is stated that a WeakHashMap is a special implementation of the Map interface that references its keys via weak references. This allows the garbage collector to reclaim the key even though its still referenced by the hashmap.
BUT since we don't have much control over the garbage collector...what could be a safe use of the WeakHashMap??? why would we need a component that could be "cleaned up" depending on arbitrary memory needs?? is there a way to prevent to GC to clean the WeakHashMap, let's say when I need to browse the collection and then release it?
hope I was clear
Thanks
Francesco

You can use the WeakHashMap to implement a cache. The important thing is that the keys must be re-creatable. An example:
Say you have a component that operates on instances of the Foo class, and everything about the Foo instances is stored in a database, indexed on an ID number. There are millions of different Foo instances in the database. Applications that use your component refer to Foo objects by ID in all of your APIs.
You don't want to cache every Foo instance in the whole database, because that would require too much memory. It's better to cache only the Foo instances that you're actually using, on the theory that if the calling application asks for the 12345 Foo once, it will probably ask for it again.
So, you can use a WeakHashMap, which will allow the GC to reclaim keys only when the key is otherwise not in normal use, that is, when there are no strong references to the key. When the caller asks for Foo 12345, first you look in the WeakHashMap, and if there's no key 12345 in the WHM, then and only then do you take the performance hit of going to the database.
A good article on the WeakHashMap by Jack Shirazi is here:
http://www.onjava.com/pub/a/onjava/2001/07/09/optimization.html

Similar Messages

  • I do not see the security lock in the lower right corner, so I do not feel safe using my charge card. what happened to it.

    There used to be a small padlock feature in the lower right hand corner. When I upgraded to the latest Firefox, it disappeared. Now I do not feel safe using my credit card. What happened to it? How can I get the feature back so I can know it is a secure site?

    In Firefox 4 you no longer have the Status bar that showed the padlock in previous Firefox versions.<br />
    The padlock only shows that there is a secure connection and doesn't guarantee that you are connected to the right server.<br />
    So you might still be connected to the wrong server if you make a typo in the URL and someone has claimed that mistyped URL.<br />
    The functionality of the padlock has been replaced by the [[Site Identity Button]] on the left end of the location bar.<br />
    See also:
    * http://www.dria.org/wordpress/archives/2008/05/06/635/
    You can click the [[Site Identity Button]] on the left end of the location bar to see the padlock if there is a secure connection.
    A click on the "More Information" button will show more details about the connection.
    The background color of the "Site Identity Button" on the left end of the location bar will change color (blue or green) and show the domain in case of a secure HTTPS connection.
    * Hover the [[Site Identity Button]] then to see "Verified by xxxx"
    * Click the "Site Identity Button" on the left end of the location bar to see the padlock
    * Click the "More Information" button in that pop-up to see additional information about the connection.
    You can use this extension to get a padlock on the location bar.
    *Padlock: https://addons.mozilla.org/firefox/addon/padlock-icon/

  • Is it safe using itools software for iphone in terms of an warranteed iphone

    is it safe to use itools software for syncing iphone with my computer.does ill pass all warranty conditions after using itools to sync iphone {which is in warranty} with my pc.does iwill be able to get warranty on my iphone after using itools software.

    how to confirm that my iphone is jailbroken or not

  • Am I safe using a wireless printer on my secure network?

    Just installed my first wireless printer on my laptop through my network.   Seemed to go ok.  Is there a security risk using the WPA button where other users could also connect to my network. I ended up using the connect to my computer option when it failed.  Is there any risk to using my printer to print online bank statements? Basically, How secure am I.
    I do figure its risky connecting to an un-secured network such as a hot spot or hotel network when not at home.
    If this is a repeat question I don't want to be a nuisance.  Please let me know the link and I'll read through.  I'm new to these wireless hardware hookups other than bluetooth speakers and such.
    I spoke to one knowledgable person and he said unless someone knows the key they can't access it. At the same time he said to make sure the software is always up to date for the most protection.  Why do I need protection if it can't be accessed???
    Thanks
    ps what's this about message tags??? Can you tell that I'm new

    Wireless Direct Setup
    On Printer
    1. Turn on HP wireless direct from the printer’s control panel.
    2. Select Wireless Direct, and then select “On”.
    On iPad
    3. Settings>Wi-Fi>Look for printer>Connect to the HP wireless direct network.

  • Is it safe to use iPod while it's charging?

    Just wondering if it's safe to use the iPod while it's charging with an adapter?

    Not only is it safe, using it when charging prevents using up the battery. The battery has a finite number of charge/discharge cycles...

  • Keyboard Control FAIL: Using TAB key on websites directs cursor to nowhere!!! Why?

    (using 2.3 GHz Intel Core i5 MAC MINI, OS X Lion 10.7.5)
    I have always felt safer using the keyboard rather than mouse-clicking on search fields etc. (Open Firefox, hit command+L to the URL I want to go to, then hop to next field with TAB key).
    Most notably frustrating example is with using Google, this has not worked right for some time. Say that you to start to search for something on google "main page" and then it goes to the search results page... You go to edit your search or type your next search and the cursor is flashing inside (but off center) of the search field and inputting text is not an option. Also you may instictively hit ESC (while in search field) when you are being given suggestions you don't want, and then the cursor moves again to this no-man's land in between fields.
    The other KEYBOARD control issue that I have found is this:
    Sometimes you cannot Close Window or even Quit Firefox with the usual keyboard shortcut. Usually when a few windows or tabs are open, or when there is some pop-up survey etc. on page. You can usually ALT+TAB to another open program and then back, and then you can close window or application. But that seems really sketchy to me! This happens all the time on major websites. You can go File-> Close Window or File->Quit, but when you use the same keyboard command the file menu just flashes with an "error tone."

    The TAB key thing was the same in safe mode. Actually I should add/correct, regarding that google search example: when the cursor is in "inbetween fields" positions, actually it lets you type at the END of the text that you had typed in the field (even though the cursor is blinking before the text). and if you "Command A" to replace the text in the search field while cursor in this position, it instead selects all the rest of the text OUTSIDE the field. Just doesn't make any sense. I think this happens with fields on Facebook as well.
    That first issue is just very annoying because it comes up every day and I never get used to it. I would be actually be curious to hear from someone whom this DOES NOT happen to.
    The 2nd issue I mentioned occurs more randomly, and seems more definately a Firefox issue. (When you can only close a window or quit from the drop down menu with the mouse, Command W and Command Q just give an error beep)

  • How to use Source Code Control for Large Application?

    Hi, All!
    I would like to collect knowledge about "best practice" examples for using Source Code Control and project organization for relative large application (let's say approx 1000 SubVIs).
    Tools used:
    LabVIEW 8.0
    CVS Server
    PushOK CVS Proxy Client
    WinCVS
    With LabVIEW 8 we can organize large project pretty well. This described in article Managing Large Applications with the LabVIEW Project.
    I have read this article too: Using Source Control Software with LabVIEW In this Article Source Safe used, but with PushOK all looks nearby the same and works (some tricks for compare function are required).
    Example. Two developers working together on same project. Internally project is modular, so one developer will work with module "Analysis", and another one with "Configuration" without interferences. These modules placed into Subfolders as shown in example above.
    Scenario 1:
    Developer A started with modification of module "Analysis". Some files checked out. He would like to add some SubVIs here. So, he must also perform check out for the project file (*.lvproj), otherwise he cannot add anything into project structure.
    Developer B at the same time would like to add some new functions into module "Configuration". He also needed to check out project file, but this file already checked out by Developer A (and locked). So, he must wait until lvproj file will be checked in. Another way is mark *.lvproj files as text files in PushOK, but then one of developers will get conflict message by checking in and then merging will be necessary. This situation will coming very often, because in most cases *.lvproj file will be checked out all the time.
    Question: Which practice is better for such situation? Is Libraries better than folder for large project?
    Scenario 2:
    Developer C joined to the team. First, he must get complete project code for starting (or may be at least code of one Library, which assigned to him).
    Question: How it can be done within LabVIEW IDE? Or WinCVS (or other SCC UI) should be used for initial checkout?
    Scenario 3:
    Developer D is responcible for Build. Developers A,B,C have added lot of files into modules "Analysis", Configuration" and "FileIO". For building he need to get complete code. If our project splitted into folders, he should get latest *.lvproj first, then newly added SubVIs will appear in Project Explorer, then he should expand tree, select all SubVIs and get latest versions for all. If Project organized in Libraries, he must do the same for each library, isn't?.
    Question: Is this "normal way", or WinCVS should be used for this way? In WinCVS its possible with two mouseclicks, but I prefer to get all code from CVS within LabVIEW IDE recursively...
    That was a long post... So, if you already working with LabVIEW 8 with SCC used for large project, please post your knowledge here about project structure (Folders or Libraries) and best practices, its may be helpful and useful for all of us. Any examples/use cases/links etc are appreciated.
    Thank you,
    Andrey

    Regarding your scenarios:
    1. Using your example, let's say both developers checked out version 3
    of the project file. Assuming that there are only files under the
    directories in the example project, when Developer A checks in his
    version of the project, there will be new files in one section of the
    project separate from where Developer B is working. Developer B,
    notices that there is now a version 4 of the project. He needs to
    resolve the changes so will need to merge his changes to the latest
    version of project file. Since the project file is a text file, that is
    easy to do. Where an issue arrises is that after Developer B checks in
    his merged changes, there is a revision 5. When Developer A and B go to
    make another change, they get the latest version which will have the
    merged changes to the project file but not the referenced files from
    both Developer A and B. So when A opens version 5, he sees that he is
    missing the files that B checked in and visa versa. Here is where the
    developers will needs to manually use the source control client and,
    external to LabVIEW, get those new files.
    Where libraries help with the above scenario is that the library is a
    separate file from the project so changes made to it outside of the
    project do not require the project to be modified. So this time, the
    developers are using a single project again which time time references
    two libraries. The developers check out the libraries, make changes to
    the libraries, and then check those changes in. So when each developer
    opens the project file, since it references the project file, the
    changes to the library will be reflected. There is still the issue of
    the new files not automatically coming down when the latest version of
    the library is obtained. Again, the developers will needs to manually
    use the source control client and, external to LabVIEW, get those new
    files. In general, you should take advantage of the the modularity that
    libraries provide.
    2. As noted in the above scenario, there is no intrinsic mechanism to
    get all files referenced by a LabVIEW project. Files that are missing
    will be noted. The developer will then have to use the source control
    provider's IDE to get the initial contents of the project  (or library).
    3. See above scenarios.
    George M
    National Instruments

  • Which Account to Use for Family Sharing?

    After reading the issues of being locked out, I am a bit apprehensive to set up the account.
    Here's my scenario:
    Husband & Wife accounts want family sharing
    Husband as owner - using @me.com Apple ID AND @gmail.com iTunes account (or in settings 'Family Purchases')
    Wife also has identical setup where iCloud apps are run by an @me.com Apple ID and all iTunes purchases have been made through a @gmail.com account.
    Question: Which account does the Husband invite the wife to family sharing? The @me.com Apple ID or the @gmail.com?
    thanks!

    The Apple ID is his iTunes account.  The @me.com is his iCloud account which is connected to his Apple ID/iTune account.  Using anyone should suffice.
    In any case, if you want to be safe, Use his Apple ID account.
    Hope this helps.

  • Can't Print after using Migration Assistant, more

    Hi,
    I recently used Migration Assistant to transfer all of my apps, files, data, ... from my Power PC G5 1.8mhz running 10.4.11 to my new-ish iMac 2.16 Ghz Intel Core 2 Duo with 10.6.8.  It seems like everything worked but I have a few issues.
    1.  I can't print anything now where as before I could on the iMac.  I get a printer error message
    Error: /System/Library/Printers/Libraries/PrintJobMgr/Contents/MacOS/PrintJobMgr failed
    2.  I can access all of the old files of the G5 but I have to log out of the iMac user to one of my old user accounts.  I want to get all of those files into my iMac account and delete those old user accounts.  I especiallly want to get the old photos into my iMac user account's iPhoto.
    Thanks for any help.  I'm feeling like an imbecile right about now.  ;-)
    -Don

    You should not delete files on the new machine after running MA. The only files I would recommend deleting would be data files you no longer need, however to be safe use Time Machine to back up your machine. One never knows when you may need something again.
    Regards,
    Roger

  • Web service client proxy thread safe?

    Is the jax-ws web service client proxy and port objects thread safe in Weblogic 10.3.4?
    Been searching through the docs but can't find any info on this.

    I have searched and it seems that with cxf it's safe to do something like this.
    Nobody knows if this is safe using metro?

  • Should I use Boot Camp?

    Well, I'm new to the Mac family, so I'm being very cautious. I am currently waiting for the arrival of my MacBook Pro, and want to be prepared when it gets here.
    I currently use my PC mainly for gaming, as well as general school usage. I want to know how safe and reliable is Boot Camp? Would I be safer using something like a parallel?
    I'd basically be using the PC part of the MBP for gaming.
    Is there currently any way to unpartition the section of the HDD that was portioned during the installation process using Boot Camp?
    Thanks.

    I'll have full access to things like my Video Card, RAM, CPU power, iSight and stuff like that right?
    Yes, part of the process of running Boot Camp is that it will create a Windows formatted CD of all the hardware drivers Windows will need.
    How's the transition from one OS to the other? Is it cumbersome, or is it just a simple click of a few hotkeys, and the switch is made?
    It's a bit cumbersome after you're done setting up with Boot Camp. In order to use Windows, you have restart the Mac. Hold down the Option key immediately after the startup chime and double click your Windows partition to boot directly into Windows.
    If you choose the OS X partition as your startup disk in the System Preferences, the Mac will always boot into OS X if you do nothing else on a cold start or restart. You'll only need to hold down the Option key to boot into Windows.

  • Using different constructors

    Hello all,
    I have a class that can be instantiated with 2 different constructors, like this :
    public class LProject {
    private List projects;
    public LProject(String path, String name) {
    super(path, name);
    projects = new ArrayList();
    public LProject(String path, String name, String description) {
    super(path, name, description);
    }My question concerns the initialization of the ArrayList, if i first create an object with LProject(path, name) the ArrayList will be initialize, and the then if i create a second object with LProject(path, name, description), the ArrayList would still be usable or it would be null?
    Do i have to initialize the ArrayList in both constructor? In this case it would give me two differents ArrayList so if i add the LProject object created before it will be in two different ArrayList?
    It is not clear for me can somebody help me clear that out.
    thanks a lot

    I don't think that is thread safe.
    If I was you I would do
    private static List projects = new LinkedList();
    Actually, that is not thread safe. But your example is not thread safe, either.
    @OP: if you want to use this static list, do this:
    private static List projects = Collections.synchronizedList(new ArrayList());But synchronization is not really necessary, if you keep this field as private and if you will not provide any kind of getter method that can return this list field (you can't guarantee what the other programmers will do with your list). If you decide to not use synchronization, you must guarantee that the manipulation of this list will only occur inside this class, where you can yourself control the access of this list. And you can achieve this control only by making this list totally unavailable to the others.
    If you are expert in synchronization, then you can use a unsynchronized list, doing the control of the manipulation of this list by your own code inside your class. Otherwise, if you don't know very much about synchronization, although the cost of performance when synchronization is being used (not so much, but there is a cost), it is always safer using a synchronized list.

  • Web service request context thread safe ?

    Hello,
    I am setting some header values in the request context like this:
    BindingProvider bp = (BindingProvider)stub;
    Map<String,Object> map = bp.getRequestContext();
    map.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
    The map object is threadLocal ? can I use this code in a muthreaded environment ?
    Any ideas?

    I have searched and it seems that with cxf it's safe to do something like this.
    Nobody knows if this is safe using metro?

  • BP Searching using TREX

    Hi All.
    I trying to search BP in CRM ICWC 4.0 SP 7 using TREX, i have installed Software Agent Framework and applied the respective notes but i don't know which are next steps. When i try to execute the BP search and try with a debugging i have notice that some function modules are not returning data because something is missing.
    Can anybody help me.
    Thanks in advance
    Armando

    Hi.
    Can i find a guide or something like that in the web ?.
    I have installed the TREX, the SAF, used the transacztion CRM_SAF_TOOLS for checking and everything is Ok but when i'm using the function modelu TREX_BUPA_SEARCH in debbuging mode, i noticed the TREX search inside is not working... something is missing.
    Best Regards.
    Armando

  • Are static methods in Java thread safe?

    Are static methods in Java thread safe?
    thanks,
    suresh

    if static method use the instance variable
    You mean member variable, where member variables are either class variables (static) or instance variables (non-static).
    then you have to make it thread safe using
    synchronization Not necessarily. Depends on requirements and usage context.
    else in case of local var. it
    is thread safe. Not necessarily. That local variable could refer to an object that's visible to multiple threads.
    Statements like "Local variables are threadsafe but member variables aren't" are an oversimplification.
    Something is threadsafe if it cannot be made to behave incorrectly simply by running it in a multithreaded context. Determining that can be very difficult.

Maybe you are looking for