I have a question when creating a class:
ej = new Ejemplo("1");
I would like to know what a class amounts to. I always wanted to create many classes for better identification.
I've read that when someone drops too many objects into a HashMap it causes lag, and I think a class weighs much more than a single Object.
If I create many classes for example 200 classes, will this cause delays? And also is it necessary to delete them?
I am making a game, when the player grabs the coin that coin disappears, but that coin is a class, when the coin disappears the class becomes useless but it is still there.
To define a class it is important to define an Object, which is an entity that contains properties (attributes) and behavior (methods). A Class is defined by an object type.
You can create many classes which each instance occupies memory but you must remember that the resources on mobile devices are limited, if you fill them with objects at some point you will get an OutofMemory error , which is very common when you do not perform optimal memory management, You will first experience your app becoming slow (laggy) and later it may cause your app to crash.
OutofMemory error : Caused when the Java virtual machine cannot allocate an object because it is out of memory, and no more memory can be made available by the Garbage Collector.
The Garbage Collector's job is to remove unreferenced objects from the stack.
If you have objects that are "useless but still in memory", dereference them so that the Garbage Collector takes action and frees up memory that your application might require.
I have read several articles where they comment that the Garbage Collector must be called to free memory.
Currently it is not something that ensures to free memory since doing this does not ensure objects are collected (don't try to outsmart the operating system).
The simplest case to remove the object from memory is simply by assigning the value null:
You can also implement the finalize() method .
This is the official documentation related to memory management in Android (English):
https://developer.android.com/training/articles/memory.html
You have a confusion between class and object . I will try to explain to you the relationship and difference between these.
Strictly speaking, at runtime of a program you cannot create a class. The only thing you can create in memory are objects.
Of course there is a relationship between class and object. Let's first see the definition found in Concepts of objects and classes in Java
I know this is confusing at first and the best analogy I know is the mold-object analogy.
Imagine a real life object, which has a certain shape, for example a plastic figure.
The class is the mold with which you can make each instance of the figure. Each instance will have its own attributes, for example its color.
Going back to the beginning, at runtime you cannot create classes . What you do is instantiate objects , using the constructor of their class, which is how each is made . What occupies memory in the computer are these instances of objects.
Whether 200 instances of a particular object is too many or too few depends on the amount of memory available to your process on the device it's running on, and how much memory each instance occupies. In addition to memory, the object instances created can consume other resources.
Speaking of java in particular, you don't need to manually free memory, but you do need to be careful not to keep references to objects you no longer need, to minimize unnecessary consumption of memory and other resources. The JVM garbage collector will take care of freeing the memory of all those objects that have gone out of scope. You can influence an object to be freed, even before it leaves the scope where it was created by removing the reference to it. For example, if you have a routine like this:
You can influence the GC to destroy the object sooner by making this change:
When I say you can influence , I mean that you don't have a guarantee when the object instance will be destroyed. That is decided by the JVM and can vary between versions of it, operating system, etc.
If it is imperative that resources be released after a certain point in your program, it is better to implement the release in a method that you call yourself, for example: