In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand weak references and soft references in SAP ABAP and Java". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "how to understand weak references and soft references in SAP ABAP and Java" together!
Have you noticed the ABAP abstract class CL_ABAP_REFERENCE? This abstract class has only one GET method that returns an object reference.
Its two subclasses CL_ABAP_SOFT_REFERENCE and CL_ABAP_WEAK_REFERENCE, respectively, implement the GET method of the abstract class, but both are implemented in the ABAP Kernel. For ABAP application developers, the source code is invisible and is a black box.
How to use this class? Or check SAP Help documentation:
An object in the system class CL_ABAP_WEAK_REFERENCE represents a weak reference to an object in a class. Unlike regular object references, a weak reference is ignored during execution of the garbage collector. This means that a weak reference does not prevent the referenced object from being deleted when the garbage collector is executed.
CL_ABAP_WEAK_REFERENCE An instance of the CL_ABAP_WEAK_REFERENCE class that represents a weak application pointing to an object instance. Literally, since there are weak references, there are naturally strong applications of their opposites. Suppose there is an ABAP class lcl_person:
DATA: lo_person TYPE REF TO lcl_person.CREATE OBJECT lo_person.
The above code defines a strong reference variable named lo_person that points to an instance of the lcl_person object. When the garbage collector is working, the memory area occupied by the lo_person object instance is not freed by the ABAP garbage collector as long as the strong reference lo_person of the lcl_person object instance is still valid (i.e. CLEAR is not called, or has not been reassigned to another object instance). In other words, an lcl_person object instance that has at least one strong reference to it will not have its memory region collected by the ABAP garbage collector under any circumstances.
Weak references, on the other hand, are ignored during garbage collection. This means that when the ABAP garbage collector starts working, if an object instance does not have any strong references to it, the object instance cannot escape being collected regardless of whether there are weak references to it.
Take a concrete example.
This 30-line ABAP report implements a simple LCL_PERSON class. Line 17 creates an instance of this class whose strong reference is stored in the reference variable lo_person.
Line 18 creates a weak reference lo_weak. wrapped around an instance of the LCL_PERSON object Calling the get method with a weak reference to lo_weak returns two different results in two different cases:
(1)If the lcl_person object instance created in line 17 has been collected by the garbage collector, get returns a null reference; (2) if the lcl_person object instance has not been collected, get returns a reference to it.
For those of you who need the source code for this article, you can find it on my SAP Community blog:
Weak reference in ABAP and Java
I gave this ABAP program two input parameters, clear and gc, that control whether to clear the strong reference variable lo_person and whether to call the ABAP garbage collector with code, respectively.
CLEAR: lo_person. is called if the parameter clear passed in when executing the program is set to true. According to the ABAP help documentation, CLEAR is applied to the reference variable lo_person, which after execution points to a null reference.
Another parameter gc is set to true to start the ABAP garbage collector in code: cl_abap_memory_utilities=>do_garbage_collection.
These two switches open and close, forming four different combinations. Under these four permutations, will the object instance pointed to by the weak reference lo_weak be collected by the ABAP garbage collector? The results are as follows:
Thus, an object instance pointed to by a weak reference will be collected by the garbage collector if it is not pointed to by at least one strongly referenced variable after the ABAP garbage collector is started.
Using the transaction code s_memory_inspector, make a memory snapshot after the garbage collector starts, and find that the lcl_person object instance has been collected in the first permutation of the above table: No memory objects found.
In the other three permutations, lcl_person escapes garbage collection:
Java also has a weak reference implementation for CL_ABAP_WEAK_REFERENCE: java.lang.ref.WeakReference.
Jerry ABAP program in this article, translated into Java code as follows:
Because the weak references in both languages work exactly the same, Jerry won't go into the Java version above.
Line 25 above sets the object pointed to by the strong reference variable jerry to null, line 26 starts the Java garbage collector, so line 27 calls the weak reference variable get method to get the result: null.
So what are the usage scenarios for this weak reference?
The best way to learn is to perform Where-used operations on CL_ABAP_WEAK_REFERENCE to find out which SAP standard applications use this class.
In the SAP CRM system Jerry uses below, weak references are used in many cases.
Among the more than 500 usage scenarios, the most typical is the implementation scenario of Cache. The following figure shows the CRM Enhancement Tool(AET) factory class method GGET_DATA_TYPE_HANDLER, which returns the corresponding handler instance according to two input parameters, field data type and field behavior type. These processors need to be instantiated from several database tables and stored in memory, so the initialization process takes some time. To avoid spending time repeatedly accessing database tables and creating new instances each time this method is invoked, the factory class introduces a caching mechanism, namely the inner table gt_type_handler_cache in line 21 of the following figure. Each time the GET method is invoked, check the inner table to see if there is a corresponding processor instance. If so, return directly, saving time-consuming processor instantiation.
Looking at the cache design, in the type structure of the line item, the type of handler is not a specific IF_AXT_DATATYPE_HANDLER, but a weak reference.
Technically, the seventh line of the above figure should be changed to:
handler TYPE REF TO IF_AXT_DATATYPE_HANDLER is also the correct design and is the most common cache design scheme for most ABAP applications. For the sake of discussion, I'll call this common scheme Scheme B, while the scheme in which the AET factory class uses a weak application-oriented processor instance is called Scheme A.
The advantage of Plan A is that it can be attacked and defended.
If ABAP garbage collector is not called, and there is at least one strong reference pointing to a processor instance, there is no big difference between the two schemes when running, the only slight difference is that scheme A hits the table in the read cache, gets the weak reference stored in the buffer, and then calls the get method of the weak reference, gets the processor instance and returns. In case B, after the table hit in the read cache, the processor instance itself exists in the buffer, and it can be returned directly to the calling end.
Fallback is when there are no more strong references to the processor instance in the program and the ABAP garbage collector starts working, the processor instance pointed to by the weak reference is destroyed, freeing up the memory it consumed. The next time the GET method is called, it reloads the data from the database, initializes the handler instance (red area below), and re-creates the weak reference (blue area below).
In a nutshell, the weak reference CL_ABAP_WEAK_REFERENCE is best used to describe instances of objects that are useful but not necessarily resident in memory. Therefore, there are many applications in the cache design of SAP CRM framework code.
In addition to strong and weak ABAP references, there is a third type of reference: soft reference (CL_ABAP_SOFT_REFERENCE).
In contrast to weak references, soft references point to objects that are destroyed only if they are not pointed to by any strong references and the garbage collector runs out of system memory. What percentage of available memory is "insufficient"? Soft references are not implemented in ABAP, so we can't go any further.
In Java, besides weak references and soft references, there are phantom references.
As the name implies, virtual references in Java are "useless" because the result obtained by the get method of virtual references is always null.
In some Chinese materials, Phantom Reference has been translated into "phantom reference" and "ghost reference" because of its performance behavior. The name reminds me of the Terran Wraith in StarCraft.
Virtual references are primarily used to track the collection of object instances by the garbage collector and must be used in conjunction with a Reference Queue. When the garbage collector is ready to reclaim an object, if it finds a virtual reference to that instance, it adds the virtual reference to the reference queue associated with it before reclaiming memory for that instance.
Because there are no false references in ABAP, Jerry does not expand the narrative.
Thank you for reading, the above is "how to understand SAP ABAP and Java in the weak reference and soft reference" content, after the study of this article, I believe that everyone on how to understand SAP ABAP and Java in the weak reference and soft reference this problem has a deeper understanding, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.