Runtime.getRuntime ()। TotalMemory () और freeMemory () क्या हैं?


146

मैं सोच रहा था कि Runtime.getRuntime ()। TotalMemory () , Runtime.getRuntime ()। FreeMemory () , और Runtime.getRuntime ()। मैक्सिममोरी () का सही अर्थ क्या है।

मेरी समझ यह Runtime.getRuntime().totalMemory()है कि मेरी स्मृति कुल प्रक्रिया का उपयोग कर रही है। क्या वो सही है?

कैसे freeMemory()और maxMemory()?

जवाबों:


195

एपीआई के अनुसार

totalMemory()

जावा वर्चुअल मशीन में मेमोरी की कुल मात्रा लौटाता है। इस विधि द्वारा लौटाया गया मान समय के अनुसार भिन्न हो सकता है, जो मेजबान वातावरण पर निर्भर करता है। ध्यान दें कि किसी भी प्रकार के ऑब्जेक्ट को रखने के लिए आवश्यक मेमोरी की मात्रा कार्यान्वयन-निर्भर हो सकती है।

maxMemory()

अधिकतम वर्चुअल मेमोरी का उपयोग करता है जिसे जावा वर्चुअल मशीन उपयोग करने का प्रयास करेगी। यदि कोई अंतर्निहित सीमा नहीं है, तो Long.MAX_VALUE मान वापस आ जाएगा।

freeMemory()

जावा वर्चुअल मशीन में मुफ्त मेमोरी की मात्रा देता है। जीसी विधि को कॉल करने से फ्रीमैरी द्वारा लौटाए गए मूल्य में वृद्धि हो सकती है।

आपके प्रश्न के संदर्भ में, मान maxMemory()लौटाता है -Xmx

आप सोच रहे होंगे कि टोटल मीमोरी () और मैक्सिममोरी () क्यों है । इसका उत्तर यह है कि JVM मेमोरी को आलसी तरीके से आवंटित करता है। कहते हैं कि आप अपने जावा प्रक्रिया को इस तरह शुरू करते हैं:

java -Xms64m -Xmx1024m Foo

आपकी प्रक्रिया 64mb मेमोरी से शुरू होती है, और यदि और जब इसे (1024 मीटर तक) की आवश्यकता होती है, तो यह मेमोरी आवंटित करेगी। फू के लिए वर्तमान में JVM के लिए उपलब्ध totalMemory()मेमोरी की मात्रा से मेल खाती है । यदि JVM को अधिक मेमोरी की आवश्यकता होती है, तो यह अधिकतम मेमोरी तक आलसी को आवंटित करेगा । यदि आप साथ चलते हैं , तो आपको मिलने वाला मूल्य और बराबर होगा।-Xms1024m -Xmx1024mtotalMemory()maxMemory()

यदि आप उपयोग की गई मेमोरी की मात्रा की सही गणना करना चाहते हैं, तो आप निम्न गणना के साथ ऐसा करते हैं:

final long usedMem = totalMemory() - freeMemory();

-Xmxमूल्य सीधे प्रारंभिक को प्रभावित करने लगता है maxMemory()लेकिन मैं रिपोर्ट को देखा है मूल्य maxMemory(), जबकि कार्यक्रम चल रहा है, एक छोटी राशि की वृद्धि हुई है, शायद ~ 1%।
H2ONaCl

2
यह किस तरह से अलग है Debug.getNativeHeapFreeSize()?
इगोरगानापोलस्की

@ H2ONaCl हाँ, यह थोड़ा बदल सकता है, क्योंकि JVM UseAdaptiveSizePolicyडिफ़ॉल्ट रूप से सक्षम है। और बीटीडब्ल्यू: maxMemory()= Xmx- एकल उत्तरजीवी स्थान का आकार। क्यों? क्योंकि एक ही समय में, केवल एक बचे स्थान का उपयोग किया जा सकता है।
जी। डेमेकी

236

नाम और मूल्य भ्रमित कर रहे हैं। यदि आप कुल मुफ्त मेमोरी की तलाश कर रहे हैं, तो आपको अपने स्वयं के द्वारा इस मूल्य की गणना करनी होगी। यह वह नहीं है जो आपको मिलता है freeMemory();

