Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » [SOLVED] Using P2 garbage collector(How to access from code)
[SOLVED] Using P2 garbage collector [message #1403723] Fri, 25 July 2014 14:47 Go to next message
Alexandros Karypidis is currently offline Alexandros KarypidisFriend
Messages: 104
Registered: March 2010
Senior Member
Hi,

I have a problem with the instructions in the Equinox/p2 FAQ, which provides this snippet for running the garbage collector from code:

IProvisioningAgentProvider provider = // obtain the IProvisioningAgentProvider using OSGi services
IProvisioningAgent agent = provider.createAgent(null);  // null = location for running system
if(agent == null) throw new RuntimeException("Location was not provisioned by p2");
IProfileRegistry profileRegistry = (IProfileRegistry) agent.getService(IProfileRegistry.SERVICE_NAME);
if (profileRegistry == null) throw new RuntimeException("Unable to acquire the profile registry service.");
// can also use IProfileRegistry.SELF for the current profile
IProfile profile = profileRegistry.getProfile("SDKProfile");
GarbageCollector gc = (GarbageCollector) agent.getService(GarbageCollector.SERVICE_NAME);
gc.runGC(profile);


I want to use this code to explicitly invoke the GarbageCollector, but his class is inaccessible due to an access restriction (the "org.eclipse.equinox.p2.garbagecollector" plugin exports package "org.eclipse.equinox.internal.p2.garbagecollector" with an "x-friends" restriction for "org.eclipse.equinox.p2.touchpoint.eclipse" and "org.eclipse.pde.core". Here is the MANIFEST.MF snippet:

Export-Package: org.eclipse.equinox.internal.p2.garbagecollector;x-fri
 ends:="org.eclipse.equinox.p2.touchpoint.eclipse,org.eclipse.pde.core
 "


So what is the "proper" way to invoke the GC from code? (would be a nice update for the FAQ page as well)

[Updated on: Thu, 07 August 2014 13:14]

Report message to a moderator

Re: Using P2 garbage collector [message #1405162 is a reply to message #1403723] Wed, 06 August 2014 18:25 Go to previous message
Alexandros Karypidis is currently offline Alexandros KarypidisFriend
Messages: 104
Registered: March 2010
Senior Member
The only way I've managed to run the garbage collector in a "clean" way, is to mock an application context and fire up a "nested" application like this:

	public static final String P2_GC_PLUGIN_ID = "org.eclipse.equinox.p2.garbagecollector";
	public static final String APPLICATION_EXT_POINT = "org.eclipse.core.runtime.applications";

	IApplication app = null;
	IExtensionRegistry reg = Platform.getExtensionRegistry();
	IExtensionPoint ep = reg.getExtensionPoint(APPLICATION_EXT_POINT);
	IExtension[] extensions = ep.getExtensions();
	found: for (int i = 0; i < extensions.length; i++) {
		IExtension ext = extensions[i];
		IConfigurationElement[] ce = ext.getConfigurationElements();
		for (int j = 0; j < ce.length; j++) {
			IContributor contributor = ext.getContributor();
			String name = contributor.getName();
			if (P2_GC_PLUGIN_ID.equals(name)) {
				Object obj = ce[j].createExecutableExtension("run");
				app = (IApplication) obj;
				break found;
			}
		}
	}

	if (app == null) {
		throw new RuntimeException(
				"Could not find P2 garbage collection application extension.");
	} else {
		app.start(getP2AppContextForProfile("profile"));
	}


The mock context simple returns the "-profile" argument that specifies the profile name:

	private static IApplicationContext getP2AppContextForProfile(final String profileName) {
		return new IApplicationContext() {
			@Override
			public Map<?,?> getArguments() {
				Map<String, Object> m = new HashMap<String, Object>();
				m.put(IApplicationContext.APPLICATION_ARGS, new String[] {
						"-profile", profileName });
				return m;
			}
// all other methods do nothing and return null
		};
	}

[Updated on: Wed, 06 August 2014 19:16]

Report message to a moderator

Previous Topic:Avoid adding to known repositories when using p2 director
Next Topic:Simple configurator and bundle uninstall
Goto Forum:
  


Current Time: Mon Nov 19 05:38:51 GMT 2018

Powered by FUDForum. Page generated in 0.02096 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top