[ 
https://issues.apache.org/jira/browse/BROOKLYN-528?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16148792#comment-16148792
 ] 

ASF GitHub Bot commented on BROOKLYN-528:
-----------------------------------------

Github user andreaturli commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/808#discussion_r136297846
  
    --- Diff: 
core/src/main/java/org/apache/brooklyn/core/objs/proxy/ClassLoaderCache.java ---
    @@ -0,0 +1,121 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.core.objs.proxy;
    +
    +import static com.google.common.base.Preconditions.checkNotNull;
    +
    +import java.util.Set;
    +import java.util.concurrent.ConcurrentMap;
    +
    +import org.apache.brooklyn.util.javalang.AggregateClassLoader;
    +
    +import com.google.common.base.Objects;
    +import com.google.common.collect.ImmutableSet;
    +import com.google.common.collect.Maps;
    +import com.google.common.collect.Sets;
    +
    +/**
    + * For getting/creating {@link AggregateClassLoader} for an entity proxy. 
The returned class loader
    + * can be used when calling {@link 
java.lang.reflect.Proxy#newProxyInstance(ClassLoader, Class[], 
java.lang.reflect.InvocationHandler)}.
    + * 
    + * The classloader is created with references to ClassLoaders for the 
given class and interfaces
    + * (including all super-classes and super-interfaces of those). Also see 
comment in
    + * {@link InternalEntityFactory#createEntityProxy(Iterable, 
org.apache.brooklyn.api.entity.Entity)}.
    + * 
    + * If a classloader has already been created for the given 
class+interfaces, then it guarantees
    + * to return that same classloader instance. This is very important when 
using {@code newProxyInstance}
    + * because if a different ClassLoader instance is used then it will 
reflectively create a new Proxy class.
    + * See https://issues.apache.org/jira/browse/BROOKLYN-528.
    + */
    +class ClassLoaderCache {
    --- End diff --
    
    [minor] @aledsage cache looks exactly what we need here.
    FYI @robertgmoss has used a `computeIfAbsent` in 
https://github.com/apache/brooklyn-server/blob/master/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/AbstractComputeServiceRegistry.java#L79
 which may save some boilerplate code.


> Memory leak: proxy class (and java.lang.reflect.* instances) per entity
> -----------------------------------------------------------------------
>
>                 Key: BROOKLYN-528
>                 URL: https://issues.apache.org/jira/browse/BROOKLYN-528
>             Project: Brooklyn
>          Issue Type: Bug
>    Affects Versions: 0.11.0
>            Reporter: Aled Sage
>            Assignee: Aled Sage
>
> There is a memory leak when creating and deleting entities.
> After creating and then deleting a really simple app many times, there are a 
> lot of classes with names like {{class com.sun.proxy.$Proxy10033}} (probably 
> one per entity we created!). This also leads to a huge number of instances of 
> things like {{java.lang.reflect.Method}}.
> e.g. deploy and delete the app:
> {noformat}
> services: 
> - type: org.apache.brooklyn.entity.stock.BasicApplication
> {noformat}
> ---
> Looking at the code in 
> {{org.apache.brooklyn.core.objs.proxy.InternalEntityFactory.createEntityProxy()}},
>  when it calls {{java.lang.reflect.Proxy.newProxyInstance}} it first creates 
> a new {{AggregateClassLoader}} instance and passes that in. This causes 
> {{newProxyInstance}} to create a new class every time.
> The fix should be simple: to reuse {{AggregateClassLoader}} instances, rather 
> than creating new ones.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to