निम्नलिखित गाइड देखें:

कुल निर्दिष्ट मेमोरी , यह कॉन्फ़िगर -Xmx मान के बराबर होगी :

Runtime.getRuntime () maxMemory ()।;

वर्तमान आवंटित मुक्त मेमोरी , नई वस्तुओं के लिए तैयार वर्तमान आवंटित स्थान है । सावधानी यह कुल उपलब्ध मुफ्त मेमोरी नहीं है:

Runtime.getRuntime () freeMemory ()।;

कुल आवंटित मेमोरी , जावा प्रक्रिया के लिए आरक्षित कुल आवंटित स्थान है:

Runtime.getRuntime () totalMemory ()।;

उपयोग की गई स्मृति , की गणना की जानी चाहिए:

usedMemory = Runtime.getRuntime ()। TotalMemory () - Runtime.getRuntime ()। freeMemory ();

कुल मुफ्त मेमोरी , की गणना की जानी चाहिए:

freeMemory = Runtime.getRuntime ()। maxMemory () - usedMemory;

एक तस्वीर स्पष्ट करने में मदद कर सकती है:

जावा रनटाइम मेमोरी


1
क्या यह अलग है Debug.getMemoryInfo()?
इगोरगानापल्स्की

1
नोट: उपयोग की गई मेमोरी में अब संदर्भित ऑब्जेक्ट नहीं हो सकते हैं जो अगले GC द्वारा बह जाएंगे।
गाब :05

@cheneym, फ्री और अनलॉक्ड मेमोरी को कब्जे में लिया जाएगा क्योंकि जावा बाइट कोड के निर्देश प्रोसेसर द्वारा संसाधित किए जाएंगे जब "Xmx - Usedmemory" मशीन में एवलेब हो। Xmx बैलून की अधिकतम क्षमता की तरह है जो मशीन में ही एवलब् एयर से मिलने वाली हवा से भर सकता है, जैसे ही इसे हवा मिलेगी, यह भर जाएगी और Xmx की सीमा से अधिक हो जाने पर इसमें विस्फोट होगा। लेकिन टोटल फ्रीमोरी वोंट जेवीएम के लिए मशीन में वास्तविक एवबल मेमोरी नहीं बताते हैं, लेकिन सिर्फ nmbr.Is वहाँ किसी भी तरह से मैं मशीन में वास्तविक एवलब्बल मेमोरी का पता लगा सकता हूं ताकि मुझे पता चल सके कि आरकेडी मेमोरी एवीब्लब्ल है या नहीं, जेवीएम रिमैनिंग के लिए है प्रक्रिया?
मारिया

12

इसे बेहतर ढंग से समझने के लिए, इस प्रोग्राम को चलाएं (jdk1.7.x में):

$ java -Xms1025k -Xmx1025k -XshowSettings:vm  MemoryTest

यह प्रिंट होगा JVM विकल्प और इस्तेमाल किया , मुक्त , कुल और अधिकतम JVM में उपलब्ध स्मृति।

public class MemoryTest {    
    public static void main(String args[]) {
                System.out.println("Used Memory   :  " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) + " bytes");
                System.out.println("Free Memory   : " + Runtime.getRuntime().freeMemory() + " bytes");
                System.out.println("Total Memory  : " + Runtime.getRuntime().totalMemory() + " bytes");
                System.out.println("Max Memory    : " + Runtime.getRuntime().maxMemory() + " bytes");            
        }
}

8

अन्य सभी उत्तरों का कोडित संस्करण (लेखन के समय):

import java.io.*;

