On Jan 26, 2023, at 9:30 AM, Roland Müller <rol...@gmail.com> wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CA%2B8p0G3nxAxWu0cNAJR_FWYQWvPdJHSP9v5X6VLhWFdh%2BxxenQ%40mail.gmail.com.
Am Mittwoch, 25. Januar 2023 schrieb 'Jakob Borg' via golang-nuts <golan...@googlegroups.com>:
> On 25 Jan 2023, at 12:26, Shashwat <qa80...@gmail.com> wrote:
>>
>> Is it safe to declare a variable inside an infinite loop ?
>>
>> for {
>> a := 0
>> a += 1
>> }
>>
>> Won't this cause memory allocation in every iteration ?
>>
>> As the declared variables will be allocated memory in the stack and not heap memory, so garbage collector can't clear the allocated memory, and eventually process will be terminated, isn't it?
>
> No. Apart from compiler optimisations, consider that the variable goes out of scope at the end of each iteration.
>
In Java this may cause memory fragmentation when the amount of allocated memory varies and the variables use much memory.
Dont know whether this may occur in Go too.