Skip to content
This repository has been archived by the owner on Jun 18, 2023. It is now read-only.

The GarbageCollector aspect. #20

Open
GoogleCodeExporter opened this issue Aug 15, 2015 · 6 comments
Open

The GarbageCollector aspect. #20

GoogleCodeExporter opened this issue Aug 15, 2015 · 6 comments

Comments

@GoogleCodeExporter
Copy link

- Create the @GarbageCollector annotation; the annotation should be on the
methods.
- The annotation contains a parameter (minFreeMemory). 

The idea is to annotate methods with @GarbageCollector annotation; when the
annotated method is executed and the amount of free memory is less than the
value of minFreeMemory parameter then a call to the System.gc() is made.

Original issue reported on code.google.com by adyc...@gmail.com on 1 Jan 2009 at 4:05

@GoogleCodeExporter
Copy link
Author

I don't know if this is a god idea because System.gc will remove all the unused
objects so this operation is quite expensive.

Original comment by adyc...@gmail.com on 1 Jan 2009 at 4:35

@GoogleCodeExporter
Copy link
Author

Here is a possible version for this aspect.

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


/**
 * Aspect that is used to automatically execute a {@link System#gc()} when the
 * level of free heap memory is less than {@value #HEAP_MINIMUM_ALLOWED_LEVEL}.
 * 
 *
 */
@Aspect
public class GarbageCollectorAspect {

    /**
     * The minimum level of free heap memory allowed.
     */
    private static final long HEAP_MINIMUM_ALLOWED_LEVEL = 300000;

//  @Pointcut("cflow(execution(@GarbageCollectorTrigger * *(..))) "
//        + "&& !within(GarbageCollectorAspect+)"
//        + "&& within(com.xxx.xxx.xxx."
//        + "handler.XIR2ArchiveFileHandler+)")
//  @Pointcut("cflow(execution(@GarbageCollectorTrigger * *(..))) "
//  + "&& !within(GarbageCollectorAspect+)")

    @Pointcut("execution(public void com.xx.xxx.xxx."
        + "archiveload.handler.*.startElement(..))")
    void executionOfStartElementMethodsJointPoint() {
    }

    /**
     * Before every execution of the method intercepted by the pointcut 
     * described by 
     * {@link #executionOfStartElementMethodsJointPoint()} the advice verifies 
     * the level of free heap memory and if the level is less than 
     * {@value #HEAP_MINIMUM_ALLOWED_LEVEL} then a {@link System#gc()} is executed.
     * 
     * @param thisJoinPoint aspectJ generated object.
     */
    @Before("executionOfStartElementMethodsJointPoint()")
    public void executionOfStartElementMethodsAdvice(JoinPoint thisJoinPoint) {
        long freeHeapMemory = Runtime.getRuntime().freeMemory();

        if (freeHeapMemory < HEAP_MINIMUM_ALLOWED_LEVEL) {
            System.out.println("---------System.gc executed !--------");
            System.gc();
        }
    }
}    

Original comment by adyc...@gmail.com on 5 Jan 2009 at 11:02

@GoogleCodeExporter
Copy link
Author

Original comment by adyc...@gmail.com on 11 May 2009 at 5:13

  • Added labels: Milestone-Release0.5

@GoogleCodeExporter
Copy link
Author

Original comment by adyc...@gmail.com on 11 May 2009 at 5:15

  • Added labels: Milestone-Release0.4
  • Removed labels: Milestone-Release0.5

@GoogleCodeExporter
Copy link
Author

Original comment by adyc...@gmail.com on 15 Nov 2009 at 6:32

  • Removed labels: Milestone-Release0.4

@GoogleCodeExporter
Copy link
Author

Original comment by adyc...@gmail.com on 22 Jan 2011 at 5:20

  • Changed state: New

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant