<commit_before>/* 
 * Copyright 2016 <NAME> <<EMAIL>>.
 *
 * Licensed 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.rappsilber.gui.components.memory;

import java.awt.Component;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.LinkedList;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import org.rappsilber.utils.ObjectWrapper;
import org.rappsilber.utils.RArrayUtils;
import org.rappsilber.utils.StringUtils;

/**
 *
 * @author <NAME> <<EMAIL>>
 */
public class Memory extends javax.swing.JPanel {

    
    java.util.Timer m_scanTimer = new java.util.Timer(true);
    private int m_timeout = 600;
    Runtime runtime = Runtime.getRuntime();
    
    
    protected class ScanTask extends TimerTask {
        AtomicBoolean running = new AtomicBoolean(false);
        double recentMinFreeMem = 0;
        double recentMaxFreeMem = 0;
        LinkedList<Double> recent = new LinkedList<>();
        int maxRecent=10;
        int logMemory = 0;
        int didgc = 0;
        
        @Override
        public void run() {
            if (running.compareAndSet(false, true)) {
                try {
                    double fm = runtime.freeMemory();
                    String fmu = "B";
                    double mm = runtime.maxMemory();
                    double tm = runtime.totalMemory();
                    double um = tm-fm;
                    recent.add(um);
                    if (recent.size()>maxRecent) {
                        recent.removeFirst();
                    }
                    ObjectWrapper<Double> min= new ObjectWrapper<>();
                    ObjectWrapper<Double> max = new ObjectWrapper<>();
                    RArrayUtils.minmax(recent,min,max);
                    String message = "Used: " + StringUtils.toHuman(um) + " of " + StringUtils.toHuman(mm) + "  (Free:" + StringUtils.toHuman(fm) + " Total:" + StringUtils.toHuman(tm) + " Max:"+ StringUtils.toHuman(mm) +") (recent used:[" + (min.value == null ? "Min is NULL" : StringUtils.toHuman(min.value)) +".." + (max.value ==null ? "Max is NULL" :  StringUtils.toHuman(max.value)) +"])";
                    if (tglLog.isSelected()) {
                        if (logMemory++ % 60 == 0 ) {
                            Logger.getLogger(Memory.class.getName()).log(Level.INFO,message);
                        }
                    } else 
                        logMemory = 0;
                    if (txtMemory!=null) {
                        txtMemory.setText(message);
                    }
                    if (tglAGC.isSelected() && mm-um < 10*1024*1024 && didgc== 0) {
                        Logger.getLogger(Memory.class.getName()).log(Level.INFO,"AutoGC triggered");
                        message = "Used: " + StringUtils.toHuman(um) + " of " + StringUtils.toHuman(mm) + "  (Free:" + StringUtils.toHuman(fm) + " Total:" + StringUtils.toHuman(tm) + " Max:"+ StringUtils.toHuman(mm) +")";
                        Logger.getLogger(Memory.class.getName()).log(Level.INFO,"Memory before GC:" + message);
                        
                        System.gc();
                        System.gc();
                        
                        fm = runtime.freeMemory();
                        mm = runtime.maxMemory();
                        tm = runtime.totalMemory();
                        um = tm-fm;
                        message = "Used: " + StringUtils.toHuman(um) + " of " + StringUtils.toHuman(mm) + "  (Free:" + StringUtils.toHuman(fm) + " Total:" + StringUtils.toHuman(tm) + " Max:"+ StringUtils.toHuman(mm) +")";
                        Logger.getLogger(Memory.class.getName()).log(Level.INFO,"Memory after GC:" + message);
                        didgc=100;
                    } else if (didgc>0) {
                        didgc--;
                    }
                } catch (Exception e) {
                    Logger.getLogger(Memory.class.getName()).log(Level.INFO,"Error during memory display:",e);
                }
                running.set(false);
            }
        }
        
    }
    
