Package java.lang.management

Examples of java.lang.management.MemoryNotificationInfo


        }
    }
   
    public void handleNotification(Notification n, Object o) {
        CompositeData cd = (CompositeData) n.getUserData();
        MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);
        // free the amount exceeded over the threshold and then a further half
        // so if threshold = heapmax/2, we will be trying to free
        // used - heapmax/2 + heapmax/4
        long toFree = 0L;
        if(n.getType().equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
            long threshold = (long)(info.getUsage().getMax() * memoryThresholdFraction);
            toFree = info.getUsage().getUsed() - threshold + (long)(threshold * 0.5);

            //log
            String msg = "memory handler call- Usage threshold "
                + info.getUsage();
            if(!firstUsageThreshExceededLogged){
                log.info("first " + msg);
                firstUsageThreshExceededLogged = true;
            }else{
                log.debug(msg);
            }
        } else { // MEMORY_COLLECTION_THRESHOLD_EXCEEDED CASE
            long threshold = (long)(info.getUsage().getMax() * collectionMemoryThresholdFraction);
            toFree = info.getUsage().getUsed() - threshold + (long)(threshold * 0.5);
           
            //log
            String msg = "memory handler call - Collection threshold "
                + info.getUsage();
            if(!firstCollectionThreshExceededLogged){
                log.info("first " + msg);
                firstCollectionThreshExceededLogged = true;
            }else{
                log.debug(msg);
            }

        }
        
        if (toFree < 0) {
            log.debug("low memory handler returning " +
                "because there is nothing to free");
            return;
        }
        synchronized(spillables) {
            // Walk the list first and remove nulls, otherwise the sort
            // takes way too long.
            Iterator<WeakReference<Spillable>> i;
            for (i = spillables.iterator(); i.hasNext();) {
                Spillable s = i.next().get();
                if (s == null) {
                    i.remove();
                }
            }
            Collections.sort(spillables, new Comparator<WeakReference<Spillable>>() {

                /**
                 * We don't lock anything, so this sort may not be stable if a WeakReference suddenly
                 * becomes null, but it will be close enough.
                 * Also between the time we sort and we use these spillables, they
                 * may actually change in size - so this is just best effort
                 */   
                public int compare(WeakReference<Spillable> o1Ref, WeakReference<Spillable> o2Ref) {
                    Spillable o1 = o1Ref.get();
                    Spillable o2 = o2Ref.get();
                    if (o1 == null && o2 == null) {
                        return 0;
                    }
                    if (o1 == null) {
                        return 1;
                    }
                    if (o2 == null) {
                        return -1;
                    }
                    long o1Size = o1.getMemorySize();
                    long o2Size = o2.getMemorySize();
               
                    if (o1Size == o2Size) {
                        return 0;
                    }
                    if (o1Size < o2Size) {
                        return 1;
                    }
                    return -1;
                }
            });
            long estimatedFreed = 0;
            int numObjSpilled = 0;
            boolean invokeGC = false;
            for (i = spillables.iterator(); i.hasNext();) {
                Spillable s = i.next().get();
                // Still need to check for null here, even after we removed
                // above, because the reference may have gone bad on us
                // since the last check.
                if (s == null) {
                    i.remove();
                    continue;
                }
                long toBeFreed = s.getMemorySize();
                log.debug("Memorysize = "+toBeFreed+", spillFilesizethreshold = "+spillFileSizeThreshold+", gcactivationsize = "+gcActivationSize);
                // Don't keep trying if the rest of files are too small
                if (toBeFreed < spillFileSizeThreshold) {
                    log.debug("spilling small files - getting out of memory handler");
                    break ;
                }
                s.spill();              
                numObjSpilled++;
                estimatedFreed += toBeFreed;
                accumulatedFreeSize += toBeFreed;
                // This should significantly reduce the number of small files
                // in case that we have a lot of nested bags
                if (accumulatedFreeSize > gcActivationSize) {
                    invokeGC = true;
                }
               
                if (estimatedFreed > toFree) {
                    log.debug("Freed enough space - getting out of memory handler");
                    invokeGC = true;
                    break;
                }
            }          
            /* Poke the GC again to see if we successfully freed enough memory */
            if(invokeGC) {
                System.gc();
                // now that we have invoked the GC, reset accumulatedFreeSize
                accumulatedFreeSize = 0;
            }
            if(estimatedFreed > 0){
                String msg = "Spilled an estimate of " + estimatedFreed +
                " bytes from " + numObjSpilled + " objects. " + info.getUsage();;
                log.info(msg);
            }

        }
    }
