static用在類的方法中,我現在的問題是static的這個變數是儲存在記憶體中,
1 具體儲存在記憶體中的哪個地方?
2 這個static的變數多長時間被收回?
3 過多的使用的static會不會造成伺服器記憶體不夠?
4 不同的使用者的static變數為什麼會互不影響?
回複內容:
static用在類的方法中,我現在的問題是static的這個變數是儲存在記憶體中,
1 具體儲存在記憶體中的哪個地方?
2 這個static的變數多長時間被收回?
3 過多的使用的static會不會造成伺服器記憶體不夠?
4 不同的使用者的static變數為什麼會互不影響?
When you declare a class method/variable as static, it is bound to and shared by the class, not the object. From a memory management perspective what this means is that when the class definition is loaded into the heap memory, these static objects are created there. When the class's actual object is created in the stack memory and when updates on the static properties are done, the pointer to the heap which contains the static object gets updated. This does help to reduce memory but not by much.
From a programming paradigm, people usually choose to use static variables for architectural advantages more than memory management optimization. In other words, one might create static variables like you mentioned, when one wants to implement a singleton or factory pattern. It provides more powerful ways of knowing what is going on at a "class" level as opposed to what transpires at an "object" level.
static的是儲存在記憶體中的吧,過多的使用應該會造成記憶體不足?