1/3
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Discuss Memory Allocation in Ruby
Memory Allocation
Sometimes, a Ruby extension needs memory for things that are not Ruby objects. For example, it may need memory for a large bitmap, an image, or many small structures that Ruby does not directly use.
To allocate such memory safely and work correctly with Ruby's garbage collector, special memory allocation routines should be used. These routines do a little more work than the normal malloc() function.
For example, when ALLOC_N finds that there is not enough memory for the requested amount, it asks the garbage collector to try to free some unused memory. If the requested memory is invalid or cannot be allocated even after this, it raises a NoMemError.
Memory Allocation Routines1. ALLOC_N
Syntax:type ALLOC_N(c-type, n)
Allocates memory for n objects of the given C type.
The c-type must be the actual name of the C type, not a variable of that type.
It is useful when we need memory for multiple objects of the same type.
2. ALLOC
Syntax:type ALLOC(c-type)
Allocates memory for one object of the given C type.
The result is converted into a pointer to that type.
3. REALLOC_N
Syntax:REALLOC_N(var, c-type, n)
Reallocates memory for n objects of the given C type.
The newly allocated memory is assigned to var.
var is a pointer to the given C type.
4. ALLOCA_N
Syntax:type ALLOCA_N(c-type, n)
Allocates memory for n objects of the given C type on the stack.
This memory is temporary.
It is automatically released when the function that called ALLOCA_N returns.
Which Method Allocates Memory for Objects in Ruby?
Ruby manages memory using its garbage collector. When there is too much memory being used, Ruby can release a small amount of unused memory/pages.
Ruby may use the malloc function to allocate memory. When memory is no longer needed, the operating system may release that freed memory back to the system. The exact behavior depends on the operating system and the implementation of the malloc library.
How Is Memory Allocated?
There are two basic ways in which memory can be allocated.
When we declare a variable or create an instance of a structure or class, memory is allocated for that object. The name given to the object is then used to access that block of memory.
The main storage allocation strategies are:
1. Static Allocation
Memory is arranged for all required data objects at compile time.
The storage is decided before the program starts running.
2. Stack Allocation
Run-time memory is managed using a stack.
Memory is automatically handled according to the order in which functions and variables are used.
Memory allocated on the stack using ALLOCA_N is automatically released when the function returns.
3. Heap Allocation
Memory is allocated and deallocated as needed during run time.
This memory comes from a data area called the heap.
Heap memory is useful when the required amount of memory is not known in advance.
Steps to Find a Memory Leak
A memory leak happens when memory that is no longer needed is not properly released or remains unnecessarily allocated.
The following steps can be used to find memory leaks in a Ruby program:
Check for unused gems in the Gemfile and remove them if they are not needed.
Check the issue tracker of every gem that is still present in the Gemfile to see whether other users have reported memory leaks.
Run RuboCop with the rubocop-performance extension to identify possible performance-related problems.
Visually check the Ruby code and look for places where memory may be unnecessarily allocated or not properly released.

Discuss characteristics of scripting languages.
Top 10 Characteristics of Scripting Languages
Easy to learn and use – They have simple syntax and require less code.
Interpreted – The code is usually executed directly by an interpreter without separate compilation.
Rapid development – Programs can be developed, tested, and modified quickly.
Dynamically typed – The data type of a variable can usually be determined during program execution.
Automatic memory management – Memory is managed automatically, often using garbage collection.
Task automation – They are useful for automating repetitive tasks such as file handling and system administration.
Portable – Scripts can often run on different operating systems if the required interpreter is available.
Extensible and embeddable – They can be integrated into existing applications and used to extend their functionality.
Interactive – Many scripting languages allow commands to be executed immediately, making testing and debugging easier.
Easy integration – They can easily interact with databases, operating-system commands, web services, and other software components.
Explain the uses of scripting languages
10 Easiest Uses of Scripting Languages
Web Development – Used to create interactive and dynamic websites.
Automation – Used to automatically perform repetitive tasks.
Data Processing – Used to collect, organize, and process data.
System Administration – Used to manage files, users, and system operations.
Database Management – Used to store, retrieve, update, and manage data in databases.
Software Testing – Used to automatically test programs and find errors.
File Management – Used to create, copy, move, rename, and delete files.
Prototyping – Used to quickly create a basic working model of an application.
Command Execution – Used to run multiple operating-system commands automatically.
Application Integration – Used to connect different programs and allow them to work together.

9. Explain the uses of scripting languages
