how to find the physical memory available?

4,494 views
Skip to first unread message

sijan

unread,
Apr 6, 2015, 10:55:54 AM4/6/15
to golan...@googlegroups.com
hi guys i am trying to display the total  physical memory.

i tried using the following code :

fmt.Printf("%v",runtime.MemStats.TotalAlloc)

but i get error saying 

# command-line-arguments
./memory.go:10:33: error: type ‘runtime.MemStats’ has no method ‘TotalAlloc’
 fmt.Printf("%v",runtime.MemStats.TotalAlloc);

can anyone help me find the correct way to use the TotalAlloc variable?

Ian Lance Taylor

unread,
Apr 6, 2015, 11:49:10 AM4/6/15
to sijan, golang-nuts
runtime.MemStats is a type, not a struct.

You want something like (untested):

var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Print(m.TotalAlloc)

Ian

Dave Cheney

unread,
Apr 6, 2015, 11:58:56 AM4/6/15
to golan...@googlegroups.com
Toyal physical memory of the host, or that your process is using?

Sijan Shrestha

unread,
Apr 7, 2015, 5:27:51 AM4/7/15
to golan...@googlegroups.com
I want to know the total physical memory of the host.

package main

import (
    "fmt"
    "runtime"
)

func main() {


    var m runtime.MemStats
    runtime.ReadMemStats(&m)
    fmt.Print(m.TotalAlloc)

}

This Progarm works fine , but i dont think its the code to find the total memory of the host . can you please Help on this ?

Sijan Shrestha

unread,
Apr 7, 2015, 5:31:43 AM4/7/15
to golan...@googlegroups.com, sijansh...@gmail.com
Thank you . The code works fine.
But i was trying to find the Total physical memory of the host, which is not what the code is providing ! can You please help me on this !

package main

import (
    "fmt"
    "runtime"
)

func main() {

    var m runtime.MemStats
    runtime.ReadMemStats(&m)
    fmt.Print(m.TotalAlloc)

}

The code returns me 0.00286 mb , which is not what i was searching for. the code I belive is returning memory allocated for the process it ran ???? any comments on this ?

Dave Cheney

unread,
Apr 7, 2015, 5:59:37 AM4/7/15
to golan...@googlegroups.com, sijansh...@gmail.com
This is true, memstats is just the memory usage of this Go process.

Finding the physcial memory of the host will be operating system specfic, for example of linux the free(1) command is used. This generally calls back to a sysctl or by looking in the /proc directory.

If you want a solution for linux, /proc/meminfo has the information you want.

Paul Borman

unread,
Apr 7, 2015, 10:01:53 AM4/7/15
to Dave Cheney, golang-nuts, sijansh...@gmail.com
A word of caution , learned from experience, about /proc/meminfo:

While you are only asking for total memory right now (which is straightforward), it is not straightforward if you want to know the amount of available memory.  MemFree is not how much memory is quickly available for processes (which is traditionally what one wants), rather, it is the amount of memory not being used for anything.  Linux makes memory otherwise unused available for a file cache.  When a page is used in the file cache it is not marked as free, but when processes need memory, those pages can be easily evicted and used for the process.

The formula I have used, while probably not perfect, is:

Available = MemFree + ActiveFile + InactiveFile - VMMinFreeBytes

That last value is $(cat /proc/sys/vm/min_free_kbytes) * 1024, the other three are from /proc/meminfo.  It seems to give a reasonable approximation of how much memory is really available.

    -Paul


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Carlos Castillo

unread,
Apr 7, 2015, 10:25:06 AM4/7/15
to golan...@googlegroups.com
It's been a while since I last used it, but you probably can use: github.com/cloudfoundry/gosigar which should support the common Go platforms (linux, darwin, windows).

Martin

unread,
Apr 7, 2015, 10:37:25 AM4/7/15
to Carlos Castillo, golan...@googlegroups.com
MemFree is not how much memory is quickly available for processes (which is traditionally what one wants)

Funny I was immediately thinking: Physical Memory is the amount of installed memory to be used for an inventory. **Which is traditionally what one wants** :)

I guess the views on "traditionally wanted things" might indeed differ a wee bit depending on the use case.

--

fatdo...@gmail.com

unread,
Apr 7, 2015, 6:30:16 PM4/7/15
to golan...@googlegroups.com
I don't know your platform but possibly Sysinfo_t in the syscall package could be useful.

fatdo...@gmail.com

unread,
Apr 7, 2015, 6:36:42 PM4/7/15
to golan...@googlegroups.com
I would have edited the post with more incite but my posts need approval which takes several hours most times and I cant edit a post while it waits approval. This project has nazi-like control to the point that its utterly laughable.

Ian Lance Taylor

unread,
Apr 7, 2015, 6:43:23 PM4/7/15
to AK Willis, golang-nuts
On Tue, Apr 7, 2015 at 3:36 PM, <fatdo...@gmail.com> wrote:
> I would have edited the post with more incite but my posts need approval
> which takes several hours most times and I cant edit a post while it waits
> approval. This project has nazi-like control to the point that its utterly
> laughable.

