{"id":17874778,"url":"https://github.com/dastergon/oomutil","last_synced_at":"2025-03-21T23:31:32.070Z","repository":{"id":57523265,"uuid":"130577462","full_name":"dastergon/oomutil","owner":"dastergon","description":"A Go package with read-only operations for determining the Out-Of-Memory (OOM) status of a process on Linux ","archived":false,"fork":false,"pushed_at":"2018-04-22T21:39:42.000Z","size":14,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-01T10:41:25.974Z","etag":null,"topics":["golang","golang-library","golang-package","linux","process"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dastergon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-22T14:52:57.000Z","updated_at":"2024-07-04T13:54:40.000Z","dependencies_parsed_at":"2022-09-26T18:10:41.725Z","dependency_job_id":null,"html_url":"https://github.com/dastergon/oomutil","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dastergon%2Foomutil","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dastergon%2Foomutil/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dastergon%2Foomutil/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dastergon%2Foomutil/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dastergon","download_url":"https://codeload.github.com/dastergon/oomutil/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244166694,"owners_count":20409178,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","golang-library","golang-package","linux","process"],"created_at":"2024-10-28T11:11:23.916Z","updated_at":"2025-03-21T23:31:31.789Z","avatar_url":"https://github.com/dastergon.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# oomutil\nPackage oomutil implements some read-only operations for determining the Out-Of-Memory (OOM) status of a process on Linux.\n\nThis package retrieves information from:\n* /proc/(pid)/oom_score\n* /proc/(pid)/oom_score_adj\n* /proc/sys/vm/overcommit_memory\n\n### Installation\nInstall using standard `go get`:\n\n```shell\n\n$ go get -u github.com/dastergon/oomutil\n\n```\n\n### How to use\n\nThis is an example how to use this package:\n\n```golang\npackage main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n\n\t\"github.com/dastergon/oomutil\"\n)\n\nfunc main() {\n\tpid := os.Getpid()\n\tps, err := oomutil.NewOOMProcess(int32(pid))\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\toomScore, err := ps.OOMScore()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\toomScoreAdj, err := ps.OOMScoreAdj()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tmemoryOvercommit, err := ps.MemoryOvercommit()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tfmt.Printf(\"Memory Overcommit: %d\\nPID: %d =\u003e OOM Score: %d, OOM Score Adj: %d\\n\", memoryOvercommit, pid, oomScore, oomScoreAdj)\n}\n\n```\n\n## Determining OOM Status\nThe OOM-killer checks if the systems is truly out-of-memory, and selects a process to kill.\n\n### /proc/[pid]/oom_score\nAccording to [proc(5)](https://linux.die.net/man/5/proc) man page:\n```\n/proc/[pid]/oom_score (since Linux 2.6.11)\nThis file displays the current score that the kernel gives to this process for the purpose of selecting a process for the OOM-killer. A higher score means that the process is more likely to be selected by the OOM-killer. The basis for this score is the amount of memory used by the process, with increases (+) or decreases (-) for factors including:\n\n* whether the process creates a lot of children using fork(2) (+);\n* whether the process has been running a long time, or has used a lot of CPU time (-);\n* whether the process has a low nice value (i.e., \u003e 0) (+);\n* whether the process is privileged (-); and\n* whether the process is making direct hardware access (-).\n\nThe oom_score also reflects the adjustment specified by the oom_score_adj or oom_adj setting for the process.\n\nThe higher the value of oom_score of any process, the higher is its likelihood of getting killed by the OOM Killer when the system is running out of memory.\n```\n\n### /proc/[pid]/oom_score_adj\nAccording to [proc(5)](https://linux.die.net/man/5/proc) man page:\n\n```\n/proc/[pid]/oom_score_adj (since Linux 2.6.36)\nThis file can be used to adjust the badness heuristic used to select which process gets killed in out-of-memory conditions.\nThe badness heuristic assigns a value to each candidate task ranging from 0 (never kill) to 1000 (always kill) to determine which process is targeted. The units are roughly a proportion along that range of allowed memory the process may allocate from, based on an estimation of its current memory and swap use. For example, if a task is using all allowed memory, its badness score will be 1000. If it is using half of its allowed memory, its score will be 500.\n\nThere is an additional factor included in the badness score: root processes are given 3% extra memory over other tasks.\n\nThe amount of \"allowed\" memory depends on the context in which the OOM-killer was called. If it is due to the memory assigned to the allocating task's cpuset being exhausted, the allowed memory represents the set of mems assigned to that cpuset (see cpuset(7)). If it is due to a mempolicy's node(s) being exhausted, the allowed memory represents the set of mempolicy nodes. If it is due to a memory limit (or swap limit) being reached, the allowed memory is that configured limit. Finally, if it is due to the entire system being out of memory, the allowed memory represents all allocatable resources.\n\nThe value of oom_score_adj is added to the badness score before it is used to determine which task to kill. Acceptable values range from -1000 (OOM_SCORE_ADJ_MIN) to +1000 (OOM_SCORE_ADJ_MAX). This allows user space to control the preference for OOM-killing, ranging from always preferring a certain task or completely disabling it from OOM-killing. The lowest possible value, -1000, is equivalent to disabling OOM-killing entirely for that task, since it will always report a badness score of 0.\n\nConsequently, it is very simple for user space to define the amount of memory to consider for each task. Setting a oom_score_adj value of +500, for example, is roughly equivalent to allowing the remainder of tasks sharing the same system, cpuset, mempolicy, or memory controller resources to use at least 50% more memory. A value of -500, on the other hand, would be roughly equivalent to discounting 50% of the task's allowed memory from being considered as scoring against the task.\n\nFor backward compatibility with previous kernels, /proc/[pid]/oom_adj can still be used to tune the badness score. Its value is scaled linearly with oom_score_adj.\n\nWriting to /proc/[pid]/oom_score_adj or /proc/[pid]/oom_adj will change the other with its scaled value.\n```\n\n### /proc/sys/vm/overcommit_memory\nAccording to [proc(5)](https://linux.die.net/man/5/proc) man page:\n```\nThis file contains the kernel virtual memory accounting mode. Values are:\n0: heuristic overcommit (this is the default)\n1: always overcommit, never check\n2: always check, never overcommit\n```\n/proc/sys/vm/overcommit_memory set to 2, disables the OOM-Killer.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdastergon%2Foomutil","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdastergon%2Foomutil","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdastergon%2Foomutil/lists"}