Guice memory leaks and current working workarounds

130 views
Skip to first unread message

calvin...@gmail.com

unread,
May 8, 2015, 4:02:11 PM5/8/15
to google...@googlegroups.com

Summary:
Issues with memory leaks when using multiple guice instances in succession

Scenario:

Bring up a guice container (instance1) and allow the application to run with instance1, after some time bring up new guice container (guice2) and if successful atomically switch the application to instance2.  If instance2 fails to start for any reason then keep using instance1.  Once the switch is complete then let guice1 be garbage collected.  After the application run for a while with intance2 restart the whole process and bring up instance4, then instance5 ...

Problem:

Guice filter always contains a reference to any new guice instance.  In this case the guice filter is included in the guice container instance so each time guice starts a new guice fitler gets created and used correctly since the application determines which guice filter to submit the request to.  In the case that instance2 failed to start then instance2 should be garbage collected and not stored in guice filter.

Workaround:

1. use reflection to call GuiceFilter.reset  This is a static method and clears the unneeded reference.  The guice filter still has reference to the correct guice instance via the variable injectedPipeline

Source code of workaround:

            Class guiceFilterClass = GuiceFilter.class;
            Method[] methods = guiceFilterClass.getDeclaredMethods();
            for (Method method : methods) {
                if (method.getName().equals("reset")) {
                    method.setAccessible(true);
                    method.invoke(null);
                    break;
                }
            }

Luke Sandberg

unread,
May 8, 2015, 9:39:05 PM5/8/15
to google...@googlegroups.com
i think you can call "new GuiceFilter().destroy()" and get a similar effect with no reflection.

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/d3c6677a-1488-4b22-9d98-d7c4e238c78f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Laszlo Ferenczi

unread,
May 9, 2015, 2:07:16 AM5/9/15
to google...@googlegroups.com
Hi,

Can you explain why do you need to throw away the injectors after a given time ?
How you handle singletons and provider injections after the injector swap ?

Thanks !

--
L

Reply all
Reply to author
Forward
0 new messages