    /**
     * Creates new form Memory
     */
    public Memory() {
        initComponents();
        m_scanTimer.scheduleAtFixedRate(new ScanTask(), 1000, 1000);
        this.addAncestorListener(new AncestorListener() {

            public void ancestorAdded(AncestorEvent event) {
                Component c =  Memory.this;
                while (c.getParent() != null) {
                    c = c.getParent();
                }
                if (c instanceof java.awt.Window) {
                    ((java.awt.Window) c).addWindowListener(new WindowListener() {

                        public void windowOpened(WindowEvent e) {
                            m_scanTimer.scheduleAtFixedRate(new ScanTask(), 1000, 1000);
                        }

                        public void windowClosing(WindowEvent e) {
                        }

                        public void windowClosed(WindowEvent e) {
                            m_scanTimer.cancel();
                        }

                        public void windowIconified(WindowEvent e) {
                        }

                        public void windowDeiconified(WindowEvent e) {
                        }<commit_msg>Memory Component runs now more often to update min max and average more  often - but update of the interface is still only done ones per second<commit_after>/* 
 * Copyright 2016 <NAME> <<EMAIL>>.
 *
 * Licensed 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.rappsilber.gui.components.memory;

import java.awt.Component;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.LinkedList;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import org.rappsilber.utils.ObjectWrapper;
import org.rappsilber.utils.RArrayUtils;
import org.rappsilber.utils.StringUtils;

/**
 *
 * @author <NAME> <<EMAIL>>
 */
public class Memory extends javax.swing.JPanel {

    
    java.util.Timer m_scanTimer = new java.util.Timer(true);
    private int m_timeout = 600;
    Runtime runtime = Runtime.getRuntime();
    
    
    protected class ScanTask extends TimerTask {
        AtomicBoolean running = new AtomicBoolean(false);
        double recentMinFreeMem = 0;
        double recentMaxFreeMem = 0;
        LinkedList<Double> recent = new LinkedList<>();
        int maxRecent=100;
        int updateSteps=10;
        int logMemory = 0;
        int didgc = 0;
        int step=0;
        
        @Override
        public void run() {
            if (running.compareAndSet(false, true)) {
                try {
                    double fm = runtime.freeMemory();
                    String fmu = "B";
                    double mm = runtime.maxMemory();
                    double tm = runtime.totalMemory();
                    double um = tm-fm;
                    recent.add(um);
                    if (recent.size()>maxRecent) {
                        recent.removeFirst();
                    }
                    if (++step==updateSteps) {
                        step=0;
                        ObjectWrapper<Double> min= new ObjectWrapper<>();
                        ObjectWrapper<Double> max = new ObjectWrapper<>();
                        ObjectWrapper<Double> average = new ObjectWrapper<>();
                        RArrayUtils.minmaxaverage(recent,min,max,average);
                        String message = "Used: " + StringUtils.toHuman(um) + " of " + StringUtils.toHuman(mm) + "  (Free:" + StringUtils.toHuman(fm) + " Total:" + StringUtils.toHuman(tm) + " Max:"+ StringUtils.toHuman(mm) +") (recent used:[" +  StringUtils.toHuman(min.value) +".."+ StringUtils.toHuman(average.value) +".." + StringUtils.toHuman(max.value) +"])";
                        if (tglLog.isSelected()) {
                            if (logMemory++ % 60 == 0 ) {
                                Logger.getLogger(Memory.class.getName()).log(Level.INFO,message);
                            }
                        } else 
                            logMemory = 0;
                        if (txtMemory!=null) {
                            txtMemory.setText(message);
                        }
                        if (tglAGC.isSelected() && mm-um < 10*1024*1024 && didgc== 0) {
                            Logger.getLogger(Memory.class.getName()).log(Level.INFO,"AutoGC triggered");
                            message = "Used: " + StringUtils.toHuman(um) + " of " + StringUtils.toHuman(mm) + "  (Free:" + StringUtils.toHuman(fm) + " Total:" + StringUtils.toHuman(tm) + " Max:"+ StringUtils.toHuman(mm) +")";
                            Logger.getLogger(Memory.class.getName()).log(Level.INFO,"Memory before GC:" + message);

                            System.gc();
                            System.gc();

                            fm = runtime.freeMemory();
                            mm = runtime.maxMemory();
                            tm = runtime.totalMemory();
                            um = tm-fm;
                            message = "Used: " + StringUtils.toHuman(um) + " of " + StringUtils.toHuman(mm) + "  (Free:" + StringUtils.toHuman(fm) + " Total:" + StringUtils.toHuman(tm) + " Max:"+ StringUtils.toHuman(mm) +")";
                            Logger.getLogger(Memory.class.getName()).log(Level.INFO,"Memory after GC:" + message);
                            didgc=100;
                        } else if (didgc>0) {
                            didgc--;
                        }
                    }
                } catch (Exception e) {
                    Logger.getLogger(Memory.class.getName()).log(Level.INFO,"Error during memory display:",e);
                }
                running.set(false);
            }
        }
        
    }
    
    /**
     * Creates new form Memory
     */
    public Memory() {
        initComponents();
        m_scanTimer.scheduleAtFixedRate(new ScanTask(), 100, 100);
        this.addAncestorListener(new AncestorListener() {

            public void ancestorAdded(AncestorEvent event) {
                Component c =  Memory.this;
                while (c.getParent() != null) {
                    c = c.getParent();
                }
                if (c instanceof java.awt.Window) {
                    ((java.awt.Window) c).addWindowListener(new WindowListener() {

                        public void windowOpened(WindowEvent e) {
                            m_scanTimer.scheduleAtFixedRate(new ScanTask(), 1000, 1000);
                        }

                        public void windowClosing(WindowEvent e) {
                        }

                        public void windowClosed(WindowEvent e) {
                            m_scanTimer.cancel();
                        }

                        public void windowIconified(WindowEvent e) {
                        }

                        public void windowDeiconified(WindowEvent e) {
                        }
{'fraction_non_alphanumeric': 0.11065342056376706, 'fraction_numerical': 0.008642274452790566, 'mean_word_length': 1.6001679966400673, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1888988', 'n_tokens_mistral': 3370, 'n_tokens_neox': 3092, 'n_words': 731}