/**
 * This class is based on <a href="http://stackoverflow.com/users/2478930/cheneym">cheneym</a>'s
 * <a href="http://stackoverflow.com/a/18375641/253468">awesome interpretation</a>
 * of the Java {@link Runtime}'s memory query methods, which reflects intuitive thinking.
 * Also includes comments and observations from others on the same question, and my own experience.
 * <p>
 * <img src="https://i.stack.imgur.com/GjuwM.png" alt="Runtime's memory interpretation">
 * <p>
 * <b>JVM memory management crash course</b>:
 * Java virtual machine process' heap size is bounded by the maximum memory allowed.
 * The startup and maximum size can be configured by JVM arguments.
 * JVMs don't allocate the maximum memory on startup as the program running may never require that.
 * This is to be a good player and not waste system resources unnecessarily.
 * Instead they allocate some memory and then grow when new allocations require it.
 * The garbage collector will be run at times to clean up unused objects to prevent this growing.
 * Many parameters of this management such as when to grow/shrink or which GC to use
 * can be tuned via advanced configuration parameters on JVM startup.
 *
 * @see <a href="http://stackoverflow.com/a/42567450/253468">
 *     What are Runtime.getRuntime().totalMemory() and freeMemory()?</a>
 * @see <a href="http://www.oracle.com/technetwork/java/javase/memorymanagement-whitepaper-150215.pdf">
 *     Memory Management in the Sun Java HotSpot™ Virtual Machine</a>
 * @see <a href="http://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html">
 *     Full VM options reference for Windows</a>
 * @see <a href="http://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html">
 *     Full VM options reference for Linux, Mac OS X and Solaris</a>
 * @see <a href="http://www.oracle.com/technetwork/articles/java/vmoptions-jsp-140102.html">
 *     Java HotSpot VM Options quick reference</a>
 */
public class SystemMemory {

    // can be white-box mocked for testing
    private final Runtime runtime = Runtime.getRuntime();

    /**
     * <b>Total allocated memory</b>: space currently reserved for the JVM heap within the process.
     * <p>
     * <i>Caution</i>: this is not the total memory, the JVM may grow the heap for new allocations.
     */
    public long getAllocatedTotal() {
        return runtime.totalMemory();
    }

    /**
     * <b>Current allocated free memory</b>: space immediately ready for new objects.
     * <p>
     * <i>Caution</i>: this is not the total free available memory,
     * the JVM may grow the heap for new allocations.
     */
    public long getAllocatedFree() {
        return runtime.freeMemory();
    }

    /**
     * <b>Used memory</b>:
     * Java heap currently used by instantiated objects. 
     * <p>
     * <i>Caution</i>: May include no longer referenced objects, soft references, etc.
     * that will be swept away by the next garbage collection.
     */
    public long getUsed() {
        return getAllocatedTotal() - getAllocatedFree();
    }

    /**
     * <b>Maximum allocation</b>: the process' allocated memory will not grow any further.
     * <p>
     * <i>Caution</i>: This may change over time, do not cache it!
     * There are some JVMs / garbage collectors that can shrink the allocated process memory.
     * <p>
     * <i>Caution</i>: If this is true, the JVM will likely run GC more often.
     */
    public boolean isAtMaximumAllocation() {
        return getAllocatedTotal() == getTotal();
        // = return getUnallocated() == 0;
    }

    /**
     * <b>Unallocated memory</b>: amount of space the process' heap can grow.
     */
    public long getUnallocated() {
        return getTotal() - getAllocatedTotal();
    }

    /**
     * <b>Total designated memory</b>: this will equal the configured {@code -Xmx} value.
     * <p>
     * <i>Caution</i>: You can never allocate more memory than this, unless you use native code.
     */
    public long getTotal() {
        return runtime.maxMemory();
    }

    /**
     * <b>Total free memory</b>: memory available for new Objects,
     * even at the cost of growing the allocated memory of the process.
     */
    public long getFree() {
        return getTotal() - getUsed();
        // = return getAllocatedFree() + getUnallocated();
    }

    /**
     * <b>Unbounded memory</b>: there is no inherent limit on free memory.
     */
    public boolean isBounded() {
        return getTotal() != Long.MAX_VALUE;
    }