As far as I can tell your posts do not require approval, although I'm
not entirely certain that I'm looking at Google Groups in the right
way.

Nobody's posts require approval every time. We've set the group so
that the first time some e-mail address posts, someone has to approve
the post. We did that because without it we were getting inundated by
spam. After the first post, no further approval is required. It has
nothing to do with nazi-like control, it's just spam filtering, an
unfortunate requirement in today's Internet.

Ian

Paul Borman

unread,
Apr 7, 2015, 7:48:30 PM4/7/15
to Martin, Carlos Castillo, golang-nuts
Actually, isn't it a pony that people traditionally want?  But that seems out of context in a discussion of memory, doesn't it :-)

In the context of available memory (the subject of that statement), people are probably looking for much memory is quickly available for processes.

In the context of total memory, they are probably looking for the amount of installed memory to be used for an inventory.

Of course, neither MemFree nor MemTotal will likely give you what you want in either of those contexts.  MemTotal is not the total amount of memory in the machine.  For example, on my machine with 32GB of ram (0x2000000, it reports 32873888 (0x1F59DA0) for MemTotal.  (accounting for 98% of the memory).

All in all, while /proc/meminfo can be very useful, it also can be a bit misleading.


fatdo...@gmail.com

unread,
Apr 9, 2015, 9:22:54 PM4/9/15
to golan...@googlegroups.com, fatdo...@gmail.com
After I post on this account, it clearly states that my message must wait for approval. Sometimes it takes minutes, sometimes it takes hours. I have another work account that my messages  seem to be posted immediately. So, yes, some type of approval system does exist.

Caleb Spare

unread,
Apr 9, 2015, 9:27:09 PM4/9/15
to fatdo...@gmail.com, golang-nuts
Given that many (most?) people use this only as a mailing list (i.e. via email clients), editing posts is probably not useful or advisable.

-Caleb

--

Ian Lance Taylor

unread,
Apr 9, 2015, 9:38:19 PM4/9/15
to AK Willis, golang-nuts
On Thu, Apr 9, 2015 at 6:22 PM, <fatdo...@gmail.com> wrote:
> After I post on this account, it clearly states that my message must wait
> for approval. Sometimes it takes minutes, sometimes it takes hours. I have
> another work account that my messages seem to be posted immediately. So,
> yes, some type of approval system does exist.

I can't claim to understand Google Groups, but I'm the person that
does most of the approvals out of the moderation queue. I promise
that I'm not treating you specially. I can't find you in the list of
members, I'm not approving (or rejecting) your messages, and yet, here
they are. Is there some other Google Groups moderation queue
somewhere?

Ian



> On Tuesday, April 7, 2015 at 6:43:23 PM UTC-4, Ian Lance Taylor wrote:
>>
>> On Tue, Apr 7, 2015 at 3:36 PM, <fatdo...@gmail.com> wrote:
>> > I would have edited the post with more incite but my posts need approval
>> > which takes several hours most times and I cant edit a post while it
>> > waits
>> > approval. This project has nazi-like control to the point that its
>> > utterly
>> > laughable.
>>
>> As far as I can tell your posts do not require approval, although I'm
>> not entirely certain that I'm looking at Google Groups in the right
>> way.
>>
>> Nobody's posts require approval every time. We've set the group so
>> that the first time some e-mail address posts, someone has to approve
>> the post. We did that because without it we were getting inundated by
>> spam. After the first post, no further approval is required. It has
>> nothing to do with nazi-like control, it's just spam filtering, an
>> unfortunate requirement in today's Internet.
>>
>> Ian
>

Sijan Shrestha

unread,
Apr 21, 2015, 11:04:39 PM4/21/15
to golan...@googlegroups.com, sijansh...@gmail.com
Thank you for ur reply.

/proc/meminfo does have "memtotal" that gives the total physical memory.

does golang provide a package or any function which we can call, and will return the total physical memory???

Hotei

unread,
Apr 22, 2015, 9:44:09 AM4/22/15
to golan...@googlegroups.com, sijansh...@gmail.com
I don't think go provides one in the standard library but here's a version I use.  It won't run in playground for obvious reasons but should work on a "normal" Linux system (Ubunutu or Mint in my case).  Other OS that have proc in a different place or format may need tweaking.  OS without proc are handled with a very generic error so you might want to use build tags to craft a better error message if you expect to encounter that situation.  http://play.golang.org/p/Cg3kremWqZ

Hotei

unread,
Apr 22, 2015, 9:53:30 AM4/22/15
to golan...@googlegroups.com
By the way, if I was going to update the MemAvail function mentioned in my previous post I'd likely take Paul Borman's advice about how to calculate it.  My own experience has been that the value I calculated wasn't all that useful.  Since you want physical memory you'll have to modify the code a bit, but at least that value should be stable.

Reply all
Reply to author
Forward
0 new messages