View Full Code Here


        }
    }
   
    public void handleNotification(Notification n, Object o) {
        CompositeData cd = (CompositeData) n.getUserData();
        MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);
        // free the amount exceeded over the threshold and then a further half
        // so if threshold = heapmax/2, we will be trying to free
        // used - heapmax/2 + heapmax/4
        long toFree = 0L;
        if(n.getType().equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
            long threshold = (long)(info.getUsage().getMax() * memoryThresholdFraction);
            toFree = info.getUsage().getUsed() - threshold + (long)(threshold * 0.5);
            log.info("low memory handler called (Usage threshold exceeded) " + info.getUsage());
        } else { // MEMORY_COLLECTION_THRESHOLD_EXCEEDED CASE
            long threshold = (long)(info.getUsage().getMax() * collectionMemoryThresholdFraction);
            toFree = info.getUsage().getUsed() - threshold + (long)(threshold * 0.5);
            log.info("low memory handler called (Collection threshold exceeded) " + info.getUsage());
        }
        
        if (toFree < 0) {
            log.debug("low memory handler returning " +
                "because there is nothing to free");
View Full Code Here

        }
    }
   
    public void handleNotification(Notification n, Object o) {
        CompositeData cd = (CompositeData) n.getUserData();
        MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);
       
        // free the amount exceeded over the threshold and then a further half
        // so if threshold = heapmax/2, we will be trying to free
        // used - heapmax/2 + heapmax/4
        long toFree = 0L;
        if(n.getType().equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
            long threshold = (long)(info.getUsage().getMax() * memoryThresholdFraction);
            toFree = info.getUsage().getUsed() - threshold + (long)(threshold * 0.5);
            log.info("low memory handler called (Usage threshold exceeded) " + info.getUsage());
        } else { // MEMORY_COLLECTION_THRESHOLD_EXCEEDED CASE
            long threshold = (long)(info.getUsage().getMax() * collectionMemoryThresholdFraction);
            toFree = info.getUsage().getUsed() - threshold + (long)(threshold * 0.5);
            log.info("low memory handler called (Collection threshold exceeded) " + info.getUsage());
        }
        
        if (toFree < 0) {
            log.debug("low memory handler returning " +
                "because there is nothing to free");
View Full Code Here

        arr.add("Hegemony or survival ?");
        notifierBean.addNotificationListener(listener, null, arr);

        // Fire off a notification and ensure that the listener receives it.
        MemoryUsage mu = new MemoryUsage(1, 2, 3, 4);
        MemoryNotificationInfo info = new MemoryNotificationInfo("Lloyd", mu,
                42);
        CompositeData cd = ManagementUtils
                .toMemoryNotificationInfoCompositeData(info);
        Notification notification = new Notification(
                MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
View Full Code Here

        // Add a listener without a filter object.
        SimpleTestListener listener = new SimpleTestListener();
        notifierBean.addNotificationListener(listener, null, null);
        // Fire off a notification and ensure that the listener receives it.
        MemoryUsage mu = new MemoryUsage(1, 2, 3, 4);
        MemoryNotificationInfo info = new MemoryNotificationInfo("Sinclair",
                mu, 42);
        CompositeData cd = ManagementUtils
                .toMemoryNotificationInfoCompositeData(info);
        Notification notification = new Notification(
                MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
View Full Code Here

    }
   
    @Override
    public void handleNotification(Notification n, Object o) {
        CompositeData cd = (CompositeData) n.getUserData();
        MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);
        // free the amount exceeded over the threshold and then a further half
        // so if threshold = heapmax/2, we will be trying to free
        // used - heapmax/2 + heapmax/4
        long toFree = 0L;
        if(n.getType().equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
            long threshold = (long)(info.getUsage().getMax() * memoryThresholdFraction);
            toFree = info.getUsage().getUsed() - threshold + (long)(threshold * 0.5);

            //log
            String msg = "memory handler call- Usage threshold "
                + info.getUsage();
            if(!firstUsageThreshExceededLogged){
                log.info("first " + msg);
                firstUsageThreshExceededLogged = true;
            }else{
                log.debug(msg);
            }
        } else { // MEMORY_COLLECTION_THRESHOLD_EXCEEDED CASE
            long threshold = (long)(info.getUsage().getMax() * collectionMemoryThresholdFraction);
            toFree = info.getUsage().getUsed() - threshold + (long)(threshold * 0.5);
           
            //log
            String msg = "memory handler call - Collection threshold "
                + info.getUsage();
            if(!firstCollectionThreshExceededLogged){
                log.info("first " + msg);
                firstCollectionThreshExceededLogged = true;
            }else{
                log.debug(msg);
            }

        }
        clearSpillables();
        if (toFree < 0) {
            log.debug("low memory handler returning " +
                "because there is nothing to free");
            return;
        }
        synchronized(spillables) {
            Collections.sort(spillables, new Comparator<WeakReference<Spillable>>() {

                /**
                 * We don't lock anything, so this sort may not be stable if a WeakReference suddenly
                 * becomes null, but it will be close enough.
                 * Also between the time we sort and we use these spillables, they
                 * may actually change in size - so this is just best effort
                 */   
                @Override
                public int compare(WeakReference<Spillable> o1Ref, WeakReference<Spillable> o2Ref) {
                    Spillable o1 = o1Ref.get();
                    Spillable o2 = o2Ref.get();
                    if (o1 == null && o2 == null) {
                        return 0;
                    }
                    if (o1 == null) {
                        return 1;
                    }
                    if (o2 == null) {
                        return -1;
                    }
                    long o1Size = o1.getMemorySize();
                    long o2Size = o2.getMemorySize();
               
                    if (o1Size == o2Size) {
                        return 0;
                    }
                    if (o1Size < o2Size) {
                        return 1;
                    }
                    return -1;
                }
            });
            long estimatedFreed = 0;
            int numObjSpilled = 0;
            boolean invokeGC = false;
            boolean extraGCCalled = false;
            for (Iterator<WeakReference<Spillable>> i = spillables.iterator(); i.hasNext();) {
                WeakReference<Spillable> weakRef = i.next();
                Spillable s = weakRef.get();
                // Still need to check for null here, even after we removed
                // above, because the reference may have gone bad on us
                // since the last check.
                if (s == null) {
                    i.remove();
                    continue;
                }
                long toBeFreed = s.getMemorySize();
                log.debug("Memorysize = "+toBeFreed+", spillFilesizethreshold = "+spillFileSizeThreshold+", gcactivationsize = "+gcActivationSize);
                // Don't keep trying if the rest of files are too small
                if (toBeFreed < spillFileSizeThreshold) {
                    log.debug("spilling small files - getting out of memory handler");
                    break ;
                }
                // If single Spillable is bigger than the threshold,
                // we force GC to make sure we really need to keep this
                // object before paying for the expensive spill().
                // Done at most once per handleNotification.
                if( !extraGCCalled && extraGCSpillSizeThreshold != 0
                    && toBeFreed > extraGCSpillSizeThreshold   ) {
                    log.debug("Single spillable has size " + toBeFreed + "bytes. Calling extra gc()");
                    // this extra assignment to null is needed so that gc can free the
                    // spillable if nothing else is pointing at it
                    s = null;
                    System.gc();
                    extraGCCalled = true;
                    // checking again to see if this reference is still valid
                    s = weakRef.get();
                    if (s == null) {
                        i.remove();
                        accumulatedFreeSize = 0;
                        invokeGC = false;
                        continue;
                    }
                }
                s.spill();              
                numObjSpilled++;
                estimatedFreed += toBeFreed;
                accumulatedFreeSize += toBeFreed;
                // This should significantly reduce the number of small files
                // in case that we have a lot of nested bags
                if (accumulatedFreeSize > gcActivationSize) {
                    invokeGC = true;
                }
               
                if (estimatedFreed > toFree) {
                    log.debug("Freed enough space - getting out of memory handler");
                    invokeGC = true;
                    break;
                }
            }          
            /* Poke the GC again to see if we successfully freed enough memory */
            if(invokeGC) {
                System.gc();
                // now that we have invoked the GC, reset accumulatedFreeSize
                accumulatedFreeSize = 0;
            }
            if(estimatedFreed > 0){
                String msg = "Spilled an estimate of " + estimatedFreed +
                " bytes from " + numObjSpilled + " objects. " + info.getUsage();;
                log.info(msg);
            }

        }
    }
View Full Code Here

    private CompositeData memoryCompositeData;

    public void test_Constructor_NullPoolName_NullUsage() {
        try {
            new MemoryNotificationInfo((String) null, (MemoryUsage) null,
                    -4294901761L);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // Expected
        }
View Full Code Here

        }
    }

    public void test_Constructor_NullUsage() {
        try {
            new MemoryNotificationInfo("poolName", (MemoryUsage) null,
                    -4294901761L);
            fail("should throw NullPointerException");
        } catch (NullPointerException e) {
            // Expected
        }
View Full Code Here

        assertNull(MemoryNotificationInfo.from(null));
    }

    public void test_from() {
        final MemoryUsage memoryUsage = new MemoryUsage(1, 2, 3, 4);
        final MemoryNotificationInfo memoryNotifyInfo = new MemoryNotificationInfo("Lloyd", memoryUsage, 42);
       
        CompositeData compositeData = ManagementUtils
                .toMemoryNotificationInfoCompositeData(memoryNotifyInfo);
        MemoryNotificationInfo fromInfo = MemoryNotificationInfo
                .from(compositeData);
        assertEquals(memoryNotifyInfo.getPoolName(), fromInfo.getPoolName());
        assertEquals(memoryNotifyInfo.getCount(), fromInfo.getCount());

        MemoryUsage fromUsage = fromInfo.getUsage();
        assertEquals(memoryUsage.getInit(), fromUsage.getInit());
        assertEquals(memoryUsage.getMax(), fromUsage.getMax());
        assertEquals(memoryUsage.getUsed(), fromUsage.getUsed());
        assertEquals(memoryUsage.getCommitted(), fromUsage.getCommitted());
    }
View Full Code Here

        assertEquals(memoryUsage.getCommitted(), fromUsage.getCommitted());
    }

    public void test_getPoolName() {
        final MemoryUsage memoryUsage = new MemoryUsage(1, 2, 3, 4);
        final MemoryNotificationInfo memoryNotifyInfo = new MemoryNotificationInfo("Lloyd", memoryUsage, 42);
        assertEquals("Lloyd", memoryNotifyInfo.getPoolName());
    }
View Full Code Here

TOP

Related Classes of java.lang.management.MemoryNotificationInfo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.