Finalize() method for newer JVMs

Hi,
Just a quick question on finalize() if that's ok. From memory the only time the developer should have to implement this method is when you want to do something with a system resource during garbage collection.
A finalize method should never have to worry about clearing object references etc? Correct?
I am 99.9999999999% sure on this.
I am looking at some old code here which contains some objects which implement the finalise method, I am trying to figure out why they did it.
Is there any obscure case (i.e. with weak references or something) where an object may have to clear object references in finalise?
Thanks in advance.

malcolmmc wrote:
beginner2 wrote:
Hi,
Just a quick question on finalize() if that's ok. From memory the only time the developer should have to implement this method is when you want to do something with a system resource during garbage collection.
Even where you do want to do that (and it's rarely a good strategy except as some kind of backstop, since there's no guarantee you system resource related object will ever be garbage collected), I'd recommend using a PhantomReference instead. It's more controllable. You control what thread it's cleaned up on, you can pass references in a way that's dangerous in a finalize and you can discard it if (as should happen) the program explicitly closes it's connection with the resource.++agree: PhantomReference<TollBooth>

Similar Messages

  • Java API - finalize method for XmlResults

    Hello!
    I'm wondering why in DB XML Java code we have a finalize() method which invokes delete() method whereas we are supposed to invoke delete() method manually (according to the documentation). Is it ok if delete() method is invoked twice (it happens in case GC cleans up the XmlResults object) ? Can't we get some problems because of that?
    Thanks,
    Vyacheslav

    Also you should note that Java actually makes no guarantees that the finalize() method is called, and it can frequently happen in practice that this is the case. For instance, the JVM usually doesn't bother calling finalize() on objects if it is shutting down, which can be disastrous for DB XML objects like Environment, XmlManager and XmlContainer, which need to clean up and write data to disk when they are closed. Read the [FAQ | http://www.oracle.com/technology/products/berkeley-db/faq/xml_faq.html#16] for more details.
    John

  • Does WebDynPro have finalizer method for some ojects?

    I need to create object pool with guaranteed  call to destroy participated objects (DB Sessions) after end of some method execution,
    or owning object release.
    Does WebDynPro has such a method?
    How to implement it?
    thanks

    OK, very  thanks , that answer was presented on most seen place,
    (not always seen).
    what is unteresting - does it really guarranteed to be called?
    Happen at all experiments - is not "guarranteed".
    Are there any else ways?

  • Object finalize method

    As far as my concept goes, the finalize() method for an object can be called only once. If it is called explicitly, the garbage collector will not call it again. But one of my programs is calling finalize() method apprantly more than once. Please see the code below.
    class ObjFinalizeTest{
         public void finalize() {
              System.out.println("Class Terminated");
         static void foo() {
              ObjFinalizeTest aa = new ObjFinalizeTest();
    public class Chap1 {
         static char a ;
         static int b ;
         static ObjFinalizeTest c;
         public static void main(String[] args) {
              c= new ObjFinalizeTest();
              c.finalize(); //explicit call to finalize
              System.out.println(c);
              c=null;
              System.gc();
    }this is the output I am getting
    Class Terminated
    ObjFinalizeTest@923e30
    Class TerminatedCan anybody please explain this phenomenon

    Finalize is just like any other method, it's nothing special, same with main. What you are doing is explicitly calling it and then the JVM is calling it as well. I think the docs only state that the JVM will only call the method once, you however can call it as many times as you like.

  • Finalize() method being called multiple times for same object?

    I got a dilly of a pickle here.
    Looks like according to the Tomcat output log file that the finalize method of class User is being called MANY more times than is being constructed.
    Here is the User class:
    package com.db.multi;
    import java.io.*;
    import com.db.ui.*;
    import java.util.*;
    * @author DBriscoe
    public class User implements Serializable {
        private String userName = null;
        private int score = 0;
        private SocketImage img = null;
        private boolean gflag = false;
        private Calendar timeStamp = Calendar.getInstance();
        private static int counter = 0;
        /** Creates a new instance of User */
        public User() { counter++;     
        public User(String userName) {
            this.userName = userName;
            counter++;
        public void setGflag(boolean gflag) {
            this.gflag = gflag;
        public boolean getGflag() {
            return gflag;
        public void setScore(int score) {
            this.score = score;
        public int getScore() {
            return score;
        public void setUserName(String userName) {
            this.userName = userName;
        public String getUserName() {
            return userName;
        public void setImage(SocketImage img) {
            this.img = img;
        public SocketImage getImage() {
            return img;
        public void setTimeStamp(Calendar c) {
            this.timeStamp = c;
        public Calendar getTimeStamp() {
            return this.timeStamp;
        public boolean equals(Object obj) {
            try {
                if (obj instanceof User) {
                    User comp = (User)obj;
                    return comp.getUserName().equals(userName);
                } else {
                    return false;
            } catch (NullPointerException npe) {
                return false;
        public void finalize() {
            if (userName != null && !userName.startsWith("OUTOFDATE"))
                System.out.println("User " + userName + " destroyed. " + counter);
        }As you can see...
    Every time a User object is created, a static counter variable is incremented and then when an object is destroyed it appends the current value of that static member to the Tomcat log file (via System.out.println being executed on server side).
    Below is the log file from an example run in my webapp.
    Dustin
    User Queue Empty, Adding User: com.db.multi.User@1a5af9f
    User Dustin destroyed. 0
    User Dustin destroyed. 0
    User Dustin destroyed. 0
    User Dustin destroyed. 0
    User Dustin destroyed. 0
    USER QUEUE: false
    INSIDE METHOD: false
    AFTER METHOD: false
    User Dustin destroyed. 1
    User Dustin destroyed. 1
    User Dustin destroyed. 1
    User Dustin destroyed. 1
    USER QUEUE: false
    INSIDE METHOD: false
    AFTER METHOD: false
    User Dustin destroyed. 2
    User Dustin destroyed. 2
    User Dustin destroyed. 2
    User Dustin destroyed. 2
    User Dustin destroyed. 2
    User Dustin destroyed. 2
    User Dustin destroyed. 2
    User Dustin destroyed. 2
    USER QUEUE: false
    INSIDE METHOD: false
    AFTER METHOD: false
    User Dustin destroyed. 3
    User Dustin destroyed. 3
    User Dustin destroyed. 3
    User Dustin destroyed. 3
    User Dustin destroyed. 3
    User Dustin destroyed. 3
    User Dustin destroyed. 3
    User Dustin destroyed. 3
    User Dustin destroyed. 3
    USER QUEUE: false
    INSIDE METHOD: false
    AFTER METHOD: false
    User Dustin destroyed. 4
    User Dustin destroyed. 4
    User Dustin destroyed. 4
    User Dustin destroyed. 4
    User Dustin destroyed. 4
    User Dustin destroyed. 4
    User Dustin destroyed. 4
    User Dustin destroyed. 4
    User Dustin destroyed. 4
    USER QUEUE: false
    INSIDE METHOD: false
    AFTER METHOD: false
    User Dustin destroyed. 5
    User Dustin destroyed. 5
    User Dustin destroyed. 5
    User Dustin destroyed. 5
    User Dustin destroyed. 5
    User Dustin destroyed. 5
    User Dustin destroyed. 5
    User Dustin destroyed. 5
    User Dustin destroyed. 5
    USER QUEUE: false
    INSIDE METHOD: false
    AFTER METHOD: false
    User Dustin destroyed. 6
    User Dustin destroyed. 6
    User Dustin destroyed. 6
    User Dustin destroyed. 6
    User Dustin destroyed. 6
    User Dustin destroyed. 6
    User Dustin destroyed. 6
    User Dustin destroyed. 6
    User Dustin destroyed. 6
    User Dustin destroyed. 6
    Joe
    USER QUEUE: false
    INSIDE METHOD: false
    AFTER METHOD: false
    User Dustin pulled from Queue, Game created: Joe
    User Already Placed: Dustin with Joe
    User Dustin destroyed. 7
    User Dustin destroyed. 7
    User Dustin destroyed. 7
    User Dustin destroyed. 7
    User Dustin destroyed. 7
    User Dustin destroyed. 7
    User Dustin destroyed. 7
    User Dustin destroyed. 7
    User Dustin destroyed. 7
    User Dustin destroyed. 7
    INSIDE METHOD: false
    INSIDE METHOD: false
    USER QUEUE: true
    INSIDE METHOD: false
    INSIDE METHOD: false
    User Dustin destroyed. 9
    User Joe destroyed. 9
    User Dustin destroyed. 9
    User Dustin destroyed. 9
    User Dustin destroyed. 9
    User Dustin destroyed. 9
    INSIDE METHOD: true
    INSIDE METHOD: false
    USER QUEUE: true
    INSIDE METHOD: false
    INSIDE METHOD: false
    INSIDE METHOD: true
    INSIDE METHOD: false
    USER QUEUE: true
    INSIDE METHOD: false
    INSIDE METHOD: false
    It really does seem to me like finalize is being called multiple times for the same object.
    That number should incremement for every instantiated User, and finalize can only be called once for each User object.
    I thought this was impossible?
    Any help is appreciated!

    Thanks...
    I am already thinking of ideas to limit the number of threads.
    Unfortunately there are two threads of execution in the servlet handler, one handles requests and the other parses the collection of User objects to check for out of date timestamps, and then eliminates them if they are out of date.
    The collection parsing thread is currently a javax.swing.Timer thread (Bad design I know...) so I believe that I can routinely check for timestamps in another way and fix that problem.
    Just found out too that Tomcat was throwing me a ConcurrentModificationException as well, which may help explain the slew of mysterious behavior from my servlet!
    The Timer thread has to go. I got to think of a better way to routinely weed out User objects from the collection.
    Or perhaps, maybe I can attempt to make it thread safe???
    Eg. make my User collection volatile?
    Any opinions on the best approach are well appreciated.

  • My previous method for payment on itunes was debit card but now I dont have one.  How do I change to no card without creating a new apple id

    My previous method for payment on itunes was debit card but now I dont have one.  How do I change to no card without creating a new apple id?  It tells me to update payment details when I try to update apps but it only has visa, mastercard, amex and maesto.  I dont have any of them.  I did previously use visa but I dont have the card anymore and cant get another one.  Please can someone help me set it up so I dont need a payment method.  Thanks

    Thanks for you reply.  Is that the only way as it would cost me £15?

  • Why can I no longer, after having downloaded Lion, write accents and other diacriticals when in the Google, Yahoo, FaceBook, or even here in this post? The new method for getting at the "option" symbols works fine in other places like spotlight, but now e

    Why can I no longer, after having downloaded Lion, write accents and other diacriticals when in the Google, Yahoo, FaceBook, or even here in this post? The new method for getting at the "option" symbols works fine in other places like spotlight, but now even the older "option+letter" doesn't work in most places.

    Chrome doesn't support the new accent/diacritics/macron chooser. I'm not sure about other browsers such as Firefox. You can use the old Option+letter combination that Doug suggested. Hopefully updates will solve these little incompatibilities shortly.
    Neill

  • CSS Flexbox - a new layout method for web pages

    Hi,
    As you may all know, css flexbox is a new layout method for laying out your web page, and with Firefox releasing version 22 within the next few days, (should have been yesterday) it is now no longer hidden behind browser settings. Add to this the fact that all desktop browser except IE10 and Safari 5 & 6, no longer require the use of a vendor prefix, and that http://html5please.com/ simply say ’USE’, flexbox is now finally a viable layout alternative, especially for responsive layouts.
    Flexbox has been available on all Android and iOS devices, (and any device using the webkit engine, e.g. new blackberry’s and Kindle Fire devices) since they were first release.
    To help find problems, and provide help in using flexbox, we would be grateful if you would experiment and provide feedback in the use of flexbox for layouts.
    To help you get started a video tutorial, (with files to download) and examples of layouts, (more to be added next week) and tips & tricks to help with some of the more common problems have been provided at http://flexboxlayouts.com/. This site will also be updated to provide you with a list of problems and hopefully the solutions to those problems, so that you will have a ’one stop’ reference site for using flexbox.
    If you have any tips in using flexbox, or a flexbox layout that you would like added to the site, then you can use the ’Submit’ email address on the http://flexboxlayouts.com/ site.
    For more info on the css flexbox specifications see -
    http://www.w3.org/TR/2012/CR-css3-flexbox-20120918/
    or if you just want to know what property is supported by which browser/device -
    http://flexboxlayouts.com/pdfs/flexbox%20browser%20Properties%20.pdf
    Note to regular contributors & moderators:
    As I only have time to visit and help in the forum for a few hours per week, should you find any flexbox problems unanswered then please let me know via email.
    PZ

    Hi Al
    Yes I know, and this is why I am asking people to experiment with flexbox.
    The fix for IE10 is to give the right hand sidebar, (or the content) a larger flex-grow property, the bug is caused by the flex shorthand property not recognizing % values, and using the flex-basis as a set width instead of a preferred size.
    I have logged the bug, (and many others) with the various browser bug bases.
    This and other bugs, do have 'fixes', so I hope users will experiment with flexbox and provide feedback.
    Flexbox is no different than any other css feature, "we can only find the problems in actual use".
    Strangely enough, I have found the most consistent to use is the old 2009 implementation on mobile devices, (no doubt iOS7 will change all that ).
    PZ

  • I have a ipod touch 2nd generation 8gb 4.2.1 im new to itunes i made a itunes account but it says i need a payment method is there anyway i can use my paypal if not is there a way to not have to use a payment method for itunes store please help ty

    i have a ipod touch 2nd generation 8gb 4.2.1 im new to itunes i made a itunes account but it says i need a payment method is there anyway i can use my paypal if not is there a way to not have to use a payment method for itunes store please help ty

    Create a NEW account using these instructions. Make sure you follow the instructions. Many do not and if you do not you will not get the None option. You must use an email address that you have not used with Apple before.
    Creating an iTunes Store, App Store, iBookstore, and Mac App Store account without a credit card
    Using those instructions you may also be able to select PayPal if that is allowed in your country.

  • Payment Methods for implementing new bank

    Hello Experts,
    I need your expert suggestions on the below:
    My Client is implementing Bank XX to replace Bank YY.  At present, the client is using Payment methods 'A' for ACH Payments and 'C' for Checks with House banks "00001" and "00002" respectively for the Bank YY. We will be setting up New House Banks for the new bank.
    When we implement New bank YY, can we:
    1. Utilize the same Payment Methods A and C for the New bank YY.
    2. What are the issues that will be encountered if we utilize the same Payment Methods.
    The Client is planning to use both the Banks for a short period of time and eventually stop using the old bank XX.
    Regards,
    Andy

    Hi Andy,
    If we go by the Payment program configuration then I don't think there is going to be much impact expect for the bank determination in this case as stated below.
    1. You already have the payment methods A and C within the relevant country and company code - so nothing on this front.
    2. You need to create new house bank 'XX' and accounts within it in transaction code FI12 (Configuration activity)
    3. But for bank determination purpose you would have to rank new house bank XX with rank 1 and not rank old house bank YY (if you don't want to use it any more) or rank it with rank 2 (if you still wish to use this house bank if the available amounts within the new house banks are low).
    4. Further you would also have to identify (within the bank determination tab) the bank account to be used from within the new house bank XX.
    5. Further if you are using this for ACH payments then assuming the transmission of file through IDOC you need to set up the EDI partner profile for this new house bank XX and also within the EDI compatible payment methods state payment method 'A'. Both these things can be done via transaction code FI12. But for setting up the partner profile may have to take help of ABAPER who can do it via transaction code WE20 replicating same settings as for old house bank 'YY' for new house bank 'XX'.
    6. For check payment method you need to set up the check lots for the new house bank 'XX' via transaction code FCHI.
    I dont think further you need to do anything other than test execute the payment.
    Hope this helps you!
    Regards,
    Prasad

  • I have downloaded whats app/brave temple run but using netsafe card. well now it shows 1.98$ is pending , though m trying with new netsafe card to pay it is showing error saying Pls select valid method for payment. i used same method as i did earlier

    i have downloaded whats app/brave temple run but using netsafe card. well now it shows 1.98$ is pending , though m trying with new netsafe card to pay it is showing error saying Pls select valid method for payment. i used same method as i did earlier

    I have recently started having this problem in PSE8. The Adobe workaround
    did work, but I don't fancy having to re register each time I use it.
    What I have discovered is that it's nothing to do with the image metadata as it occurs before any image is opened.
    It SEEMS to only occur if you use file/open with to open an image in the editor - IE start PSE with that command.
    If you close elements down, and start it using programs/PSE/Elements (or your desktop shortcut) - the panorama feature magically works.
    Each time I've opened the editor 'automatically' using image/open with, it seems to create the problem.
    Hope this helps

  • Strongly Suggest the Apple to develop the new input method for Chinese

    As we know, there is only one input method in iOS, but it is really hard to use, so I strongly suggest the Apple to develop the new input method for Chinese, any body agree with me? please support our suggestion!!!! thanks.

    ZhangWQ wrote:
    As we know, there is only one input method in iOS,
    I am puzzled by your statement.  Here is the list of 7 that I see:
    Simplified Chinese: Handwriting, Pinyin, Stroke,
    Traditional Chinese: Pinyin, Zhuyin, Cangjie, Stroke
    When you use the feedback channel to contact Apple, make sure you tell them exactly what it is you want.  Asking them to "develop a new input method" will certainly just be ignored because they will have no idea what you are talking about.

  • Is there quick method for putting cloud songs on a new computer?

    Is there a quick method for putting cloud songs on a new computer?

    Setu iCloud on the new computer >  Apple - iCloud - Learn how to set up iCloud on all your devices

  • My Paper Strip Method for Cleaning BlackBerry Tour 9630 Trackball, Make Good as New!

    I just completed a quick little tutorial demonstrating my paper strip method for cleaning the trackball of the BlackBerry Tour 9630. As many of you know, cleaning the trackball on the Tour can be difficult since you can't remove it from the device like on previous BlackBerrys. Well, I've come up with a pretty successful solution that can get your trackball working good as new again.
    You can check out some testimonies on the CrackBerry.com forums if you like.
    Please consider leaving a comment below or on YouTube if this method helped you.
    Thanks!
    YouTube - Paper Strip Method for Cleaning BlackBerry Tour 9630 Trackball, Make it Good as New!

    DO NOT DO THIS!
    Paper strip didn't fix anything on my trackball, and on the thrid attemt, ripped off under the trackball rendering my phone all but useless. This is a good way to really screw your phone up. Can't beleive the RIM site is linked to it.........

  • Help needed in overriding the finalize() method!

    Hi
    I need some help in overwriting the finalize() method.
    I have a program, but at certain points, i would like to "kill" a particular object.
    I figured that the only way to do that is to make that object's class override the finalize method, and call it when i need to kill the object.
    Once that is done, i would call the garbage collector gc() to hopefully dispose of it.
    Please assist me?
    Thanks
    Regards

    To, as you put it, kill an object, just null it. This
    will be an indication for the garbage collector to
    collect the object. In the finalizer, you marely null
    all fields in the class. Like this:
    public class DummyClass
    String string = "This is a string";
    Object object = new Boolean(true);
    public void finalize() throws Throwable
    super.finalize();
    string = null;
    object = null;
    public static void main(String[] args)
    //Create a new object, i.e allocate an
    te an instance.
    DummyClass cls = new DummyClass();
    //Null the reference
    cls = null;
    This is a pointless exercise. If an object is being finalized, then all the references it contains are about to cease being relevant anyway, so there's no purpose to be served in setting them to null.
    All that clearing a reference to an object does is to clear the reference. Whether the object is subsequently finalized depends on whether the object has become unreachable.
    Directly calling finalize is permitted, but doesn't usually serve any purpose. In particular, it does not cause the object to be released. Further, calling the finalize method does not consitute finalization of the object. Finalization occurs when the method is called as a consequence of a garbage collector action.
    If a variable contains a reference to an object (say a table), and you want a new table instead, then create a new table object, and assign its reference to the variable. Assuming that the variable was the only place that the old table's reference was stored, the old table will, sooner or later, get finalized and collected.
    Sylvia.

Maybe you are looking for

  • Values from WESelectCascade not in Target-URL

    Hi, I have create a Formular with WETextbox and WESelectCascade. The Value from the WETextbox was added to the URL but not the values from the WESelectCascade. The allelements-String in the WEBuilder-Function includes the WESelectCascade-Elements. I

  • DBMS_AQ.dequeue_array always returns 0 messages

    Hi all, first of all, thanks for your time reading this... I have to dequeue messages from a Oracle queue and for performance reasons, I must dequeue batches of messages and not one by one. I have a logmessage object that it is what is queued: CREATE

  • Firewire Connect to a PC Help!

    I'm trying to connect my IMac to my laptop (PC) using Firewire. I know you can link two Macs using Target disk mode but is it possible to link a PC to a MAC So I can copy my old tunes from my PC Any help greatly appreciated!!!

  • How to sync phone and photos to recover 2k lost pictures

    i dont know exactly if i deleted all of my pictures or if they vanished with my last update... hard to say but i had about 2400 pictures... they are however on my iphone and im curious to see if i sync my phone and computer if it will import them all

  • With 20DB boost, mic input comes out from speaker - SB X-Fi XtremeMusic

    If? +20DB boost is turned on, michrophone input or my voice comes out from the speakers even "Listen to this device" is unchecked on the Sound setting. If I start playing music by WMP under this condition and raise tthe speaker volume above certain l