Delayed restart of Firefox: interactive and automatic ways

    There are times when Firefox has to be left unattended for a long time (for example, it runs an extension that collects critical news, or an integrated torrent client, or even a mini-server). It is no secret that with the time the browser is running, memory consumption increases. Sooner or later, the system may feel uncomfortable. Therefore, it would be nice to be able to ask the browser pending restart tasks. At least three types of such tasks are possible.



    1. Single restart upon user request


    First install the Custom Buttons extension . It allows you to create buttons on the toolbar to execute arbitrary code (moreover, the code will be privileged, with extension rights). In essence, this is a framework for quickly creating mini-extensions.

    After installation, create a button and enter the following code in the first settings tab of this button:

    var timeout = prompt ("Enter the number of minutes before restarting:", "1440");
    if (timeout) {
    	window.setTimeout ("Application.restart ();", timeout * 60 * 1000);
    }

    In the dialog called up by pressing the button, you can enter the number of minutes before the restart. By default, the day is set.



    2. Unconditional periodic restart without user intervention


    If the browser is left to itself for a particularly long period, you may have to resort to repeated periodic restarts. To do this, in the second tab of the settings of the newly created button (tab "Initialization") you need to enter the following code:

    var timeout = 1440; // minutes
    window.setTimeout ("Application.restart ();", timeout * 60 * 1000);

    After each launch of the browser, the time set by the parameter will begin timeout. Again, a default once a day restart is defined, but the user can replace the number 1440 with any other (do not accidentally enter a too small interval (fraction), otherwise you will have to deal with the consequences in safe mode).

    If you need to restart at the specified time, and not after the specified period, there is a suitable solution.



    3. Conditional periodic restart without user intervention


    To the previous method, we can add a check of one of the suitable conditions. Consider two: browser downtime and memory usage.

    A. Checking user inactivity time

    The function will check the inactivity time every minute, and if it exceeds the parameter idleLimit(set in minutes), the browser will restart.

    var idleLimit = 60; // minutes
    var idleService =
    	 Components.classes ["@ mozilla.org/widget/idleservice;1"]
    	.getService (Components.interfaces.nsIIdleService);
    window.setInterval (
    	function () {
    		if (idleService.idleTime / 60000> idleLimit) {
    			Application.restart ();
    		}
    	},
    	60,000
    );

    B. Check the amount of memory

    Everything is a little more complicated here. The MDC search did not provide documented interfaces for obtaining such information. But in Firefox 3.6 a page appeared about:memory. An analysis of its code leads to a file chrome://global/content/aboutMemory.jsthat uses several again undocumented interfaces. With their help, you can take a chance on such a code (the function goes through the available memory types, and if at least one of them exceeds the set limit (specified in megabytes), the browser will restart):

    var memoryLimit = 200; // megabytes
    var enumeratedReporters =
    	 Components.classes ["@ mozilla.org/memory-reporter-manager;1"]
    	.getService (Components.interfaces.nsIMemoryReporterManager)
    	.enumerateReporters ();
    while (enumeratedReporters.hasMoreElements ()) {
    	if (
    		 enumeratedReporters.getNext ()
    		.QueryInterface (Components.interfaces.nsIMemoryReporter)
    		.memoryUsed / 1000000
    			> memoryLimit
    	) {
    		Application.restart ();
    	}
    }

    However, it is not known how the interfaces used depend on operating systems (there are mentions on the network that it about:memorydoes not work on Linux). It is also unclear in which version of Firefox these interfaces appeared. Therefore, I would be grateful to the readers if they could test the following code and write in the comments about what it gives on different OC and in different versions of Firefox (try not to repeat the configurations that you already wrote about):

    try {
    	var enumeratedReporters =
    		 Components.classes ["@ mozilla.org/memory-reporter-manager;1"]
    		.getService (Components.interfaces.nsIMemoryReporterManager)
    		.enumerateReporters ();
    	var report = "";
    	while (enumeratedReporters.hasMoreElements ()) {
    		var memoryReporter =
    			enumeratedReporters.getNext ()
    			.QueryInterface (Components.interfaces.nsIMemoryReporter);
    		report + =
    			memoryReporter.path + ": \ t" +
    			memoryReporter.memoryUsed / 1000000 + "\ n";
    	}
    	alert (report || "Memory information not available.");
    } catch (error) {alert (error)}

    B. Combine both checks

    The function checks downtime every minute. When it exceeds the limit, the function will begin to check the memory size every minute. When this limit is exceeded, the browser will restart.

    var idleLimit = 60; // minutes
    var memoryLimit = 200; // megabytes
    var idleService =
    	 Components.classes ["@ mozilla.org/widget/idleservice;1"]
    	.getService (Components.interfaces.nsIIdleService);
    var reporterManager = 
    	 Components.classes ["@ mozilla.org/memory-reporter-manager;1"]
    	.getService (Components.interfaces.nsIMemoryReporterManager);
    window.setInterval (
    	function () {
    		if (idleService.idleTime / 60000> idleLimit) {
    			var enumeratedReporters = reporterManager.enumerateReporters ();
    			while (enumeratedReporters.hasMoreElements ()) {
    				if (
    				   enumeratedReporters.getNext ()
    				  .QueryInterface (Components.interfaces.nsIMemoryReporter)
    				  .memoryUsed / 1000000
    					> memoryLimit
    				) {
    					Application.restart ();
    				}
    			}		
    		}
    	},
    	60,000
    );



    It remains to add that before setting the restarts, you should disable the check for browser updates and extensions (or at least make the updates automatic), otherwise the update dialogs may block Firefox after the next restart.

    Also popular now: