Asked  7 Months ago    Answers:  5   Viewed   33 times

I'm processing an old database php array to a new database. The data .php files are in total around 220 MB large.

I've inserted these lines in the script so that it should run fine:

ini_set("memory_limit", "-1");
set_time_limit(0);

This is how I include the products:

// The exports made by PHPMYADMIN, exported as PHP-Array
require_once 'export/tx_ttproductsv2_products.php';
require_once 'export/tx_ttproductsv2_keyword.php';
require_once 'export/tx_ttproductsv2_keywords_in_products.php';
require_once 'export/tx_ttproductsv2_typebook.php';
require_once 'export/tx_ttproductsv2_typegospel7.php';
require_once 'export/tx_ttproductsv2_typemedia.php';

When the script is trying to require them I get this error:

PHP Fatal error: Out of memory (allocated 880541696) (tried to allocate 469762048 bytes) in ......

I've got an local EasyPHP installation running on x64 Win 7 SP1. I've got 6 GB memory, Intel i5.

How can I get PHP to run the whole script without the memory error?

 Answers

96

Memory limitation comes from the OS, not from PHP itself.

Your script allocated 800MB and is trying to allocate further 500MB

Try to run the script on 64bit OS with 64 bit PHP.

Wednesday, March 31, 2021
 
Eugenie
answered 7 Months ago
26

One of the things that really helped the memory issue in my case ended up being setting inPurgeable to true for my Bitmaps. See Why would I ever NOT use BitmapFactory's inPurgeable option? and the answer's discussion for more info.

Dianne Hackborn's answer and our subsequent discussion (also thanks, CommonsWare) helped clarify certain things I was confused about, so thank you for that.

Friday, June 4, 2021
 
dmp
answered 5 Months ago
dmp
86

Apache was crashing due to a memory leak which was caused by not closing a resource that was being used again and again in a for loop, as well as the script making use of recursion.

Thanks all for the help.

Wednesday, August 4, 2021
 
Philippe
answered 3 Months ago
98

I have about 300-400 concurrent connection/users on the site at the same time, on average. I DO NOT have SSH access :/

This is your problem.

Shared hosting companies usually limit more than just PHP memory, you could be reaching i/o, and memory limits elsewhere. You could be inside a VM already and reaching that limit, etc. Shared hosting comes cheap but once you start throwing decent traffic at it they generally suspend your account or you start running up against limits.

I'd advise upgrading with that much traffic to something like a VPS, dedicated server or move to cloud based hosting. You're obviously doing something right with the traffic levels so move off quickly :)

Friday, August 6, 2021
 
Vincent Stimper
answered 3 Months ago
86

You need to resize the image if it is very large.

Do as follows:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new URL(imageUrl).openStream(),null,options);
//use your required height and width in place of 80 (size of imageview maybe)
options.inSampleSize = calculateInSampleSize(options,80, 80); 
options.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeStream(new URL(imageUrl).openStream(),null,options);


public int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}
Wednesday, October 6, 2021
 
employeegts
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :