Java Class / en - Storage

3

I have a question about the storage of the classes, I had created a question about storage, I had answered that ALL classes are stored ( ClaseC a = new ClaseC() ).

So, every time I would use new Random() I would be storing another class more, how could I create a class like " Random() "?

    
asked by Java doub 15.09.2016 в 18:43
source

2 answers

1

In addition to what your partner @MatiEzelQ tells you, the instances have a life cycle and this has to do with the garbage collection .

Currently there are two "types" of spaces for instances:

  • Young generation
  • Old generation

For each type of space there are different containers.

Young generation / New generation

This space contains 3 containers:

  • Eden: here we store the objects that we just instantiated.
  • Survivor 0: Here the objects that have no reference are stored at the moment of passing the garbage collector.
  • Survivor 1: If the garbage collector still finds objects without reference in Survivor 0, they are passed to Survivor 1.

Old generation / Old generation

If when the collector goes through the Survivor 1 finds that the objects there still have no reference, they become part of the old generation.

This generation contains 2 containers:

  • Ternured: here arrive the objects that do not follow without reference when the collector went through the Survivor 1. The collector will generally pass through here when the space is full , releasing the objects definitively .
  • Permanent: here the JVM loads the classes it needs for its operation. The collector will not go through here because these instances are permanent.
answered by 15.09.2016 / 19:31
source
3

Although I understand, the problem you say is that while you are installing more classes, you are going to be occupying more RAM memory with your program. If you are interested in an instance, you have two storage places (both in the ram):

The instances of your classes themselves are stored in the heap. That is, when doing a new ClaseA , the only thing you are doing is creating the instance in the heap. Now if you make ClaseA a = new ClaseA(); you are creating a variable in the STACK (called: a) that has a reference to the instance, which is stored in the HEAP.

I leave an image where you can see, perhaps, more clearly what I try to explain:

I hope I have understood your doubt well.

    
answered by 15.09.2016 в 19:13