    /**
     * Dump of the current state for debugging or understanding the memory divisions.
     * <p>
     * <i>Caution</i>: Numbers may not match up exactly as state may change during the call.
     */
    public String getCurrentStats() {
        StringWriter backing = new StringWriter();
        PrintWriter out = new PrintWriter(backing, false);
        out.printf("Total: allocated %,d (%.1f%%) out of possible %,d; %s, %s %,d%n",
                getAllocatedTotal(),
                (float)getAllocatedTotal() / (float)getTotal() * 100,
                getTotal(),
                isBounded()? "bounded" : "unbounded",
                isAtMaximumAllocation()? "maxed out" : "can grow",
                getUnallocated()
        );
        out.printf("Used: %,d; %.1f%% of total (%,d); %.1f%% of allocated (%,d)%n",
                getUsed(),
                (float)getUsed() / (float)getTotal() * 100,
                getTotal(),
                (float)getUsed() / (float)getAllocatedTotal() * 100,
                getAllocatedTotal()
        );
        out.printf("Free: %,d (%.1f%%) out of %,d total; %,d (%.1f%%) out of %,d allocated%n",
                getFree(),
                (float)getFree() / (float)getTotal() * 100,
                getTotal(),
                getAllocatedFree(),
                (float)getAllocatedFree() / (float)getAllocatedTotal() * 100,
                getAllocatedTotal()
        );
        out.flush();
        return backing.toString();
    }

    public static void main(String... args) {
        SystemMemory memory = new SystemMemory();
        System.out.println(memory.getCurrentStats());
    }
}

7

रनटाइम # TotalMemory - मेमोरी जो JVM ने इस प्रकार आवंटित की है। यह जरूरी नहीं है कि उपयोग या अधिकतम में क्या है।

रनटाइम # maxMemory - मेमोरी की अधिकतम मात्रा जिसे जेवीएम का उपयोग करने के लिए कॉन्फ़िगर किया गया है। एक बार जब आपकी प्रक्रिया इस राशि तक पहुँच जाती है, तो JVM अधिक आवंटित नहीं करेगा और इसके बजाय GC बहुत अधिक बार।

रनटाइम # फ्रीमोरी - मुझे यकीन नहीं है कि अगर यह अधिकतम या कुल के हिस्से से मापा जाता है जो अप्रयुक्त है। मैं अनुमान लगा रहा हूं कि यह कुल के हिस्से का माप है जो अप्रयुक्त है।


5

कचरा-संग्रह तंत्र द्वारा JVM हीप का आकार बढ़ने योग्य और सिकुड़ने योग्य हो सकता है। लेकिन, यह अधिकतम स्मृति आकार पर आवंटित नहीं कर सकता है: Runtime.maxMemory। यह अधिकतम स्मृति का अर्थ है। कुल मेमोरी का अर्थ है आवंटित आकार का आकार। और मुक्त स्मृति का मतलब है कुल स्मृति में उपलब्ध आकार।

उदाहरण) java -Xms20M -Xmn10M -Xmx50M ~~~ इसका मतलब है कि jvm को स्टार्ट (ms) पर ढेर 20M आवंटित करना चाहिए। इस मामले में, कुल मेमोरी 20M है। मुक्त मेमोरी 20M से उपयोग किया जाने वाला आकार है। यदि अधिक हीप की आवश्यकता है, तो JVM अधिक आवंटित करता है, लेकिन 50M (mx) से अधिक नहीं कर सकता। अधिकतम के मामले में, कुल मेमोरी 50M है, और फ्री साइज 50M- यूज्ड साइज है। के रूप में minumum आकार (एमएन) के लिए, अगर ढेर का उपयोग नहीं किया जाता है, तो jvm हीप के आकार को 10M तक छोटा कर सकता है।

यह तंत्र स्मृति की दक्षता के लिए है। यदि छोटे java प्रोग्राम विशाल निश्चित आकार के हीप मेमोरी पर चलते हैं, तो बहुत अधिक मेमोरी बेकार हो सकती है।


1

आप एमबी प्रारूप में परिणाम देख सकते हैं , जिसमें 1024 x 1024 का विभाजन 1 एमबी के बराबर है ।

int dataSize = 1024 * 1024;

System.out.println("Used Memory   : " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/dataSize + " MB");
System.out.println("Free Memory   : " + Runtime.getRuntime().freeMemory()/dataSize + " MB");
System.out.println("Total Memory  : " + Runtime.getRuntime().totalMemory()/dataSize + " MB");
System.out.println("Max Memory    : " + Runtime.getRuntime().maxMemory()/dataSize + " MB");  
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.