Another Forcing Java Garbage Collection Question (with *I think* a justifiable use case) -


i have data producer produces data used number of different consumers (let's call them cs). each c has own set of data it's interested in. data producer has weak references cs can notified updates.

each c used number of independent components, no single 1 component can tell when c no longer needed, have leave jvm determine when c gc-able.

the goal here notified when c becomes obsolete, data producer can turn off data interesting unused c.

as mentioned earlier, data producer has weak references cs, it's easy when c gced. however, noticed cs stick around in vm extended period of time before it's gc-ed , while, data producer producing lot of data should have needed to.

is there way me force gc of unused cs? hoping yes, i'm expecting no, follow-up question, have suggestion how can (re)design make work better/more efficiently?

thank you!!

some answers below make lot of points - in particular suggestion force users of c subscribe , unsubscribe. difficult because:

c 1 possible implementation of interface (which doesn't have subscription requirement), users use. c implementation injected during run-time, can't force users comply c-specific requirements. (ouch?)

the components use c can notify when interested , no longer interested.

when there @ least 1 interested component, turn on subscription. (not before appears case here), when last component un-subscribes, turn off data.

set<c> components = ...  public void subscribesto(c component) {    if (components.isempty())        enablesubscription();    components.add(component); }  public void unsubscribefrom(c component) {    components.remove(component);    if (components.isempty())        disablesubscription(); } 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -