File indexing completed on 2024-05-19 03:56:24

0001 /*
0002     This file is part of the KDE Frameworks
0003 
0004     SPDX-FileCopyrightText: 2022 Mirco Miranda
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 #ifndef KMEMORYINFO_H
0009 #define KMEMORYINFO_H
0010 
0011 #include <QSharedDataPointer>
0012 
0013 #include <kcoreaddons_export.h>
0014 
0015 class KMemoryInfoPrivate;
0016 
0017 /**
0018  * @brief The KMemoryInfo class provides an interface to get memory information (RAM/SWAP).
0019  *
0020  * To use the class, simply create an instance.
0021  * \code
0022  * KMemoryInfo memInfo;
0023  * if (!memInfo.isNull()) {
0024  *     ...
0025  * }
0026  * \endcode
0027  *
0028  * @since 5.95
0029  */
0030 class KCOREADDONS_EXPORT KMemoryInfo
0031 {
0032 public:
0033     ~KMemoryInfo();
0034 
0035     /**
0036      * @brief KMemoryInfo
0037      * Constructs a class with a snapshot of the state of the memory. If an error occurs, a null object is returned.
0038      * @sa isNull.
0039      */
0040     KMemoryInfo();
0041 
0042     /**
0043      * @brief KMemoryInfo
0044      * Constructs a copy of the other memoryinfo.
0045      */
0046     KMemoryInfo(const KMemoryInfo &other);
0047 
0048     /**
0049      * @brief operator =
0050      * Makes a copy of the other memoryinfo and returns a reference to the copy.
0051      */
0052     KMemoryInfo &operator=(const KMemoryInfo &other);
0053 
0054     /**
0055      * @brief operator ==
0056      * @return @c true if this memoryinfo is equal to the other memoryinfo, otherwise @c false.
0057      */
0058     bool operator==(const KMemoryInfo &other) const;
0059 
0060     /**
0061      * @brief operator !=
0062      * @return @c true if this memoryinfo is different from the other memoryinfo, otherwise @c false.
0063      */
0064     bool operator!=(const KMemoryInfo &other) const;
0065 
0066     /**
0067      * @brief isNull
0068      * @return @c true if the class is null, otherwise @c false.
0069      */
0070     bool isNull() const;
0071 
0072     /**
0073      * @brief totalPhysical
0074      * @return The total system RAM in bytes.
0075      */
0076     quint64 totalPhysical() const;
0077 
0078     /**
0079      * @brief freePhysical
0080      *
0081      * The free memory is the amount of free RAM as reported by the operating system.
0082      * This value is often tainted with caches and buffers used by the operating system, resulting in a low value.
0083      * @note Don't use this value to determine if you have enough RAM for your data.
0084      * @return The free RAM reported by OS in bytes.
0085      * @sa availablePhysical.
0086      */
0087     quint64 freePhysical() const;
0088 
0089     /**
0090      * @brief availablePhysical
0091      *
0092      * The available memory is the free RAM without considering caches and buffers allocated by the operating system.
0093      * @note You should always use this value to check if there is enough RAM for your data.
0094      * @return The memory available to the processes in bytes.
0095      * @sa freePhysical.
0096      */
0097     quint64 availablePhysical() const;
0098 
0099     /**
0100      * @brief cached
0101      * @return The size of RAM used as cache in bytes.
0102      */
0103     quint64 cached() const;
0104 
0105     /**
0106      * @brief buffers
0107      * @return The size of RAM used as buffers in bytes. This value can be zero.
0108      */
0109     quint64 buffers() const;
0110 
0111     /**
0112      * @brief totalSwapFile
0113      * @return The size of swap file in bytes.
0114      * @note On an operating system where the paging file is dynamically allocated, this value can be zero when no memory pages are swapped.
0115      */
0116     quint64 totalSwapFile() const;
0117 
0118     /**
0119      * @brief freeSwapFile
0120      * @return The free swap size in bytes.
0121      */
0122     quint64 freeSwapFile() const;
0123 
0124 private:
0125     /**
0126      * @brief update Refresh the memory information.
0127      * @return @c true on success, otherwise @c false.
0128      */
0129     KCOREADDONS_NO_EXPORT bool update();
0130 
0131     QSharedDataPointer<KMemoryInfoPrivate> d;
0132 };
0133 
0134 #endif // KMEMORYINFO_H