Very slow garbage collection when overriding finalize

I am working on a 3D visualization program, and smooth animation is very important; long pauses for GC is unacceptable.
GC is normally very fast, but when I tried to override finalize() to do some necessary cleanup this is no longer the case. I wrote a simple test application to verify this behaviour (included below), and ran it with java -verbose:gc.
Here is the output when commenting out finalize():
[GC 511K->248K(1984K), 0.0035095 secs]
[GC 760K->265K(1984K), 0.0016098 secs]
[GC 777K->265K(1984K), 0.0010843 secs]
[GC 777K->265K(1984K), 0.0009727 secs]
[GC 777K->265K(1984K), 0.0006170 secs]
[GC 777K->265K(1984K), 0.0005789 secs]
[GC 777K->265K(1984K), 0.0005970 secs]
[GC 777K->265K(1984K), 0.0006351 secs]
And here is the same when overriding finalize():
[GC 511K->285K(1984K), 0.0042582 secs]
[GC 797K->797K(1984K), 0.0120764 secs]
[GC 1309K->1260K(1984K), 0.0106945 secs]
[GC 1772K->1772K(2368K), 0.0124083 secs]
[Full GC 1772K->900K(2368K), 0.0306330 secs]
[GC 1412K->1412K(2080K), 0.0106363 secs]
[GC 1924K->1920K(2464K), 0.0111980 secs]
[Full GC 1920K->908K(2464K), 0.0318079 secs]
Can someone explain what is going on? I assume Java is doing quite a lot of work just in order to run my finalize, but this seems a bit much; the GC time is two orders of magnitude slower!
I am running SUN Java 1.4 under Linux.
My test application:
class C
    int i;
    int[] a = new int[20];
    public C(int i)
        this.i = i;
        for (int j=0; j<a.length; j++) a[j] = i*j;
    protected void finalize() {
        if (i > 200) return;
class gctest
    public static void main(String[] args)
        C[] c = new C[500];
        for (int i=0; i < 100000; i++) {
            c[i%c.length] = new C(i);
            long l = System.currentTimeMillis()+1;
            while (System.currentTimeMillis() < l);
}Thanks.
Kristian Eide

Let me keep it simple. gussev is correct.
Do NOT use finalize. I don't know what that other poster read that suggested in rare cases finalize is needed. Finalize can never be usefull for anything but hints at when your class has been "finalized."
You must use something from the java.lang.ref package. It has been said that finalize is not guaranteed to run. For you I am assuming it means a memory leak. If it does not mean a memory leak because when the java process is done, the c++ process will always be done too, then you can use finalize, but its not recommended. Yes, when your object has a finalize method defined, it will delay its collection. Normally if their is no finalize method the GC will just collect the object, but if their is a finalize, the GC has to consider that this may take time, so it can postpone GC until it feels it has time, or it has no choice. looking at your output and the addition of the word "Full GC" to the second set, I am assuming the GC has postponed as long as it can and when it finally runs, its collecting a nice size set of objects.
Suck it up, bite the bullet, take courage, do what you gotta do, etc. etc... Forget about finalize :D I think a WeakReference is likely most appropriate for your application. You may need to make your own thread that cleans up these objects though, and give it a lower priority than your drawing thread. In essence you will be determining when finalize is run so to speak as opposed to letting the GC, and you are smarter than the GC, or at least thats the idea :)
This should get you started
http://developer.java.sun.com/developer/technicalArticles/ALT/RefObj/

