How can I understand the reason my computer freezes?

I would like to ask a question: Due to my job I often use many programs together and sometimes my computer freezes, sometimes I can really feel its pain. This often happens when I both use Skype (video calls) and IntelliJ or Virtual Machine is on.

How can I understand if this is due by CPU or RAM (or maybe something else)?

Thank you for your help.

EDIT. As suggested by @heynnema, here is the result for commands free -h

              total        used        free      shared  buff/cache   available
Mem:          5,8Gi       4,2Gi       327Mi        67Mi       1,2Gi       1,2Gi
Swap:         2,0Gi       679Mi       1,3Gi

and swapon -s

Filename                Type        Size    Used    Priority
/swapfile               file        2097148 695552  -2

Solution 1:

It would be helpful to have a sort of graph so I can see what was happening while I was working

Then use ``Stacer` downloadable from the ubuntu software center

enter image description here

enter image description here

Solution 2:

Your /swapfile is probably too small. Let's increase it from 2G to 4G...

Note: Depending on your usage pattern, you may need more RAM.

Note: Incorrect use of the rm and dd commands can cause data loss. Suggest copy/paste.

In the terminal...

sudo swapoff -a           # turn off swap
sudo rm -i /swapfile      # remove old /swapfile

sudo dd if=/dev/zero of=/swapfile bs=1M count=4096

sudo chmod 600 /swapfile  # set proper file protections
sudo mkswap /swapfile     # init /swapfile
sudo swapon /swapfile     # turn on swap
free -h                   # confirm 6G RAM and 4G swap

Edit /etc/fstab, using sudo -H gedit /etc/fstab or sudo pico /etc/fstab.

Confirm this /swapfile line in /etc/fstab... and confirm no other “swap” lines... use SPACES in this line... confirm NO TABS...

/swapfile  none  swap  sw  0  0

reboot                    # reboot and verify operation