If I understand correctly, a 32-bit OS (Ubuntu) can only address 4GiB memory, so RAM with size larger than 4Gib will only be used 4Gib of itself and the rest is a waste.
That's not correct. You are correct that a 32-bit OS can only address 4GiB of virtual memory at a time. But that has nothing to do with physical memory. 32-bit OSes can address more than 4GiB of physical memory using, for example, PAE.
with virtual memory being swap + RAM, if the size of the virtual memory exceeds 4Gib, will the exceeding part be a waste for the 32-bit OS?
Virtual memory is a technique for creating things that look like memory that don't actually map to physical RAM. Mapped virtual memory cannot exceed 4GiB on a 32-bit OS. However, windowing can be used to allow more than 4GiB of virtual memory to be addressed, though not all of it at once.
if I now have to choose the size for my swap partition, is it a factor to consider that the 32-bit OS can only address 4GiB memory? Does the size of swap have to be chosen with respect to the 4Gib addressible limitation? Will the swap exceeding 4GiB always be a waste?
No, because the 32-bit OS limit is about how much virtual memory it can address at once. Say you're running eight processes and each of them want to use 1GiB of virtual memory. That's fine and doesn't hit any limitations, but that might require more than 4GiB of swap.
is virtual memory equal to RAM and swap? or can virtual memory use space on the hard drive outside the swap partition?
Virtual memory is limited by issues that have nothing to do with how much RAM or swap you have. Mapped files are virtual memory. Paging uses space outside the swap partition to store clean pages and space in the swap partition to store dirty pages. Ideally, the system can page both clean and dirty pages, as this gives it the most flexibility.
"If I understand correctly, a 32-bit OS (Ubuntu) can only address 4GiB memory" Sorry but this is flatly wrong. With PAE a 32-bit OS can address much more than 4 GiB of RAM. The 32-bit of the OS limits only virtual address space. – Jamie Hanrahan – 2016-08-28T05:00:40.173