Similar Messages

  • Firefox is very slow to respond when opening and nearly always stops responding altogether even refusing to react to Task Manager "End Program@ command. The "not responding@ error message also comes up during navigation on line.

    Firefox is very slow to respond when opening and nearly always stops responding altogether, even refusing to react to the Task Manager "End Program" command. The "not responding" error message also comes up during navigation on line. Several attempt have to be made to get on line. A loss of stability seems to be endemic at the moment.

    Try following the instructions here: [[Firefox hangs]]

  • GT70 internet very slow and sluggish when waking from sleep

    My GT70 sometimes is very slow and sluggish to respond when it is awoken from sleep mode. Browsers become unresponsive (hyperlinks don't register as links and require multiple clicks to respond) and internet gets very slow. Speedtest.net reports  .50-3mb/s when it is usually 24mb/s and above.  The hdd also thrashes about and won't calm down for what seems like an eternity.
    This only happens when opening the lid after the machine sits idle for several hours. This is fixed by restarting the machine.
    Does anyone know why this laptop would be behaving like this? it is very frustrating and makes sleep mode pretty much useless.
    cheers,
    -deluxxe

    Quote from: darkhawk on 22-November-13, 13:17:05
    First thing I would do is get CPU-Z and open that after wake-up and seeing if the CPU is running at the speed expected, or if it's stuck at some lower speed.
    thanks for the reply. will try that next time it happens.

  • Very SLOW Pages 09 when not connectec to the Internet. !!!

    Hi:
    Im Writing a document that has the following Stats:
    words: 7,279
    Pages: 32
    Lines: 689
    Paragraphs: 164
    Sections :10
    Grapics: 2
    Characters: 46,916
    Character No spaces: 39,831
    It also contains a content table.
    The thing is today I had no internet at my house and I noticed that pages '09 was Very SLOW openning and Saving My document. I tried with other documents and they all behave as the one described before.
    I Describe the document because I have seen posts here of slow pages due to document structure. But I dont think this is document related because it happened it all the documents i opened and even just loading pages. My machine was as allways, all the programs running great except Pages.
    I also noticed that when Arrived at work and connected to the internet the Slowliness had ENDED.
    Anyone knows what is happening??

    PeterBreis0807 wrote:
    Pages links to the Net for:
    1 Updates
    once a month.
    2 Help
    the first time we enter the help in a working session
    3 The links in the startup screen.
    I never used such an item.
    There are ways to stop this behavior. The best is to use *Little Snitch* which allows you to stop certain connections being made. Basically when the attempt to connect is made, *Little Snitch* intercepts it and lets you choose: Allow, Allow once, Disallow once, Disallow forever.
    4 Users who had *Little Snitch* also noticed that Pages '09 was trying to connect to a web advertising site. Why this happens, ask Apple.
    Too late, Apple removed the task
    Yvan KOENIG (from FRANCE jeudi 19 février 2009 15:56:53)

  • MDT Wizard very slow to start when launching Litetouch.vbs

    Hi,
    For a few months, I have an issue when launching Litetouch.vbs, the Wizard initialisation il very slow. Normaly it takes 20 to 30 seconds to launch, now it takes about 4 minutes. The wizard hang 3 times for 1.5 minute. The problem is present on
    many computers here and even on a brand new installed computer.
    Something strange, when the Wizard have been launched a first time, then closed. The next time I launch Litetouch.vbs, I is fast (20 to 30 seconds).
    When I check the LOG : C:\MININT\SMSOSD\OSDLOGS\BDD.log, the lines that seems to hang are :
    <![LOG[Property definition is now = BDD_Welcome_ENU.xml]LOG]!>
    <![LOG[Property definition is now = WelcomeWiz_Initialize.xml]LOG]!>
    <![LOG[Property definition is now = DeployWiz_Definition_ENU.xml]LOG]!>
    Does anyone have an idea what can be the problem ?
    Thanks!
    Dominic

    The lines that are running slow above are when MDT is trying to write to the variables.dat file. There is nothing special about this operation, it's just reading/writing to a text file.
    I still think there is something else on this machine that is causing the litetouch.wsf scripts to run slowly. Perhaps you have some anti-virus programs causing problems.
    Best way to isolate the problem is to load a clean version of windows on a machine (without any corporate apps), and try running MDT litetouch again.
    Keith Garner - Principal Consultant [owner] -
    http://DeploymentLive.com

  • Firefox responds generally very slow but fast when moving the mouse

    Firefox respons very slow but speeds up when I move the mouse over the page. Keeping the mouse moving results in normal speed.
    ?? Peter

    Weird thing is that if I revert back to FF15, everything works with HA enabled and full bookmark menu.
    but its a no go with FF16
    PEACE

  • Webpage very slow to load when going to the ACE

    Hello,
    I was wondering if anyone else has experienced the same sort of behaviour with an ACE 4710 version A3 (2.5).
    When trying to connect to a webpage on port 443, the pages over 1 minute to download fully. I have timed the download using the plug-in
    firefox. What is strange is that the browser successfully makes the connection, 200OK etc,  but each 'get' takes a very long time to transfer. If I go directly to the server and don't pass via the ACE the page takes between .7 and 1 second to transfer.
    Is there a way to speed this transfer up, does anyone know what the issue could be? I am currently looking at connection maps to see if this could help.
    For information, there are quite a few .js files being transfered.
    Any ideas or thoughts very much appreciated.
    Kind regards,

    Hi,
    There is no clear evidence of the problem but this could also be due to lot of retransmits causing slowness.
    Can you try the command below and see if it improves?
    host1/C1(config-parammap-conn)# set tcp wan-optimization rtt 0
    Then apply this parammap to the policy-map.
    Regards,
    Siva

  • 2 Page doc = very slow, almost unworkable when typing Slow slow slow...

    Did some research, latest was posted in FEB.
    What is the deal with this? The doc has some images, but they are small jpegs. It should NOT have a 1200 millisecond, or 1.2 second delay when typing.
    Its becoming very difficult to work with.
    Any ideas?

    You're not the only one with this problem. There is a long discussion, with some proposed solutions, in this thread (and searching for "slow" in the Pages forum will bring up other threads as well).

  • Very slow PDF generation when conditional text is hidden in FM 9.0

    I am working on 96-page chapter of a larger book. A 7-page section of the chapter needs to be hidden until the next release, so I hid it with conditional text. After doing this, the "Save as PDF" printing process goes from taking 6-7 seconds to taking several minutes. If I unhide the conditional text in the same session, printing is very quick again.
    I have no problem generating very PDF files with the PDF printer in other applications (Word, PowerPoint). The issue is specific to FrameMaker and the use of conditional text. What might be causing this?
    I am using FrameMaker 9.0p255 and Distiller 9.0.
    - Chris

    Hi Jeff,
    These were good suggestions to try. Saving the document first has no effect on PDF generation.
    However, I did find that printing to the Adobe PDF printer remains fast when the conditional text is hidden. That's quite an interesting finding! I did also notice that these two methods of PDF generation aren't identical:
    "Save as PDF" opens an Adobe Distiller window, while printing to the Adobe PDF printer opens a different "Creating Adobe PDF" progress box (looks like it's from the printer driver itself).
    "Save as PDF" generates a 1188k PDF file, while printing to the Adobe PDF printer generates a 1049k PDF file.
    "Save as PDF" defaults to placing the PDF in FrameMaker's working directory, while printing to the Adobe PDF printer defaults to saving to the last place to saved a printed PDF file (awkward when working with multiple directories, as I often do).
    When I do a "Save as PDF", FrameMaker does still spool to the PDF printer. It is this spooling that takes a long time. Once spooling is complete, then the Distiller window opens and the PDF is created very quickly.
    I would still like to get "Save as PDF" working since it knows where to place the PDF file. No solution to the runtime yet, but at least there is progress...

  • After Installing Bitdefender 2011 Firefox is very slow and worse when multiple tabs are open

    Firefox performs very poorly after I installed Bitdefender 2011. There was no such problem with Bitdefender 2009. When multiple tabs are open, switching between them is not possible until tab contents are completed.

    As ninjafox says, the problem seems to be the BitDefender Anti-phishing Toolbar. Disabling this through Tools -> Add-ons and restarting Firefox seems to fix the problem for that session. However, the problem then reappears when the computer is restarted, presumably because BitDefender re-enables the toolbar.
    Another way of disabling all add-ons when starting Firefox is to hold down Shift while clicking on the icon (Windows only I think - at least doesn't work on Ubuntu for me).

  • Very Slow CURSOR MOVEMENT when drawing Automation

    I must've accidentally switched on some "fine" movement mode: when I try to increase or decrease automation - volume, pan, etc. - my mouse moves VERY SLOWLY in very small increments. How do I switch it back to normal, please?
    Thanks!

    Some tracks it will work properly, other tracks it will not. Any suggestions?
    no, i'm not holding down the control key.

  • Very slow performance jclient when running with remote server

    We have performance problems when running a JClient application, if the Application Server is on a different machine in the same 100mbit network. In our application we open 6 panels with about 15 TextFieldBindings each, on a tabbed pane. Each panel has it's own viewobject on the server. It takes the panel allmost two minutes to start up. Our own code seems to perform reasonable, but between the last line of code and the actual visibility of the panel there is a long period of low intensity network traffic between the client and the server machine, while both machines have low CPU usage. We tried setting the synchmode of the ApplicationModule to SYNC_LAZY and SYNC_IMMEDIATE, but this does not seem to make any difference.
    It seems as if the server starts throwing a lot of events after our code is executed, which are caught by the BC4J controlbinding listeners. The performance is a lot better if we have the server and the client on the same machine, and the database on a different one.
    This kind of performance is not acceptable for this application. Are we doing something that should not be done with BC4J, or are we missing something?

    We have performance problems when running a JClient application, if the Application Server is on a different machine in the same 100mbit network. In our application we open 6 panels with about 15 TextFieldBindings each, on a tabbed pane. Each panel has it's own viewobject on the server. It takes the panel allmost two minutes to start up. Our own code seems to perform reasonable, but between the last line of code and the actual visibility of the panel there is a long period of low intensity network traffic between the client and the server machine, while both machines have low CPU usage. We tried setting the synchmode of the ApplicationModule to SYNC_LAZY and SYNC_IMMEDIATE, but this does not seem to make any difference.
    It seems as if the server starts throwing a lot of events after our code is executed, which are caught by the BC4J controlbinding listeners. The performance is a lot better if we have the server and the client on the same machine, and the database on a different one.
    This kind of performance is not acceptable for this application. Are we doing something that should not be done with BC4J, or are we missing something? You must be hitting a performance issue regarding download of all property metadata for setting labels etc. on the UI (in case of remote-tier deployment).
    This issue has been resolved for our next release of JDeveloper. Basically a new api has been added that allows 3tier apps to "download" the set of "used" VO definitions, Attribute Definitions etc on the client, so that the UI comes up quick.
    Also a application/ui/binding load code-generation has been modified to allow for "lazy" loading of controls/lazy binding etc, quite like what's done in the JClient Control-bindings sample on otn.
    For 9.0.2, you may shorten the "load" time, by loading only the UI that's first displayed and pre-loading ViewObject definition. However it'll still be slower than what the above mentioned method would do in one roundtrip.

  • Very slow wireless internet when running on AC power

    Since installing Leopard (and then updated to 10.5.5), I have been having really incredibly slow wireless internet access - however it is only like this when the AC power cable is plugged in. If I pull the power lead out and work on the battery, then the internet starts working again almost immediately. The airport signal isn't lost at any point, so I am a bit confused.
    I have reset the PRAM, entered new DNS servers (opendns.org ones) and even set my RWIN lower as described here: http://discussions.apple.com/thread.jspa?messageID=5670430 but with no noticeable result.
    My macbook pro is 2.33ghz Core 2 duo. I'm using a Belkin n1 router with WEP encryption. The idea that the power supply would affect my wireless internet like this seems really bizarre. I would be grateful if anyone has any idea what could be going on here.

    Well l.e.o, since no one else is taking a stab: check your System Preferences > Hardware > Energy Saver settings. See if Settings for Power Adapter, Optimization, is not set for "Better Performance." It may be set for "Better Energy Savings," and the settings for Battery are set to Optimized for Better Performance?

  • Motorola 850 Very slow to connect when answering phone

    I have just connected my BT Motorola HS850 to the Iphone and the pairing went well. When I tried to answer the phone using the BT device I did not receive any audio nor could I transmit for about 9 seconds after I answered the call. Any ideas?

    Hi, I'm having the same issue also with Motorola HS850. The headset worked fine with my Blackberry Pearl.
    Thanks for any advice.

  • Premiere is very slow to export when title is added

    Hello.
    I have a 60 second piece of media that is the header for all my exports. It consists of 30 seconds of bars/tone, 27 secs of a moving clock and then 3 secs of black. It was supplied as MXF OP1a and is 376 MB.
    On each export I overlay text to the 27 seconds of clock for versions, durations etc. To export this takes about 5 minutes and it appears to be the text that is the problem. If I remove the text, the export of just the remaining parts takes about 10 seconds.
    I've tried it direct from Premiere and via encoder and it's the same.
    When I have 40 items to export with the header it adds massively to the export time. Some items are 30" spots and it's always the clock text that takes up the time.
    I have tried pre-rendering the text but this would have to be done on each export sequence and is not a great workflow.
    Someone suggested making the text in After Effects but again that is not a great workflow. I would rather have this self contained within the project.
    Is this normal for premiere? I've searched the forum and cannot find a similar thread.
    Premiere 7.2.1
    OSX 10.9.2
    3.06 GHz Intel Core Duo
    16GB RAM (late 2009 mac with extra RAM added)
    Mercury Playback Software Only
    I'm not sure the mac is relevant. I have tried this on a new 27 inch mac with 32 GB RAM and CUDA and the slowdown is still there.
    Thanks for any help.

    Start Firefox in <u>[[Safe Mode|Safe Mode]]</u> to check if one of the extensions (Firefox/Tools > Add-ons > Extensions) or if hardware acceleration is causing the problem (switch to the DEFAULT theme: Firefox/Tools > Add-ons > Appearance).
    *Do NOT click the Reset button on the Safe mode start window or otherwise make changes.
    *https://support.mozilla.org/kb/Safe+Mode
    Try to disable hardware acceleration in Firefox.
    *Tools > Options > Advanced > General > Browsing: "Use hardware acceleration when available"
    *https://support.mozilla.org/kb/Troubleshooting+extensions+and+themes
    *https://hacks.mozilla.org/2010/09/hardware-acceleration/

Maybe you are looking for